math.pp 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {-------------------------------------------------------------------------
  12. Using functions from AMath/DAMath libraries, which are covered by the
  13. following license:
  14. (C) Copyright 2009-2013 Wolfgang Ehrhardt
  15. This software is provided 'as-is', without any express or implied warranty.
  16. In no event will the authors be held liable for any damages arising from
  17. the use of this software.
  18. Permission is granted to anyone to use this software for any purpose,
  19. including commercial applications, and to alter it and redistribute it
  20. freely, subject to the following restrictions:
  21. 1. The origin of this software must not be misrepresented; you must not
  22. claim that you wrote the original software. If you use this software in
  23. a product, an acknowledgment in the product documentation would be
  24. appreciated but is not required.
  25. 2. Altered source versions must be plainly marked as such, and must not be
  26. misrepresented as being the original software.
  27. 3. This notice may not be removed or altered from any source distribution.
  28. ----------------------------------------------------------------------------}
  29. {
  30. This unit is an equivalent to the Delphi Math unit
  31. (with some improvements)
  32. What's to do:
  33. o some statistical functions
  34. o optimizations
  35. }
  36. {$MODE objfpc}
  37. {$inline on }
  38. {$GOTO on}
  39. unit Math;
  40. interface
  41. {$ifndef FPUNONE}
  42. uses
  43. sysutils;
  44. {$IFDEF FPDOC_MATH}
  45. Type
  46. Float = MaxFloatType;
  47. Const
  48. MinFloat = 0;
  49. MaxFloat = 0;
  50. {$ENDIF}
  51. { Ranges of the IEEE floating point types, including denormals }
  52. {$ifdef FPC_HAS_TYPE_SINGLE}
  53. const
  54. { values according to
  55. https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Single-precision_examples
  56. }
  57. MinSingle = 1.1754943508e-38;
  58. MaxSingle = 3.4028234664e+38;
  59. {$endif FPC_HAS_TYPE_SINGLE}
  60. {$ifdef FPC_HAS_TYPE_DOUBLE}
  61. const
  62. { values according to
  63. https://en.wikipedia.org/wiki/Double-precision_floating-point_format#Double-precision_examples
  64. }
  65. MinDouble = 2.2250738585072014e-308;
  66. MaxDouble = 1.7976931348623157e+308;
  67. {$endif FPC_HAS_TYPE_DOUBLE}
  68. {$ifdef FPC_HAS_TYPE_EXTENDED}
  69. const
  70. MinExtended = 3.4e-4932;
  71. MaxExtended = 1.1e+4932;
  72. {$endif FPC_HAS_TYPE_EXTENDED}
  73. {$ifdef FPC_HAS_TYPE_COMP}
  74. const
  75. MinComp = -9.223372036854775807e+18;
  76. MaxComp = 9.223372036854775807e+18;
  77. {$endif FPC_HAS_TYPE_COMP}
  78. { the original delphi functions use extended as argument, }
  79. { but I would prefer double, because 8 bytes is a very }
  80. { natural size for the processor }
  81. { WARNING : changing float type will }
  82. { break all assembler code PM }
  83. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  84. type
  85. Float = Float128;
  86. const
  87. MinFloat = MinFloat128;
  88. MaxFloat = MaxFloat128;
  89. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  90. type
  91. Float = extended;
  92. const
  93. MinFloat = MinExtended;
  94. MaxFloat = MaxExtended;
  95. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  96. type
  97. Float = double;
  98. const
  99. MinFloat = MinDouble;
  100. MaxFloat = MaxDouble;
  101. {$elseif defined(FPC_HAS_TYPE_SINGLE)}
  102. type
  103. Float = single;
  104. const
  105. MinFloat = MinSingle;
  106. MaxFloat = MaxSingle;
  107. {$else}
  108. {$fatal At least one floating point type must be supported}
  109. {$endif}
  110. type
  111. PFloat = ^Float;
  112. PInteger = ObjPas.PInteger;
  113. TPaymentTime = (ptEndOfPeriod,ptStartOfPeriod);
  114. EInvalidArgument = class(ematherror);
  115. TValueRelationship = -1..1;
  116. const
  117. EqualsValue = 0;
  118. LessThanValue = Low(TValueRelationship);
  119. GreaterThanValue = High(TValueRelationship);
  120. {$push}
  121. {$R-}
  122. {$Q-}
  123. NaN = 0.0/0.0;
  124. Infinity = 1.0/0.0;
  125. NegInfinity = -1.0/0.0;
  126. {$pop}
  127. {$IFDEF FPDOC_MATH}
  128. // This must be after the above defines.
  129. {$DEFINE FPC_HAS_TYPE_SINGLE}
  130. {$DEFINE FPC_HAS_TYPE_DOUBLE}
  131. {$DEFINE FPC_HAS_TYPE_EXTENDED}
  132. {$DEFINE FPC_HAS_TYPE_COMP}
  133. {$ENDIF}
  134. { Min/max determination }
  135. function MinIntValue(const Data: array of Integer): Integer;
  136. function MaxIntValue(const Data: array of Integer): Integer;
  137. { Extra, not present in Delphi, but used frequently }
  138. function Min(a, b: Integer): Integer;inline; overload;
  139. function Max(a, b: Integer): Integer;inline; overload;
  140. { this causes more trouble than it solves
  141. function Min(a, b: Cardinal): Cardinal; overload;
  142. function Max(a, b: Cardinal): Cardinal; overload;
  143. }
  144. function Min(a, b: Int64): Int64;inline; overload;
  145. function Max(a, b: Int64): Int64;inline; overload;
  146. function Min(a, b: QWord): QWord;inline; overload;
  147. function Max(a, b: QWord): QWord;inline; overload;
  148. {$ifdef FPC_HAS_TYPE_SINGLE}
  149. function Min(a, b: Single): Single;inline; overload;
  150. function Max(a, b: Single): Single;inline; overload;
  151. {$endif FPC_HAS_TYPE_SINGLE}
  152. {$ifdef FPC_HAS_TYPE_DOUBLE}
  153. function Min(a, b: Double): Double;inline; overload;
  154. function Max(a, b: Double): Double;inline; overload;
  155. {$endif FPC_HAS_TYPE_DOUBLE}
  156. {$ifdef FPC_HAS_TYPE_EXTENDED}
  157. function Min(a, b: Extended): Extended;inline; overload;
  158. function Max(a, b: Extended): Extended;inline; overload;
  159. {$endif FPC_HAS_TYPE_EXTENDED}
  160. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline; overload;
  161. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline; overload;
  162. {$ifdef FPC_HAS_TYPE_DOUBLE}
  163. function InRange(const AValue, AMin, AMax: Double): Boolean;inline; overload;
  164. {$endif FPC_HAS_TYPE_DOUBLE}
  165. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline; overload;
  166. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline; overload;
  167. {$ifdef FPC_HAS_TYPE_DOUBLE}
  168. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline; overload;
  169. {$endif FPC_HAS_TYPE_DOUBLE}
  170. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  171. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  172. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  173. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  174. { Floating point modulo}
  175. {$ifdef FPC_HAS_TYPE_SINGLE}
  176. function FMod(const a, b: Single): Single;inline;overload;
  177. {$endif FPC_HAS_TYPE_SINGLE}
  178. {$ifdef FPC_HAS_TYPE_DOUBLE}
  179. function FMod(const a, b: Double): Double;inline;overload;
  180. {$endif FPC_HAS_TYPE_DOUBLE}
  181. {$ifdef FPC_HAS_TYPE_EXTENDED}
  182. function FMod(const a, b: Extended): Extended;inline;overload;
  183. {$endif FPC_HAS_TYPE_EXTENDED}
  184. operator mod(const a,b:float) c:float;inline;
  185. // Sign functions
  186. Type
  187. TValueSign = -1..1;
  188. const
  189. NegativeValue = Low(TValueSign);
  190. ZeroValue = 0;
  191. PositiveValue = High(TValueSign);
  192. function Sign(const AValue: Integer): TValueSign;inline; overload;
  193. function Sign(const AValue: Int64): TValueSign;inline; overload;
  194. {$ifdef FPC_HAS_TYPE_SINGLE}
  195. function Sign(const AValue: Single): TValueSign;inline; overload;
  196. {$endif}
  197. function Sign(const AValue: Double): TValueSign;inline; overload;
  198. {$ifdef FPC_HAS_TYPE_EXTENDED}
  199. function Sign(const AValue: Extended): TValueSign;inline; overload;
  200. {$endif}
  201. function IsZero(const A: Single; Epsilon: Single): Boolean; overload;
  202. function IsZero(const A: Single): Boolean;inline; overload;
  203. {$ifdef FPC_HAS_TYPE_DOUBLE}
  204. function IsZero(const A: Double; Epsilon: Double): Boolean; overload;
  205. function IsZero(const A: Double): Boolean;inline; overload;
  206. {$endif FPC_HAS_TYPE_DOUBLE}
  207. {$ifdef FPC_HAS_TYPE_EXTENDED}
  208. function IsZero(const A: Extended; Epsilon: Extended): Boolean; overload;
  209. function IsZero(const A: Extended): Boolean;inline; overload;
  210. {$endif FPC_HAS_TYPE_EXTENDED}
  211. function IsNan(const d : Single): Boolean; overload;
  212. {$ifdef FPC_HAS_TYPE_DOUBLE}
  213. function IsNan(const d : Double): Boolean; overload;
  214. {$endif FPC_HAS_TYPE_DOUBLE}
  215. {$ifdef FPC_HAS_TYPE_EXTENDED}
  216. function IsNan(const d : Extended): Boolean; overload;
  217. {$endif FPC_HAS_TYPE_EXTENDED}
  218. function IsInfinite(const d : Single): Boolean; overload;
  219. {$ifdef FPC_HAS_TYPE_DOUBLE}
  220. function IsInfinite(const d : Double): Boolean; overload;
  221. {$endif FPC_HAS_TYPE_DOUBLE}
  222. {$ifdef FPC_HAS_TYPE_EXTENDED}
  223. function IsInfinite(const d : Extended): Boolean; overload;
  224. {$endif FPC_HAS_TYPE_EXTENDED}
  225. {$ifdef FPC_HAS_TYPE_EXTENDED}
  226. function SameValue(const A, B: Extended): Boolean;inline; overload;
  227. {$endif}
  228. {$ifdef FPC_HAS_TYPE_DOUBLE}
  229. function SameValue(const A, B: Double): Boolean;inline; overload;
  230. {$endif}
  231. function SameValue(const A, B: Single): Boolean;inline; overload;
  232. {$ifdef FPC_HAS_TYPE_EXTENDED}
  233. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean; overload;
  234. {$endif}
  235. {$ifdef FPC_HAS_TYPE_DOUBLE}
  236. function SameValue(const A, B: Double; Epsilon: Double): Boolean; overload;
  237. {$endif}
  238. function SameValue(const A, B: Single; Epsilon: Single): Boolean; overload;
  239. type
  240. TRoundToRange = -37..37;
  241. {$ifdef FPC_HAS_TYPE_DOUBLE}
  242. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  243. {$endif}
  244. {$ifdef FPC_HAS_TYPE_EXTENDED}
  245. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  246. {$endif}
  247. {$ifdef FPC_HAS_TYPE_SINGLE}
  248. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  249. {$endif}
  250. {$ifdef FPC_HAS_TYPE_SINGLE}
  251. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  252. {$endif}
  253. {$ifdef FPC_HAS_TYPE_DOUBLE}
  254. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  255. {$endif}
  256. {$ifdef FPC_HAS_TYPE_EXTENDED}
  257. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  258. {$endif}
  259. { angle conversion }
  260. function DegToRad(deg : float) : float;inline;
  261. function RadToDeg(rad : float) : float;inline;
  262. function GradToRad(grad : float) : float;inline;
  263. function RadToGrad(rad : float) : float;inline;
  264. function DegToGrad(deg : float) : float;inline;
  265. function GradToDeg(grad : float) : float;inline;
  266. {$ifdef FPC_HAS_TYPE_SINGLE}
  267. function CycleToDeg(const Cycles: Single): Single;
  268. {$ENDIF}
  269. {$ifdef FPC_HAS_TYPE_DOUBLE}
  270. function CycleToDeg(const Cycles: Double): Double;
  271. {$ENDIF}
  272. {$ifdef FPC_HAS_TYPE_EXTENDED}
  273. function CycleToDeg(const Cycles: Extended): Extended;
  274. {$ENDIF}
  275. {$ifdef FPC_HAS_TYPE_SINGLE}
  276. function DegToCycle(const Degrees: Single): Single;
  277. {$ENDIF}
  278. {$ifdef FPC_HAS_TYPE_DOUBLE}
  279. function DegToCycle(const Degrees: Double): Double;
  280. {$ENDIF}
  281. {$ifdef FPC_HAS_TYPE_EXTENDED}
  282. function DegToCycle(const Degrees: Extended): Extended;
  283. {$ENDIF}
  284. {$ifdef FPC_HAS_TYPE_SINGLE}
  285. function CycleToGrad(const Cycles: Single): Single;
  286. {$ENDIF}
  287. {$ifdef FPC_HAS_TYPE_DOUBLE}
  288. function CycleToGrad(const Cycles: Double): Double;
  289. {$ENDIF}
  290. {$ifdef FPC_HAS_TYPE_EXTENDED}
  291. function CycleToGrad(const Cycles: Extended): Extended;
  292. {$ENDIF}
  293. {$ifdef FPC_HAS_TYPE_SINGLE}
  294. function GradToCycle(const Grads: Single): Single;
  295. {$ENDIF}
  296. {$ifdef FPC_HAS_TYPE_DOUBLE}
  297. function GradToCycle(const Grads: Double): Double;
  298. {$ENDIF}
  299. {$ifdef FPC_HAS_TYPE_EXTENDED}
  300. function GradToCycle(const Grads: Extended): Extended;
  301. {$ENDIF}
  302. {$ifdef FPC_HAS_TYPE_SINGLE}
  303. function CycleToRad(const Cycles: Single): Single;
  304. {$ENDIF}
  305. {$ifdef FPC_HAS_TYPE_DOUBLE}
  306. function CycleToRad(const Cycles: Double): Double;
  307. {$ENDIF}
  308. {$ifdef FPC_HAS_TYPE_EXTENDED}
  309. function CycleToRad(const Cycles: Extended): Extended;
  310. {$ENDIF}
  311. {$ifdef FPC_HAS_TYPE_SINGLE}
  312. function RadToCycle(const Rads: Single): Single;
  313. {$ENDIF}
  314. {$ifdef FPC_HAS_TYPE_DOUBLE}
  315. function RadToCycle(const Rads: Double): Double;
  316. {$ENDIF}
  317. {$ifdef FPC_HAS_TYPE_EXTENDED}
  318. function RadToCycle(const Rads: Extended): Extended;
  319. {$ENDIF}
  320. {$ifdef FPC_HAS_TYPE_SINGLE}
  321. Function DegNormalize(deg : single) : single; inline;
  322. {$ENDIF}
  323. {$ifdef FPC_HAS_TYPE_DOUBLE}
  324. Function DegNormalize(deg : double) : double; inline;
  325. {$ENDIF}
  326. {$ifdef FPC_HAS_TYPE_EXTENDED}
  327. Function DegNormalize(deg : extended) : extended; inline;
  328. {$ENDIF}
  329. { trigoniometric functions }
  330. function Tan(x : float) : float;
  331. function Cotan(x : float) : float;
  332. function Cot(x : float) : float; inline;
  333. {$ifdef FPC_HAS_TYPE_SINGLE}
  334. procedure SinCos(theta : single;out sinus,cosinus : single);
  335. {$endif}
  336. {$ifdef FPC_HAS_TYPE_DOUBLE}
  337. procedure SinCos(theta : double;out sinus,cosinus : double);
  338. {$endif}
  339. {$ifdef FPC_HAS_TYPE_EXTENDED}
  340. procedure SinCos(theta : extended;out sinus,cosinus : extended);
  341. {$endif}
  342. function Secant(x : float) : float; inline;
  343. function Cosecant(x : float) : float; inline;
  344. function Sec(x : float) : float; inline;
  345. function Csc(x : float) : float; inline;
  346. { inverse functions }
  347. function ArcCos(x : float) : float;
  348. function ArcSin(x : float) : float;
  349. { calculates arctan(y/x) and returns an angle in the correct quadrant }
  350. function ArcTan2(y,x : float) : float;
  351. { hyperbolic functions }
  352. function CosH(x : float) : float;
  353. function SinH(x : float) : float;
  354. function TanH(x : float) : float;
  355. {$ifdef FPC_HAS_TYPE_SINGLE}
  356. function SecH(const X: Single): Single;
  357. {$ENDIF}
  358. {$ifdef FPC_HAS_TYPE_DOUBLE}
  359. function SecH(const X: Double): Double;
  360. {$ENDIF}
  361. {$ifdef FPC_HAS_TYPE_EXTENDED}
  362. function SecH(const X: Extended): Extended;
  363. {$ENDIF}
  364. {$ifdef FPC_HAS_TYPE_SINGLE}
  365. function CscH(const X: Single): Single;
  366. {$ENDIF}
  367. {$ifdef FPC_HAS_TYPE_DOUBLE}
  368. function CscH(const X: Double): Double;
  369. {$ENDIF}
  370. {$ifdef FPC_HAS_TYPE_EXTENDED}
  371. function CscH(const X: Extended): Extended;
  372. {$ENDIF}
  373. {$ifdef FPC_HAS_TYPE_SINGLE}
  374. function CotH(const X: Single): Single;
  375. {$ENDIF}
  376. {$ifdef FPC_HAS_TYPE_DOUBLE}
  377. function CotH(const X: Double): Double;
  378. {$ENDIF}
  379. {$ifdef FPC_HAS_TYPE_EXTENDED}
  380. function CotH(const X: Extended): Extended;
  381. {$ENDIF}
  382. { area functions }
  383. { delphi names: }
  384. function ArcCosH(x : float) : float;inline;
  385. function ArcSinH(x : float) : float;inline;
  386. function ArcTanH(x : float) : float;inline;
  387. { IMHO the function should be called as follows (FK) }
  388. function ArCosH(x : float) : float;
  389. function ArSinH(x : float) : float;
  390. function ArTanH(x : float) : float;
  391. { triangle functions }
  392. { returns the length of the hypotenuse of a right triangle }
  393. { if x and y are the other sides }
  394. function Hypot(x,y : float) : float;
  395. { logarithm functions }
  396. function Log10(x : float) : float;
  397. function Log2(x : float) : float;
  398. function LogN(n,x : float) : float;
  399. { returns natural logarithm of x+1, accurate for x values near zero }
  400. function LnXP1(x : float) : float;
  401. { exponential functions }
  402. function Power(base,exponent : float) : float;
  403. { base^exponent }
  404. function IntPower(base : float;exponent : longint) : float;
  405. operator ** (base,exponent : float) e: float; inline;
  406. operator ** (base,exponent : int64) res: int64;
  407. { number converting }
  408. { rounds x towards positive infinity }
  409. function Ceil(x : float) : Integer;
  410. function Ceil64(x: float): Int64;
  411. { rounds x towards negative infinity }
  412. function Floor(x : float) : Integer;
  413. function Floor64(x: float): Int64;
  414. { misc. functions }
  415. {$ifdef FPC_HAS_TYPE_SINGLE}
  416. { splits x into mantissa and exponent (to base 2) }
  417. procedure Frexp(X: single; out Mantissa: single; out Exponent: integer);
  418. { returns x*(2^p) }
  419. function Ldexp(X: single; p: Integer) : single;
  420. {$endif}
  421. {$ifdef FPC_HAS_TYPE_DOUBLE}
  422. procedure Frexp(X: double; out Mantissa: double; out Exponent: integer);
  423. function Ldexp(X: double; p: Integer) : double;
  424. {$endif}
  425. {$ifdef FPC_HAS_TYPE_EXTENDED}
  426. procedure Frexp(X: extended; out Mantissa: extended; out Exponent: integer);
  427. function Ldexp(X: extended; p: Integer) : extended;
  428. {$endif}
  429. { statistical functions }
  430. {$ifdef FPC_HAS_TYPE_SINGLE}
  431. function Mean(const data : array of Single) : float;
  432. function Sum(const data : array of Single) : float;inline;
  433. function Mean(const data : PSingle; Const N : longint) : float;
  434. function Sum(const data : PSingle; Const N : Longint) : float;
  435. {$endif FPC_HAS_TYPE_SINGLE}
  436. {$ifdef FPC_HAS_TYPE_DOUBLE}
  437. function Mean(const data : array of double) : float;inline;
  438. function Sum(const data : array of double) : float;inline;
  439. function Mean(const data : PDouble; Const N : longint) : float;
  440. function Sum(const data : PDouble; Const N : Longint) : float;
  441. {$endif FPC_HAS_TYPE_DOUBLE}
  442. {$ifdef FPC_HAS_TYPE_EXTENDED}
  443. function Mean(const data : array of Extended) : float;
  444. function Sum(const data : array of Extended) : float;inline;
  445. function Mean(const data : PExtended; Const N : longint) : float;
  446. function Sum(const data : PExtended; Const N : Longint) : float;
  447. {$endif FPC_HAS_TYPE_EXTENDED}
  448. function SumInt(const data : PInt64;Const N : longint) : Int64;
  449. function SumInt(const data : array of Int64) : Int64;inline;
  450. function Mean(const data : PInt64; const N : Longint):Float;
  451. function Mean(const data: array of Int64):Float;
  452. function SumInt(const data : PInteger; Const N : longint) : Int64;
  453. function SumInt(const data : array of Integer) : Int64;inline;
  454. function Mean(const data : PInteger; const N : Longint):Float;
  455. function Mean(const data: array of Integer):Float;
  456. {$ifdef FPC_HAS_TYPE_SINGLE}
  457. function SumOfSquares(const data : array of Single) : float;inline;
  458. function SumOfSquares(const data : PSingle; Const N : Integer) : float;
  459. { calculates the sum and the sum of squares of data }
  460. procedure SumsAndSquares(const data : array of Single;
  461. var sum,sumofsquares : float);inline;
  462. procedure SumsAndSquares(const data : PSingle; Const N : Integer;
  463. var sum,sumofsquares : float);
  464. {$endif FPC_HAS_TYPE_SINGLE}
  465. {$ifdef FPC_HAS_TYPE_DOUBLE}
  466. function SumOfSquares(const data : array of double) : float;
  467. function SumOfSquares(const data : PDouble; Const N : Integer) : float;
  468. { calculates the sum and the sum of squares of data }
  469. procedure SumsAndSquares(const data : array of Double;
  470. var sum,sumofsquares : float);inline;
  471. procedure SumsAndSquares(const data : PDouble; Const N : Integer;
  472. var sum,sumofsquares : float);
  473. {$endif FPC_HAS_TYPE_DOUBLE}
  474. {$ifdef FPC_HAS_TYPE_EXTENDED}
  475. function SumOfSquares(const data : array of Extended) : float;inline;
  476. function SumOfSquares(const data : PExtended; Const N : Integer) : float;
  477. { calculates the sum and the sum of squares of data }
  478. procedure SumsAndSquares(const data : array of Extended;
  479. var sum,sumofsquares : float);inline;
  480. procedure SumsAndSquares(const data : PExtended; Const N : Integer;
  481. var sum,sumofsquares : float);
  482. {$endif FPC_HAS_TYPE_EXTENDED}
  483. {$ifdef FPC_HAS_TYPE_SINGLE}
  484. function MinValue(const data : array of Single) : Single;inline;
  485. function MinValue(const data : PSingle; Const N : Integer) : Single;
  486. function MaxValue(const data : array of Single) : Single;inline;
  487. function MaxValue(const data : PSingle; Const N : Integer) : Single;
  488. {$endif FPC_HAS_TYPE_SINGLE}
  489. {$ifdef FPC_HAS_TYPE_DOUBLE}
  490. function MinValue(const data : array of Double) : Double;inline;
  491. function MinValue(const data : PDouble; Const N : Integer) : Double;
  492. function MaxValue(const data : array of Double) : Double;inline;
  493. function MaxValue(const data : PDouble; Const N : Integer) : Double;
  494. {$endif FPC_HAS_TYPE_DOUBLE}
  495. {$ifdef FPC_HAS_TYPE_EXTENDED}
  496. function MinValue(const data : array of Extended) : Extended;inline;
  497. function MinValue(const data : PExtended; Const N : Integer) : Extended;
  498. function MaxValue(const data : array of Extended) : Extended;inline;
  499. function MaxValue(const data : PExtended; Const N : Integer) : Extended;
  500. {$endif FPC_HAS_TYPE_EXTENDED}
  501. function MinValue(const data : array of integer) : Integer;inline;
  502. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  503. function MaxValue(const data : array of integer) : Integer;inline;
  504. function MaxValue(const data : PInteger; Const N : Integer) : Integer;
  505. { returns random values with gaussian distribution }
  506. function RandG(mean,stddev : float) : float;
  507. function RandomRange(const aFrom, aTo: Integer): Integer;
  508. function RandomRange(const aFrom, aTo: Int64): Int64;
  509. {$ifdef FPC_HAS_TYPE_SINGLE}
  510. { calculates the standard deviation }
  511. function StdDev(const data : array of Single) : float;inline;
  512. function StdDev(const data : PSingle; Const N : Integer) : float;
  513. { calculates the mean and stddev }
  514. procedure MeanAndStdDev(const data : array of Single;
  515. var mean,stddev : float);inline;
  516. procedure MeanAndStdDev(const data : PSingle;
  517. Const N : Longint;var mean,stddev : float);
  518. function Variance(const data : array of Single) : float;inline;
  519. function TotalVariance(const data : array of Single) : float;inline;
  520. function Variance(const data : PSingle; Const N : Integer) : float;
  521. function TotalVariance(const data : PSingle; Const N : Integer) : float;
  522. { Population (aka uncorrected) variance and standard deviation }
  523. function PopnStdDev(const data : array of Single) : float;inline;
  524. function PopnStdDev(const data : PSingle; Const N : Integer) : float;
  525. function PopnVariance(const data : PSingle; Const N : Integer) : float;
  526. function PopnVariance(const data : array of Single) : float;inline;
  527. procedure MomentSkewKurtosis(const data : array of Single;
  528. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  529. procedure MomentSkewKurtosis(const data : PSingle; Const N : Integer;
  530. out m1,m2,m3,m4,skew,kurtosis : float);
  531. { geometrical function }
  532. { returns the euclidean L2 norm }
  533. function Norm(const data : array of Single) : float;inline;
  534. function Norm(const data : PSingle; Const N : Integer) : float;
  535. {$endif FPC_HAS_TYPE_SINGLE}
  536. {$ifdef FPC_HAS_TYPE_DOUBLE}
  537. { calculates the standard deviation }
  538. function StdDev(const data : array of Double) : float;inline;
  539. function StdDev(const data : PDouble; Const N : Integer) : float;
  540. { calculates the mean and stddev }
  541. procedure MeanAndStdDev(const data : array of Double;
  542. var mean,stddev : float);inline;
  543. procedure MeanAndStdDev(const data : PDouble;
  544. Const N : Longint;var mean,stddev : float);
  545. function Variance(const data : array of Double) : float;inline;
  546. function TotalVariance(const data : array of Double) : float;inline;
  547. function Variance(const data : PDouble; Const N : Integer) : float;
  548. function TotalVariance(const data : PDouble; Const N : Integer) : float;
  549. { Population (aka uncorrected) variance and standard deviation }
  550. function PopnStdDev(const data : array of Double) : float;inline;
  551. function PopnStdDev(const data : PDouble; Const N : Integer) : float;
  552. function PopnVariance(const data : PDouble; Const N : Integer) : float;
  553. function PopnVariance(const data : array of Double) : float;inline;
  554. procedure MomentSkewKurtosis(const data : array of Double;
  555. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  556. procedure MomentSkewKurtosis(const data : PDouble; Const N : Integer;
  557. out m1,m2,m3,m4,skew,kurtosis : float);
  558. { geometrical function }
  559. { returns the euclidean L2 norm }
  560. function Norm(const data : array of double) : float;inline;
  561. function Norm(const data : PDouble; Const N : Integer) : float;
  562. {$endif FPC_HAS_TYPE_DOUBLE}
  563. {$ifdef FPC_HAS_TYPE_EXTENDED}
  564. { calculates the standard deviation }
  565. function StdDev(const data : array of Extended) : float;inline;
  566. function StdDev(const data : PExtended; Const N : Integer) : float;
  567. { calculates the mean and stddev }
  568. procedure MeanAndStdDev(const data : array of Extended;
  569. var mean,stddev : float);inline;
  570. procedure MeanAndStdDev(const data : PExtended;
  571. Const N : Longint;var mean,stddev : float);
  572. function Variance(const data : array of Extended) : float;inline;
  573. function TotalVariance(const data : array of Extended) : float;inline;
  574. function Variance(const data : PExtended; Const N : Integer) : float;
  575. function TotalVariance(const data : PExtended; Const N : Integer) : float;
  576. { Population (aka uncorrected) variance and standard deviation }
  577. function PopnStdDev(const data : array of Extended) : float;inline;
  578. function PopnStdDev(const data : PExtended; Const N : Integer) : float;
  579. function PopnVariance(const data : PExtended; Const N : Integer) : float;
  580. function PopnVariance(const data : array of Extended) : float;inline;
  581. procedure MomentSkewKurtosis(const data : array of Extended;
  582. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  583. procedure MomentSkewKurtosis(const data : PExtended; Const N : Integer;
  584. out m1,m2,m3,m4,skew,kurtosis : float);
  585. { geometrical function }
  586. { returns the euclidean L2 norm }
  587. function Norm(const data : array of Extended) : float;inline;
  588. function Norm(const data : PExtended; Const N : Integer) : float;
  589. {$endif FPC_HAS_TYPE_EXTENDED}
  590. { Financial functions }
  591. function FutureValue(ARate: Float; NPeriods: Integer;
  592. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  593. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  594. APaymentTime: TPaymentTime): Float;
  595. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  596. APaymentTime: TPaymentTime): Float;
  597. function Payment(ARate: Float; NPeriods: Integer;
  598. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  599. function PresentValue(ARate: Float; NPeriods: Integer;
  600. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  601. { Misc functions }
  602. function IfThen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer; inline; overload;
  603. function IfThen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64; inline; overload;
  604. function IfThen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double; inline; overload;
  605. function CompareValue ( const A, B : Integer) : TValueRelationship; inline;
  606. function CompareValue ( const A, B : Int64) : TValueRelationship; inline;
  607. function CompareValue ( const A, B : QWord) : TValueRelationship; inline;
  608. {$ifdef FPC_HAS_TYPE_SINGLE}
  609. function CompareValue ( const A, B : Single; delta : Single = 0.0 ) : TValueRelationship; inline;
  610. {$endif}
  611. {$ifdef FPC_HAS_TYPE_DOUBLE}
  612. function CompareValue ( const A, B : Double; delta : Double = 0.0) : TValueRelationship; inline;
  613. {$endif}
  614. {$ifdef FPC_HAS_TYPE_EXTENDED}
  615. function CompareValue ( const A, B : Extended; delta : Extended = 0.0 ) : TValueRelationship; inline;
  616. {$endif}
  617. function RandomFrom(const AValues: array of Double): Double; overload;
  618. function RandomFrom(const AValues: array of Integer): Integer; overload;
  619. function RandomFrom(const AValues: array of Int64): Int64; overload;
  620. {$if FPC_FULLVERSION >=30101}
  621. generic function RandomFrom<T>(const AValues:array of T):T;
  622. {$endif}
  623. { cpu specific stuff }
  624. type
  625. TFPURoundingMode = system.TFPURoundingMode;
  626. TFPUPrecisionMode = system.TFPUPrecisionMode;
  627. TFPUException = system.TFPUException;
  628. TFPUExceptionMask = system.TFPUExceptionMask;
  629. function GetRoundMode: TFPURoundingMode;
  630. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  631. function GetPrecisionMode: TFPUPrecisionMode;
  632. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  633. function GetExceptionMask: TFPUExceptionMask;
  634. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  635. procedure ClearExceptions(RaisePending: Boolean =true);
  636. implementation
  637. function copysign(x,y: float): float; forward; { returns abs(x)*sign(y) }
  638. { include cpu specific stuff }
  639. {$i mathu.inc}
  640. ResourceString
  641. SMathError = 'Math Error : %s';
  642. SInvalidArgument = 'Invalid argument';
  643. Procedure DoMathError(Const S : String);
  644. begin
  645. Raise EMathError.CreateFmt(SMathError,[S]);
  646. end;
  647. Procedure InvalidArgument;
  648. begin
  649. Raise EInvalidArgument.Create(SInvalidArgument);
  650. end;
  651. function Sign(const AValue: Integer): TValueSign;inline;
  652. begin
  653. result:=TValueSign(
  654. SarLongint(AValue,sizeof(AValue)*8-1) or { gives -1 for negative values, 0 otherwise }
  655. (longint(-AValue) shr (sizeof(AValue)*8-1)) { gives 1 for positive values, 0 otherwise }
  656. );
  657. end;
  658. function Sign(const AValue: Int64): TValueSign;inline;
  659. begin
  660. {$ifdef cpu64}
  661. result:=TValueSign(
  662. SarInt64(AValue,sizeof(AValue)*8-1) or
  663. (-AValue shr (sizeof(AValue)*8-1))
  664. );
  665. {$else cpu64}
  666. If Avalue<0 then
  667. Result:=NegativeValue
  668. else If Avalue>0 then
  669. Result:=PositiveValue
  670. else
  671. Result:=ZeroValue;
  672. {$endif}
  673. end;
  674. {$ifdef FPC_HAS_TYPE_SINGLE}
  675. function Sign(const AValue: Single): TValueSign;inline;
  676. begin
  677. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  678. end;
  679. {$endif}
  680. function Sign(const AValue: Double): TValueSign;inline;
  681. begin
  682. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  683. end;
  684. {$ifdef FPC_HAS_TYPE_EXTENDED}
  685. function Sign(const AValue: Extended): TValueSign;inline;
  686. begin
  687. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  688. end;
  689. {$endif}
  690. function degtorad(deg : float) : float;inline;
  691. begin
  692. degtorad:=deg*(pi/180.0);
  693. end;
  694. function radtodeg(rad : float) : float;inline;
  695. begin
  696. radtodeg:=rad*(180.0/pi);
  697. end;
  698. function gradtorad(grad : float) : float;inline;
  699. begin
  700. gradtorad:=grad*(pi/200.0);
  701. end;
  702. function radtograd(rad : float) : float;inline;
  703. begin
  704. radtograd:=rad*(200.0/pi);
  705. end;
  706. function degtograd(deg : float) : float;inline;
  707. begin
  708. degtograd:=deg*(200.0/180.0);
  709. end;
  710. function gradtodeg(grad : float) : float;inline;
  711. begin
  712. gradtodeg:=grad*(180.0/200.0);
  713. end;
  714. {$ifdef FPC_HAS_TYPE_SINGLE}
  715. function CycleToDeg(const Cycles: Single): Single;
  716. begin
  717. CycleToDeg:=Cycles*360.0;
  718. end;
  719. {$ENDIF}
  720. {$ifdef FPC_HAS_TYPE_DOUBLE}
  721. function CycleToDeg(const Cycles: Double): Double;
  722. begin
  723. CycleToDeg:=Cycles*360.0;
  724. end;
  725. {$ENDIF}
  726. {$ifdef FPC_HAS_TYPE_EXTENDED}
  727. function CycleToDeg(const Cycles: Extended): Extended;
  728. begin
  729. CycleToDeg:=Cycles*360.0;
  730. end;
  731. {$ENDIF}
  732. {$ifdef FPC_HAS_TYPE_SINGLE}
  733. function DegToCycle(const Degrees: Single): Single;
  734. begin
  735. DegToCycle:=Degrees*(1/360.0);
  736. end;
  737. {$ENDIF}
  738. {$ifdef FPC_HAS_TYPE_DOUBLE}
  739. function DegToCycle(const Degrees: Double): Double;
  740. begin
  741. DegToCycle:=Degrees*(1/360.0);
  742. end;
  743. {$ENDIF}
  744. {$ifdef FPC_HAS_TYPE_EXTENDED}
  745. function DegToCycle(const Degrees: Extended): Extended;
  746. begin
  747. DegToCycle:=Degrees*(1/360.0);
  748. end;
  749. {$ENDIF}
  750. {$ifdef FPC_HAS_TYPE_SINGLE}
  751. function CycleToGrad(const Cycles: Single): Single;
  752. begin
  753. CycleToGrad:=Cycles*400.0;
  754. end;
  755. {$ENDIF}
  756. {$ifdef FPC_HAS_TYPE_DOUBLE}
  757. function CycleToGrad(const Cycles: Double): Double;
  758. begin
  759. CycleToGrad:=Cycles*400.0;
  760. end;
  761. {$ENDIF}
  762. {$ifdef FPC_HAS_TYPE_EXTENDED}
  763. function CycleToGrad(const Cycles: Extended): Extended;
  764. begin
  765. CycleToGrad:=Cycles*400.0;
  766. end;
  767. {$ENDIF}
  768. {$ifdef FPC_HAS_TYPE_SINGLE}
  769. function GradToCycle(const Grads: Single): Single;
  770. begin
  771. GradToCycle:=Grads*(1/400.0);
  772. end;
  773. {$ENDIF}
  774. {$ifdef FPC_HAS_TYPE_DOUBLE}
  775. function GradToCycle(const Grads: Double): Double;
  776. begin
  777. GradToCycle:=Grads*(1/400.0);
  778. end;
  779. {$ENDIF}
  780. {$ifdef FPC_HAS_TYPE_EXTENDED}
  781. function GradToCycle(const Grads: Extended): Extended;
  782. begin
  783. GradToCycle:=Grads*(1/400.0);
  784. end;
  785. {$ENDIF}
  786. {$ifdef FPC_HAS_TYPE_SINGLE}
  787. function CycleToRad(const Cycles: Single): Single;
  788. begin
  789. CycleToRad:=Cycles*2*pi;
  790. end;
  791. {$ENDIF}
  792. {$ifdef FPC_HAS_TYPE_DOUBLE}
  793. function CycleToRad(const Cycles: Double): Double;
  794. begin
  795. CycleToRad:=Cycles*2*pi;
  796. end;
  797. {$ENDIF}
  798. {$ifdef FPC_HAS_TYPE_EXTENDED}
  799. function CycleToRad(const Cycles: Extended): Extended;
  800. begin
  801. CycleToRad:=Cycles*2*pi;
  802. end;
  803. {$ENDIF}
  804. {$ifdef FPC_HAS_TYPE_SINGLE}
  805. function RadToCycle(const Rads: Single): Single;
  806. begin
  807. RadToCycle:=Rads*(1/(2*pi));
  808. end;
  809. {$ENDIF}
  810. {$ifdef FPC_HAS_TYPE_DOUBLE}
  811. function RadToCycle(const Rads: Double): Double;
  812. begin
  813. RadToCycle:=Rads*(1/(2*pi));
  814. end;
  815. {$ENDIF}
  816. {$ifdef FPC_HAS_TYPE_EXTENDED}
  817. function RadToCycle(const Rads: Extended): Extended;
  818. begin
  819. RadToCycle:=Rads*(1/(2*pi));
  820. end;
  821. {$ENDIF}
  822. {$ifdef FPC_HAS_TYPE_SINGLE}
  823. Function DegNormalize(deg : single) : single;
  824. begin
  825. Result:=Deg-Int(Deg/360)*360;
  826. If Result<0 then Result:=Result+360;
  827. end;
  828. {$ENDIF}
  829. {$ifdef FPC_HAS_TYPE_DOUBLE}
  830. Function DegNormalize(deg : double) : double; inline;
  831. begin
  832. Result:=Deg-Int(Deg/360)*360;
  833. If (Result<0) then Result:=Result+360;
  834. end;
  835. {$ENDIF}
  836. {$ifdef FPC_HAS_TYPE_EXTENDED}
  837. Function DegNormalize(deg : extended) : extended; inline;
  838. begin
  839. Result:=Deg-Int(Deg/360)*360;
  840. If Result<0 then Result:=Result+360;
  841. end;
  842. {$ENDIF}
  843. {$ifndef FPC_MATH_HAS_TAN}
  844. function tan(x : float) : float;
  845. var
  846. _sin,_cos : float;
  847. begin
  848. sincos(x,_sin,_cos);
  849. tan:=_sin/_cos;
  850. end;
  851. {$endif FPC_MATH_HAS_TAN}
  852. {$ifndef FPC_MATH_HAS_COTAN}
  853. function cotan(x : float) : float;
  854. var
  855. _sin,_cos : float;
  856. begin
  857. sincos(x,_sin,_cos);
  858. cotan:=_cos/_sin;
  859. end;
  860. {$endif FPC_MATH_HAS_COTAN}
  861. function cot(x : float) : float; inline;
  862. begin
  863. cot := cotan(x);
  864. end;
  865. {$ifndef FPC_MATH_HAS_SINCOS}
  866. {$ifdef FPC_HAS_TYPE_SINGLE}
  867. procedure sincos(theta : single;out sinus,cosinus : single);
  868. begin
  869. sinus:=sin(theta);
  870. cosinus:=cos(theta);
  871. end;
  872. {$endif}
  873. {$ifdef FPC_HAS_TYPE_DOUBLE}
  874. procedure sincos(theta : double;out sinus,cosinus : double);
  875. begin
  876. sinus:=sin(theta);
  877. cosinus:=cos(theta);
  878. end;
  879. {$endif}
  880. {$ifdef FPC_HAS_TYPE_EXTENDED}
  881. procedure sincos(theta : extended;out sinus,cosinus : extended);
  882. begin
  883. sinus:=sin(theta);
  884. cosinus:=cos(theta);
  885. end;
  886. {$endif}
  887. {$endif FPC_MATH_HAS_SINCOS}
  888. function secant(x : float) : float; inline;
  889. begin
  890. secant := 1 / cos(x);
  891. end;
  892. function cosecant(x : float) : float; inline;
  893. begin
  894. cosecant := 1 / sin(x);
  895. end;
  896. function sec(x : float) : float; inline;
  897. begin
  898. sec := secant(x);
  899. end;
  900. function csc(x : float) : float; inline;
  901. begin
  902. csc := cosecant(x);
  903. end;
  904. { arcsin and arccos functions from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  905. function arcsin(x : float) : float;
  906. begin
  907. arcsin:=arctan2(x,sqrt((1.0-x)*(1.0+x)));
  908. end;
  909. function Arccos(x : Float) : Float;
  910. begin
  911. if abs(x)=1.0 then
  912. if x<0.0 then
  913. arccos:=Pi
  914. else
  915. arccos:=0
  916. else
  917. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  918. end;
  919. {$ifndef FPC_MATH_HAS_ARCTAN2}
  920. function arctan2(y,x : float) : float;
  921. begin
  922. if x=0 then
  923. begin
  924. if y=0 then
  925. result:=0.0
  926. else if y>0 then
  927. result:=pi/2
  928. else
  929. result:=-pi/2;
  930. end
  931. else
  932. begin
  933. result:=ArcTan(y/x);
  934. if x<0 then
  935. if y<0 then
  936. result:=result-pi
  937. else
  938. result:=result+pi;
  939. end;
  940. end;
  941. {$endif FPC_MATH_HAS_ARCTAN2}
  942. function cosh(x : float) : float;
  943. var
  944. temp : float;
  945. begin
  946. temp:=exp(x);
  947. cosh:=0.5*(temp+1.0/temp);
  948. end;
  949. function sinh(x : float) : float;
  950. var
  951. temp : float;
  952. begin
  953. temp:=exp(x);
  954. { copysign ensures that sinh(-0.0)=-0.0 }
  955. sinh:=copysign(0.5*(temp-1.0/temp),x);
  956. end;
  957. function tanh(x : float) : float;
  958. var
  959. tmp:float;
  960. begin
  961. if x < 0 then begin
  962. tmp:=exp(2*x);
  963. result:=(tmp-1)/(1+tmp)
  964. end
  965. else begin
  966. tmp:=exp(-2*x);
  967. result:=(1-tmp)/(1+tmp)
  968. end;
  969. end;
  970. {$ifdef FPC_HAS_TYPE_SINGLE}
  971. function SecH(const X: Single): Single;
  972. var
  973. Ex: ValReal;
  974. begin
  975. //https://en.wikipedia.org/wiki/Hyperbolic_functions#Definitions
  976. //SecH = 2 / (e^X + e^-X)
  977. Ex:=Exp(X);
  978. SecH:=2/(Ex+1/Ex);
  979. end;
  980. {$ENDIF}
  981. {$ifdef FPC_HAS_TYPE_DOUBLE}
  982. function SecH(const X: Double): Double;
  983. var
  984. Ex: ValReal;
  985. begin
  986. Ex:=Exp(X);
  987. SecH:=2/(Ex+1/Ex);
  988. end;
  989. {$ENDIF}
  990. {$ifdef FPC_HAS_TYPE_EXTENDED}
  991. function SecH(const X: Extended): Extended;
  992. var
  993. Ex: Extended;
  994. begin
  995. Ex:=Exp(X);
  996. SecH:=2/(Ex+1/Ex);
  997. end;
  998. {$ENDIF}
  999. {$ifdef FPC_HAS_TYPE_SINGLE}
  1000. function CscH(const X: Single): Single;
  1001. var
  1002. Ex: ValReal;
  1003. begin
  1004. //CscH = 2 / (e^X - e^-X)
  1005. Ex:=Exp(X);
  1006. CscH:=2/(Ex-1/Ex);
  1007. end;
  1008. {$ENDIF}
  1009. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1010. function CscH(const X: Double): Double;
  1011. var
  1012. Ex: ValReal;
  1013. begin
  1014. Ex:=Exp(X);
  1015. CscH:=2/(Ex-1/Ex);
  1016. end;
  1017. {$ENDIF}
  1018. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1019. function CscH(const X: Extended): Extended;
  1020. var
  1021. Ex: Extended;
  1022. begin
  1023. Ex:=Exp(X);
  1024. CscH:=2/(Ex-1/Ex);
  1025. end;
  1026. {$ENDIF}
  1027. {$ifdef FPC_HAS_TYPE_SINGLE}
  1028. function CotH(const X: Single): Single;
  1029. var
  1030. Ex, Emx: ValReal;
  1031. begin
  1032. //CotH = (e^X + e^-X) / (e^X - e^-X)
  1033. Ex:=Exp(X);
  1034. Emx:=1/Ex;
  1035. CotH:=(Ex+Emx)/(Ex-Emx);
  1036. end;
  1037. {$ENDIF}
  1038. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1039. function CotH(const X: Double): Double;
  1040. var
  1041. Ex, Emx: ValReal;
  1042. begin
  1043. Ex:=Exp(X);
  1044. Emx:=1/Ex;
  1045. CotH:=(Ex+Emx)/(Ex-Emx);
  1046. end;
  1047. {$ENDIF}
  1048. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1049. function CotH(const X: Extended): Extended;
  1050. var
  1051. Ex, Emx: Extended;
  1052. begin
  1053. Ex:=Exp(X);
  1054. Emx:=1/Ex;
  1055. CotH:=(Ex+Emx)/(Ex-Emx);
  1056. end;
  1057. {$ENDIF}
  1058. function arccosh(x : float) : float; inline;
  1059. begin
  1060. arccosh:=arcosh(x);
  1061. end;
  1062. function arcsinh(x : float) : float;inline;
  1063. begin
  1064. arcsinh:=arsinh(x);
  1065. end;
  1066. function arctanh(x : float) : float;inline;
  1067. begin
  1068. arctanh:=artanh(x);
  1069. end;
  1070. function arcosh(x : float) : float;
  1071. begin
  1072. { Provides accuracy about 4*eps near 1.0 }
  1073. arcosh:=Ln(x+Sqrt((x-1.0)*(x+1.0)));
  1074. end;
  1075. function arsinh(x : float) : float;
  1076. var
  1077. z: float;
  1078. begin
  1079. z:=abs(x);
  1080. z:=Ln(z+Sqrt(1+z*z));
  1081. { copysign ensures that arsinh(-Inf)=-Inf and arsinh(-0.0)=-0.0 }
  1082. arsinh:=copysign(z,x);
  1083. end;
  1084. function artanh(x : float) : float;
  1085. begin
  1086. artanh:=(lnxp1(x)-lnxp1(-x))*0.5;
  1087. end;
  1088. { hypot function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  1089. function hypot(x,y : float) : float;
  1090. begin
  1091. x:=abs(x);
  1092. y:=abs(y);
  1093. if (x>y) then
  1094. hypot:=x*sqrt(1.0+sqr(y/x))
  1095. else if (x>0.0) then
  1096. hypot:=y*sqrt(1.0+sqr(x/y))
  1097. else
  1098. hypot:=y;
  1099. end;
  1100. function log10(x : float) : float;
  1101. begin
  1102. log10:=ln(x)*0.43429448190325182765; { 1/ln(10) }
  1103. end;
  1104. {$ifndef FPC_MATH_HAS_LOG2}
  1105. function log2(x : float) : float;
  1106. begin
  1107. log2:=ln(x)*1.4426950408889634079; { 1/ln(2) }
  1108. end;
  1109. {$endif FPC_MATH_HAS_LOG2}
  1110. function logn(n,x : float) : float;
  1111. begin
  1112. logn:=ln(x)/ln(n);
  1113. end;
  1114. { lnxp1 function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  1115. function lnxp1(x : float) : float;
  1116. var
  1117. y: float;
  1118. begin
  1119. if (x>=4.0) then
  1120. lnxp1:=ln(1.0+x)
  1121. else
  1122. begin
  1123. y:=1.0+x;
  1124. if (y=1.0) then
  1125. lnxp1:=x
  1126. else
  1127. begin
  1128. lnxp1:=ln(y); { lnxp1(-1) = ln(0) = -Inf }
  1129. if y>0.0 then
  1130. lnxp1:=lnxp1+(x-(y-1.0))/y;
  1131. end;
  1132. end;
  1133. end;
  1134. function power(base,exponent : float) : float;
  1135. begin
  1136. if Exponent=0.0 then
  1137. result:=1.0
  1138. else if (base=0.0) and (exponent>0.0) then
  1139. result:=0.0
  1140. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  1141. result:=intpower(base,trunc(exponent))
  1142. else
  1143. result:=exp(exponent * ln (base));
  1144. end;
  1145. function intpower(base : float;exponent : longint) : float;
  1146. begin
  1147. if exponent<0 then
  1148. begin
  1149. base:=1.0/base;
  1150. exponent:=-exponent;
  1151. end;
  1152. intpower:=1.0;
  1153. while exponent<>0 do
  1154. begin
  1155. if exponent and 1<>0 then
  1156. intpower:=intpower*base;
  1157. exponent:=exponent shr 1;
  1158. base:=sqr(base);
  1159. end;
  1160. end;
  1161. operator ** (base,exponent : float) e: float; inline;
  1162. begin
  1163. e:=power(base,exponent);
  1164. end;
  1165. operator ** (base,exponent : int64) res: int64;
  1166. begin
  1167. if exponent<0 then
  1168. begin
  1169. if base<=0 then
  1170. raise EInvalidArgument.Create('Non-positive base with negative exponent in **');
  1171. if base=1 then
  1172. res:=1
  1173. else
  1174. res:=0;
  1175. exit;
  1176. end;
  1177. res:=1;
  1178. while exponent<>0 do
  1179. begin
  1180. if exponent and 1<>0 then
  1181. res:=res*base;
  1182. exponent:=exponent shr 1;
  1183. base:=base*base;
  1184. end;
  1185. end;
  1186. function ceil(x : float) : integer;
  1187. begin
  1188. Result:=Trunc(x)+ord(Frac(x)>0);
  1189. end;
  1190. function ceil64(x: float): Int64;
  1191. begin
  1192. Result:=Trunc(x)+ord(Frac(x)>0);
  1193. end;
  1194. function floor(x : float) : integer;
  1195. begin
  1196. Result:=Trunc(x)-ord(Frac(x)<0);
  1197. end;
  1198. function floor64(x: float): Int64;
  1199. begin
  1200. Result:=Trunc(x)-ord(Frac(x)<0);
  1201. end;
  1202. // Correction for "rounding to nearest, ties to even".
  1203. // RoundToNearestTieToEven(QWE.RTYUIOP) = QWE + TieToEven(ER, TYUIOP <> 0).
  1204. function TieToEven(AB: cardinal; somethingAfter: boolean): cardinal;
  1205. begin
  1206. result := AB and 1;
  1207. if (result <> 0) and not somethingAfter then
  1208. result := AB shr 1;
  1209. end;
  1210. {$ifdef FPC_HAS_TYPE_SINGLE}
  1211. procedure Frexp(X: single; out Mantissa: single; out Exponent: integer);
  1212. var
  1213. M: uint32;
  1214. E, ExtraE: int32;
  1215. begin
  1216. Mantissa := X;
  1217. E := TSingleRec(X).Exp;
  1218. if (E > 0) and (E < 2 * TSingleRec.Bias + 1) then
  1219. begin
  1220. // Normal.
  1221. TSingleRec(Mantissa).Exp := TSingleRec.Bias - 1;
  1222. Exponent := E - (TSingleRec.Bias - 1);
  1223. exit;
  1224. end;
  1225. if E = 0 then
  1226. begin
  1227. M := TSingleRec(X).Frac;
  1228. if M <> 0 then
  1229. begin
  1230. // Subnormal.
  1231. ExtraE := 23 - BsrDWord(M);
  1232. TSingleRec(Mantissa).Frac := M shl ExtraE; // "and (1 shl 23 - 1)" required to remove starting 1, but .SetFrac already does it.
  1233. TSingleRec(Mantissa).Exp := TSingleRec.Bias - 1;
  1234. Exponent := -TSingleRec.Bias + 2 - ExtraE;
  1235. exit;
  1236. end;
  1237. end;
  1238. // ±0, ±Inf, NaN.
  1239. Exponent := 0;
  1240. end;
  1241. function Ldexp(X: single; p: integer): single;
  1242. var
  1243. M, E: uint32;
  1244. xp, sh: integer;
  1245. begin
  1246. E := TSingleRec(X).Exp;
  1247. if (E = 0) and (TSingleRec(X).Frac = 0) or (E = 2 * TSingleRec.Bias + 1) then
  1248. // ±0, ±Inf, NaN.
  1249. exit(X);
  1250. Frexp(X, result, xp);
  1251. inc(xp, p);
  1252. if (xp >= -TSingleRec.Bias + 2) and (xp <= TSingleRec.Bias + 1) then
  1253. // Normalized.
  1254. TSingleRec(result).Exp := xp + (TSingleRec.Bias - 1)
  1255. else if xp > TSingleRec.Bias + 1 then
  1256. begin
  1257. // Overflow.
  1258. TSingleRec(result).Exp := 2 * TSingleRec.Bias + 1;
  1259. TSingleRec(result).Frac := 0;
  1260. end else
  1261. begin
  1262. TSingleRec(result).Exp := 0;
  1263. if xp >= -TSingleRec.Bias + 2 - 23 then
  1264. begin
  1265. // Denormalized.
  1266. M := TSingleRec(result).Frac or uint32(1) shl 23;
  1267. sh := -TSingleRec.Bias + 1 - xp;
  1268. TSingleRec(result).Frac := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint32(1) shl sh - 1) <> 0);
  1269. end else
  1270. // Underflow.
  1271. TSingleRec(result).Frac := 0;
  1272. end;
  1273. end;
  1274. {$endif}
  1275. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1276. procedure Frexp(X: double; out Mantissa: double; out Exponent: integer);
  1277. var
  1278. M: uint64;
  1279. E, ExtraE: int32;
  1280. begin
  1281. Mantissa := X;
  1282. E := TDoubleRec(X).Exp;
  1283. if (E > 0) and (E < 2 * TDoubleRec.Bias + 1) then
  1284. begin
  1285. // Normal.
  1286. TDoubleRec(Mantissa).Exp := TDoubleRec.Bias - 1;
  1287. Exponent := E - (TDoubleRec.Bias - 1);
  1288. exit;
  1289. end;
  1290. if E = 0 then
  1291. begin
  1292. M := TDoubleRec(X).Frac;
  1293. if M <> 0 then
  1294. begin
  1295. // Subnormal.
  1296. ExtraE := 52 - BsrQWord(M);
  1297. TDoubleRec(Mantissa).Frac := M shl ExtraE; // "and (1 shl 52 - 1)" required to remove starting 1, but .SetFrac already does it.
  1298. TDoubleRec(Mantissa).Exp := TDoubleRec.Bias - 1;
  1299. Exponent := -TDoubleRec.Bias + 2 - ExtraE;
  1300. exit;
  1301. end;
  1302. end;
  1303. // ±0, ±Inf, NaN.
  1304. Exponent := 0;
  1305. end;
  1306. function Ldexp(X: double; p: integer): double;
  1307. var
  1308. M: uint64;
  1309. E: uint32;
  1310. xp, sh: integer;
  1311. begin
  1312. E := TDoubleRec(X).Exp;
  1313. if (E = 0) and (TDoubleRec(X).Frac = 0) or (E = 2 * TDoubleRec.Bias + 1) then
  1314. // ±0, ±Inf, NaN.
  1315. exit(X);
  1316. Frexp(X, result, xp);
  1317. inc(xp, p);
  1318. if (xp >= -TDoubleRec.Bias + 2) and (xp <= TDoubleRec.Bias + 1) then
  1319. // Normalized.
  1320. TDoubleRec(result).Exp := xp + (TDoubleRec.Bias - 1)
  1321. else if xp > TDoubleRec.Bias + 1 then
  1322. begin
  1323. // Overflow.
  1324. TDoubleRec(result).Exp := 2 * TDoubleRec.Bias + 1;
  1325. TDoubleRec(result).Frac := 0;
  1326. end else
  1327. begin
  1328. TDoubleRec(result).Exp := 0;
  1329. if xp >= -TDoubleRec.Bias + 2 - 52 then
  1330. begin
  1331. // Denormalized.
  1332. M := TDoubleRec(result).Frac or uint64(1) shl 52;
  1333. sh := -TSingleRec.Bias + 1 - xp;
  1334. TDoubleRec(result).Frac := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint64(1) shl sh - 1) <> 0);
  1335. end else
  1336. // Underflow.
  1337. TDoubleRec(result).Frac := 0;
  1338. end;
  1339. end;
  1340. {$endif}
  1341. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1342. procedure Frexp(X: extended; out Mantissa: extended; out Exponent: integer);
  1343. var
  1344. M: uint64;
  1345. E, ExtraE: int32;
  1346. begin
  1347. Mantissa := X;
  1348. E := TExtended80Rec(X).Exp;
  1349. if (E > 0) and (E < 2 * TExtended80Rec.Bias + 1) then
  1350. begin
  1351. // Normal.
  1352. TExtended80Rec(Mantissa).Exp := TExtended80Rec.Bias - 1;
  1353. Exponent := E - (TExtended80Rec.Bias - 1);
  1354. exit;
  1355. end;
  1356. if E = 0 then
  1357. begin
  1358. M := TExtended80Rec(X).Frac;
  1359. if M <> 0 then
  1360. begin
  1361. // Subnormal. Extended has explicit starting 1.
  1362. ExtraE := 63 - BsrQWord(M);
  1363. TExtended80Rec(Mantissa).Frac := M shl ExtraE;
  1364. TExtended80Rec(Mantissa).Exp := TExtended80Rec.Bias - 1;
  1365. Exponent := -TExtended80Rec.Bias + 2 - ExtraE;
  1366. exit;
  1367. end;
  1368. end;
  1369. // ±0, ±Inf, NaN.
  1370. Exponent := 0;
  1371. end;
  1372. function Ldexp(X: extended; p: integer): extended;
  1373. var
  1374. M: uint64;
  1375. E: uint32;
  1376. xp, sh: integer;
  1377. begin
  1378. E := TExtended80Rec(X).Exp;
  1379. if (E = 0) and (TExtended80Rec(X).Frac = 0) or (E = 2 * TExtended80Rec.Bias + 1) then
  1380. // ±0, ±Inf, NaN.
  1381. exit(X);
  1382. Frexp(X, result, xp);
  1383. inc(xp, p);
  1384. if (xp >= -TExtended80Rec.Bias + 2) and (xp <= TExtended80Rec.Bias + 1) then
  1385. // Normalized.
  1386. TExtended80Rec(result).Exp := xp + (TExtended80Rec.Bias - 1)
  1387. else if xp > TExtended80Rec.Bias + 1 then
  1388. begin
  1389. // Overflow.
  1390. TExtended80Rec(result).Exp := 2 * TExtended80Rec.Bias + 1;
  1391. TExtended80Rec(result).Frac := uint64(1) shl 63;
  1392. end
  1393. else if xp >= -TExtended80Rec.Bias + 2 - 63 then
  1394. begin
  1395. // Denormalized... usually.
  1396. // Mantissa of subnormal 'extended' (Exp = 0) must always start with 0.
  1397. // If the calculated mantissa starts with 1, extended instead becomes normalized with Exp = 1.
  1398. M := TExtended80Rec(result).Frac;
  1399. sh := -TExtended80Rec.Bias + 1 - xp;
  1400. M := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint64(1) shl sh - 1) <> 0);
  1401. TExtended80Rec(result).Exp := M shr 63;
  1402. TExtended80Rec(result).Frac := M;
  1403. end else
  1404. begin
  1405. // Underflow.
  1406. TExtended80Rec(result).Exp := 0;
  1407. TExtended80Rec(result).Frac := 0;
  1408. end;
  1409. end;
  1410. {$endif}
  1411. const
  1412. { Cutoff for https://en.wikipedia.org/wiki/Pairwise_summation; sums of at least this many elements are split in two halves. }
  1413. RecursiveSumThreshold=12;
  1414. {$ifdef FPC_HAS_TYPE_SINGLE}
  1415. function mean(const data : array of Single) : float;
  1416. begin
  1417. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  1418. end;
  1419. function mean(const data : PSingle; Const N : longint) : float;
  1420. begin
  1421. mean:=sum(Data,N);
  1422. mean:=mean/N;
  1423. end;
  1424. function sum(const data : array of Single) : float;inline;
  1425. begin
  1426. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  1427. end;
  1428. function sum(const data : PSingle;Const N : longint) : float;
  1429. var
  1430. i : SizeInt;
  1431. begin
  1432. if N>=RecursiveSumThreshold then
  1433. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1434. else
  1435. begin
  1436. result:=0;
  1437. for i:=0 to N-1 do
  1438. result:=result+data[i];
  1439. end;
  1440. end;
  1441. {$endif FPC_HAS_TYPE_SINGLE}
  1442. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1443. function mean(const data : array of Double) : float; inline;
  1444. begin
  1445. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  1446. end;
  1447. function mean(const data : PDouble; Const N : longint) : float;
  1448. begin
  1449. mean:=sum(Data,N);
  1450. mean:=mean/N;
  1451. end;
  1452. function sum(const data : array of Double) : float; inline;
  1453. begin
  1454. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  1455. end;
  1456. function sum(const data : PDouble;Const N : longint) : float;
  1457. var
  1458. i : SizeInt;
  1459. begin
  1460. if N>=RecursiveSumThreshold then
  1461. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1462. else
  1463. begin
  1464. result:=0;
  1465. for i:=0 to N-1 do
  1466. result:=result+data[i];
  1467. end;
  1468. end;
  1469. {$endif FPC_HAS_TYPE_DOUBLE}
  1470. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1471. function mean(const data : array of Extended) : float;
  1472. begin
  1473. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  1474. end;
  1475. function mean(const data : PExtended; Const N : longint) : float;
  1476. begin
  1477. mean:=sum(Data,N);
  1478. mean:=mean/N;
  1479. end;
  1480. function sum(const data : array of Extended) : float; inline;
  1481. begin
  1482. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  1483. end;
  1484. function sum(const data : PExtended;Const N : longint) : float;
  1485. var
  1486. i : SizeInt;
  1487. begin
  1488. if N>=RecursiveSumThreshold then
  1489. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1490. else
  1491. begin
  1492. result:=0;
  1493. for i:=0 to N-1 do
  1494. result:=result+data[i];
  1495. end;
  1496. end;
  1497. {$endif FPC_HAS_TYPE_EXTENDED}
  1498. function sumInt(const data : PInt64;Const N : longint) : Int64;
  1499. var
  1500. i : SizeInt;
  1501. begin
  1502. sumInt:=0;
  1503. for i:=0 to N-1 do
  1504. sumInt:=sumInt+data[i];
  1505. end;
  1506. function sumInt(const data : array of Int64) : Int64; inline;
  1507. begin
  1508. Result:=SumInt(PInt64(@Data[0]),High(Data)+1);
  1509. end;
  1510. function mean(const data : PInt64; const N : Longint):Float;
  1511. begin
  1512. mean:=sumInt(Data,N);
  1513. mean:=mean/N;
  1514. end;
  1515. function mean(const data: array of Int64):Float;
  1516. begin
  1517. mean:=mean(PInt64(@data[0]),High(Data)+1);
  1518. end;
  1519. function sumInt(const data : PInteger; Const N : longint) : Int64;
  1520. var
  1521. i : SizeInt;
  1522. begin
  1523. sumInt:=0;
  1524. for i:=0 to N-1 do
  1525. sumInt:=sumInt+data[i];
  1526. end;
  1527. function sumInt(const data : array of Integer) : Int64;inline;
  1528. begin
  1529. Result:=sumInt(PInteger(@Data[0]),High(Data)+1);
  1530. end;
  1531. function mean(const data : PInteger; const N : Longint):Float;
  1532. begin
  1533. mean:=sumInt(Data,N);
  1534. mean:=mean/N;
  1535. end;
  1536. function mean(const data: array of Integer):Float;
  1537. begin
  1538. mean:=mean(PInteger(@data[0]),High(Data)+1);
  1539. end;
  1540. {$ifdef FPC_HAS_TYPE_SINGLE}
  1541. function sumofsquares(const data : array of Single) : float; inline;
  1542. begin
  1543. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  1544. end;
  1545. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  1546. var
  1547. i : SizeInt;
  1548. begin
  1549. if N>=RecursiveSumThreshold then
  1550. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1551. else
  1552. begin
  1553. result:=0;
  1554. for i:=0 to N-1 do
  1555. result:=result+sqr(data[i]);
  1556. end;
  1557. end;
  1558. procedure sumsandsquares(const data : array of Single;
  1559. var sum,sumofsquares : float); inline;
  1560. begin
  1561. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1562. end;
  1563. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  1564. var sum,sumofsquares : float);
  1565. var
  1566. i : SizeInt;
  1567. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  1568. begin
  1569. if N>=RecursiveSumThreshold then
  1570. begin
  1571. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  1572. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  1573. sum:=sum0+sum1;
  1574. sumofsquares:=sumofsquares0+sumofsquares1;
  1575. end
  1576. else
  1577. begin
  1578. tsum:=0;
  1579. tsumofsquares:=0;
  1580. for i:=0 to N-1 do
  1581. begin
  1582. temp:=data[i];
  1583. tsum:=tsum+temp;
  1584. tsumofsquares:=tsumofsquares+sqr(temp);
  1585. end;
  1586. sum:=tsum;
  1587. sumofsquares:=tsumofsquares;
  1588. end;
  1589. end;
  1590. {$endif FPC_HAS_TYPE_SINGLE}
  1591. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1592. function sumofsquares(const data : array of Double) : float; inline;
  1593. begin
  1594. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  1595. end;
  1596. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  1597. var
  1598. i : SizeInt;
  1599. begin
  1600. if N>=RecursiveSumThreshold then
  1601. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1602. else
  1603. begin
  1604. result:=0;
  1605. for i:=0 to N-1 do
  1606. result:=result+sqr(data[i]);
  1607. end;
  1608. end;
  1609. procedure sumsandsquares(const data : array of Double;
  1610. var sum,sumofsquares : float);
  1611. begin
  1612. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1613. end;
  1614. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  1615. var sum,sumofsquares : float);
  1616. var
  1617. i : SizeInt;
  1618. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  1619. begin
  1620. if N>=RecursiveSumThreshold then
  1621. begin
  1622. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  1623. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  1624. sum:=sum0+sum1;
  1625. sumofsquares:=sumofsquares0+sumofsquares1;
  1626. end
  1627. else
  1628. begin
  1629. tsum:=0;
  1630. tsumofsquares:=0;
  1631. for i:=0 to N-1 do
  1632. begin
  1633. temp:=data[i];
  1634. tsum:=tsum+temp;
  1635. tsumofsquares:=tsumofsquares+sqr(temp);
  1636. end;
  1637. sum:=tsum;
  1638. sumofsquares:=tsumofsquares;
  1639. end;
  1640. end;
  1641. {$endif FPC_HAS_TYPE_DOUBLE}
  1642. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1643. function sumofsquares(const data : array of Extended) : float; inline;
  1644. begin
  1645. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  1646. end;
  1647. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  1648. var
  1649. i : SizeInt;
  1650. begin
  1651. if N>=RecursiveSumThreshold then
  1652. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1653. else
  1654. begin
  1655. result:=0;
  1656. for i:=0 to N-1 do
  1657. result:=result+sqr(data[i]);
  1658. end;
  1659. end;
  1660. procedure sumsandsquares(const data : array of Extended;
  1661. var sum,sumofsquares : float); inline;
  1662. begin
  1663. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1664. end;
  1665. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  1666. var sum,sumofsquares : float);
  1667. var
  1668. i : SizeInt;
  1669. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  1670. begin
  1671. if N>=RecursiveSumThreshold then
  1672. begin
  1673. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  1674. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  1675. sum:=sum0+sum1;
  1676. sumofsquares:=sumofsquares0+sumofsquares1;
  1677. end
  1678. else
  1679. begin
  1680. tsum:=0;
  1681. tsumofsquares:=0;
  1682. for i:=0 to N-1 do
  1683. begin
  1684. temp:=data[i];
  1685. tsum:=tsum+temp;
  1686. tsumofsquares:=tsumofsquares+sqr(temp);
  1687. end;
  1688. sum:=tsum;
  1689. sumofsquares:=tsumofsquares;
  1690. end;
  1691. end;
  1692. {$endif FPC_HAS_TYPE_EXTENDED}
  1693. function randg(mean,stddev : float) : float;
  1694. Var U1,S2 : Float;
  1695. begin
  1696. repeat
  1697. u1:= 2*random-1;
  1698. S2:=Sqr(U1)+sqr(2*random-1);
  1699. until s2<1;
  1700. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  1701. end;
  1702. function RandomRange(const aFrom, aTo: Integer): Integer;
  1703. begin
  1704. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1705. end;
  1706. function RandomRange(const aFrom, aTo: Int64): Int64;
  1707. begin
  1708. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1709. end;
  1710. {$ifdef FPC_HAS_TYPE_SINGLE}
  1711. procedure MeanAndTotalVariance
  1712. (const data: PSingle; N: LongInt; var mu, variance: float);
  1713. function CalcVariance(data: PSingle; N: SizeInt; mu: float): float;
  1714. var
  1715. i: SizeInt;
  1716. begin
  1717. if N>=RecursiveSumThreshold then
  1718. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  1719. else
  1720. begin
  1721. result:=0;
  1722. for i:=0 to N-1 do
  1723. result:=result+Sqr(data[i]-mu);
  1724. end;
  1725. end;
  1726. begin
  1727. mu := Mean( data, N );
  1728. variance := CalcVariance( data, N, mu );
  1729. end;
  1730. function stddev(const data : array of Single) : float; inline;
  1731. begin
  1732. Result:=Stddev(PSingle(@Data[0]),High(Data)+1);
  1733. end;
  1734. function stddev(const data : PSingle; Const N : Integer) : float;
  1735. begin
  1736. StdDev:=Sqrt(Variance(Data,N));
  1737. end;
  1738. procedure meanandstddev(const data : array of Single;
  1739. var mean,stddev : float); inline;
  1740. begin
  1741. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  1742. end;
  1743. procedure meanandstddev
  1744. ( const data: PSingle;
  1745. const N: Longint;
  1746. var mean,
  1747. stdDev: Float
  1748. );
  1749. var totalVariance: float;
  1750. begin
  1751. MeanAndTotalVariance( data, N, mean, totalVariance );
  1752. if N < 2 then stdDev := 0
  1753. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1754. end;
  1755. function variance(const data : array of Single) : float; inline;
  1756. begin
  1757. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  1758. end;
  1759. function variance(const data : PSingle; Const N : Integer) : float;
  1760. begin
  1761. If N=1 then
  1762. Result:=0
  1763. else
  1764. Result:=TotalVariance(Data,N)/(N-1);
  1765. end;
  1766. function totalvariance(const data : array of Single) : float; inline;
  1767. begin
  1768. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  1769. end;
  1770. function totalvariance(const data : PSingle; const N : Integer) : float;
  1771. var mu: float;
  1772. begin
  1773. MeanAndTotalVariance( data, N, mu, result );
  1774. end;
  1775. function popnstddev(const data : array of Single) : float;
  1776. begin
  1777. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  1778. end;
  1779. function popnstddev(const data : PSingle; Const N : Integer) : float;
  1780. begin
  1781. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1782. end;
  1783. function popnvariance(const data : array of Single) : float; inline;
  1784. begin
  1785. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  1786. end;
  1787. function popnvariance(const data : PSingle; Const N : Integer) : float;
  1788. begin
  1789. PopnVariance:=TotalVariance(Data,N)/N;
  1790. end;
  1791. procedure momentskewkurtosis(const data : array of single;
  1792. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1793. begin
  1794. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1795. end;
  1796. type
  1797. TMoments2to4 = array[2 .. 4] of float;
  1798. procedure momentskewkurtosis(
  1799. const data: pSingle;
  1800. Const N: integer;
  1801. out m1: float;
  1802. out m2: float;
  1803. out m3: float;
  1804. out m4: float;
  1805. out skew: float;
  1806. out kurtosis: float
  1807. );
  1808. procedure CalcDevSums2to4(data: PSingle; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  1809. var
  1810. tm2, tm3, tm4, dev, dev2: float;
  1811. i: SizeInt;
  1812. m2to4Part0, m2to4Part1: TMoments2to4;
  1813. begin
  1814. if N >= RecursiveSumThreshold then
  1815. begin
  1816. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  1817. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  1818. for i := Low(TMoments2to4) to High(TMoments2to4) do
  1819. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  1820. end
  1821. else
  1822. begin
  1823. tm2 := 0;
  1824. tm3 := 0;
  1825. tm4 := 0;
  1826. for i := 0 to N - 1 do
  1827. begin
  1828. dev := data[i] - m1;
  1829. dev2 := sqr(dev);
  1830. tm2 := tm2 + dev2;
  1831. tm3 := tm3 + dev2 * dev;
  1832. tm4 := tm4 + sqr(dev2);
  1833. end;
  1834. m2to4[2] := tm2;
  1835. m2to4[3] := tm3;
  1836. m2to4[4] := tm4;
  1837. end;
  1838. end;
  1839. var
  1840. reciprocalN: float;
  1841. m2to4: TMoments2to4;
  1842. begin
  1843. m1 := 0;
  1844. reciprocalN := 1/N;
  1845. m1 := reciprocalN * sum(data, N);
  1846. CalcDevSums2to4(data, N, m1, m2to4);
  1847. m2 := reciprocalN * m2to4[2];
  1848. m3 := reciprocalN * m2to4[3];
  1849. m4 := reciprocalN * m2to4[4];
  1850. skew := m3 / (sqrt(m2)*m2);
  1851. kurtosis := m4 / (m2 * m2);
  1852. end;
  1853. function norm(const data : array of Single) : float; inline;
  1854. begin
  1855. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  1856. end;
  1857. function norm(const data : PSingle; Const N : Integer) : float;
  1858. begin
  1859. norm:=sqrt(sumofsquares(data,N));
  1860. end;
  1861. {$endif FPC_HAS_TYPE_SINGLE}
  1862. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1863. procedure MeanAndTotalVariance
  1864. (const data: PDouble; N: LongInt; var mu, variance: float);
  1865. function CalcVariance(data: PDouble; N: SizeInt; mu: float): float;
  1866. var
  1867. i: SizeInt;
  1868. begin
  1869. if N>=RecursiveSumThreshold then
  1870. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  1871. else
  1872. begin
  1873. result:=0;
  1874. for i:=0 to N-1 do
  1875. result:=result+Sqr(data[i]-mu);
  1876. end;
  1877. end;
  1878. begin
  1879. mu := Mean( data, N );
  1880. variance := CalcVariance( data, N, mu );
  1881. end;
  1882. function stddev(const data : array of Double) : float; inline;
  1883. begin
  1884. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  1885. end;
  1886. function stddev(const data : PDouble; Const N : Integer) : float;
  1887. begin
  1888. StdDev:=Sqrt(Variance(Data,N));
  1889. end;
  1890. procedure meanandstddev(const data : array of Double;
  1891. var mean,stddev : float);
  1892. begin
  1893. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  1894. end;
  1895. procedure meanandstddev
  1896. ( const data: PDouble;
  1897. const N: Longint;
  1898. var mean,
  1899. stdDev: Float
  1900. );
  1901. var totalVariance: float;
  1902. begin
  1903. MeanAndTotalVariance( data, N, mean, totalVariance );
  1904. if N < 2 then stdDev := 0
  1905. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1906. end;
  1907. function variance(const data : array of Double) : float; inline;
  1908. begin
  1909. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  1910. end;
  1911. function variance(const data : PDouble; Const N : Integer) : float;
  1912. begin
  1913. If N=1 then
  1914. Result:=0
  1915. else
  1916. Result:=TotalVariance(Data,N)/(N-1);
  1917. end;
  1918. function totalvariance(const data : array of Double) : float; inline;
  1919. begin
  1920. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  1921. end;
  1922. function totalvariance(const data : PDouble; const N : Integer) : float;
  1923. var mu: float;
  1924. begin
  1925. MeanAndTotalVariance( data, N, mu, result );
  1926. end;
  1927. function popnstddev(const data : array of Double) : float;
  1928. begin
  1929. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  1930. end;
  1931. function popnstddev(const data : PDouble; Const N : Integer) : float;
  1932. begin
  1933. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1934. end;
  1935. function popnvariance(const data : array of Double) : float; inline;
  1936. begin
  1937. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  1938. end;
  1939. function popnvariance(const data : PDouble; Const N : Integer) : float;
  1940. begin
  1941. PopnVariance:=TotalVariance(Data,N)/N;
  1942. end;
  1943. procedure momentskewkurtosis(const data : array of Double;
  1944. out m1,m2,m3,m4,skew,kurtosis : float);
  1945. begin
  1946. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1947. end;
  1948. procedure momentskewkurtosis(
  1949. const data: pdouble;
  1950. Const N: integer;
  1951. out m1: float;
  1952. out m2: float;
  1953. out m3: float;
  1954. out m4: float;
  1955. out skew: float;
  1956. out kurtosis: float
  1957. );
  1958. procedure CalcDevSums2to4(data: PDouble; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  1959. var
  1960. tm2, tm3, tm4, dev, dev2: float;
  1961. i: SizeInt;
  1962. m2to4Part0, m2to4Part1: TMoments2to4;
  1963. begin
  1964. if N >= RecursiveSumThreshold then
  1965. begin
  1966. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  1967. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  1968. for i := Low(TMoments2to4) to High(TMoments2to4) do
  1969. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  1970. end
  1971. else
  1972. begin
  1973. tm2 := 0;
  1974. tm3 := 0;
  1975. tm4 := 0;
  1976. for i := 0 to N - 1 do
  1977. begin
  1978. dev := data[i] - m1;
  1979. dev2 := sqr(dev);
  1980. tm2 := tm2 + dev2;
  1981. tm3 := tm3 + dev2 * dev;
  1982. tm4 := tm4 + sqr(dev2);
  1983. end;
  1984. m2to4[2] := tm2;
  1985. m2to4[3] := tm3;
  1986. m2to4[4] := tm4;
  1987. end;
  1988. end;
  1989. var
  1990. reciprocalN: float;
  1991. m2to4: TMoments2to4;
  1992. begin
  1993. m1 := 0;
  1994. reciprocalN := 1/N;
  1995. m1 := reciprocalN * sum(data, N);
  1996. CalcDevSums2to4(data, N, m1, m2to4);
  1997. m2 := reciprocalN * m2to4[2];
  1998. m3 := reciprocalN * m2to4[3];
  1999. m4 := reciprocalN * m2to4[4];
  2000. skew := m3 / (sqrt(m2)*m2);
  2001. kurtosis := m4 / (m2 * m2);
  2002. end;
  2003. function norm(const data : array of Double) : float; inline;
  2004. begin
  2005. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  2006. end;
  2007. function norm(const data : PDouble; Const N : Integer) : float;
  2008. begin
  2009. norm:=sqrt(sumofsquares(data,N));
  2010. end;
  2011. {$endif FPC_HAS_TYPE_DOUBLE}
  2012. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2013. procedure MeanAndTotalVariance
  2014. (const data: PExtended; N: LongInt; var mu, variance: float);
  2015. function CalcVariance(data: PExtended; N: SizeInt; mu: float): float;
  2016. var
  2017. i: SizeInt;
  2018. begin
  2019. if N>=RecursiveSumThreshold then
  2020. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  2021. else
  2022. begin
  2023. result:=0;
  2024. for i:=0 to N-1 do
  2025. result:=result+Sqr(data[i]-mu);
  2026. end;
  2027. end;
  2028. begin
  2029. mu := Mean( data, N );
  2030. variance := CalcVariance( data, N, mu );
  2031. end;
  2032. function stddev(const data : array of Extended) : float; inline;
  2033. begin
  2034. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  2035. end;
  2036. function stddev(const data : PExtended; Const N : Integer) : float;
  2037. begin
  2038. StdDev:=Sqrt(Variance(Data,N));
  2039. end;
  2040. procedure meanandstddev(const data : array of Extended;
  2041. var mean,stddev : float); inline;
  2042. begin
  2043. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  2044. end;
  2045. procedure meanandstddev
  2046. ( const data: PExtended;
  2047. const N: Longint;
  2048. var mean,
  2049. stdDev: Float
  2050. );
  2051. var totalVariance: float;
  2052. begin
  2053. MeanAndTotalVariance( data, N, mean, totalVariance );
  2054. if N < 2 then stdDev := 0
  2055. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  2056. end;
  2057. function variance(const data : array of Extended) : float; inline;
  2058. begin
  2059. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  2060. end;
  2061. function variance(const data : PExtended; Const N : Integer) : float;
  2062. begin
  2063. If N=1 then
  2064. Result:=0
  2065. else
  2066. Result:=TotalVariance(Data,N)/(N-1);
  2067. end;
  2068. function totalvariance(const data : array of Extended) : float; inline;
  2069. begin
  2070. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  2071. end;
  2072. function totalvariance(const data : PExtended;Const N : Integer) : float;
  2073. var mu: float;
  2074. begin
  2075. MeanAndTotalVariance( data, N, mu, result );
  2076. end;
  2077. function popnstddev(const data : array of Extended) : float;
  2078. begin
  2079. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  2080. end;
  2081. function popnstddev(const data : PExtended; Const N : Integer) : float;
  2082. begin
  2083. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  2084. end;
  2085. function popnvariance(const data : array of Extended) : float; inline;
  2086. begin
  2087. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  2088. end;
  2089. function popnvariance(const data : PExtended; Const N : Integer) : float;
  2090. begin
  2091. PopnVariance:=TotalVariance(Data,N)/N;
  2092. end;
  2093. procedure momentskewkurtosis(const data : array of Extended;
  2094. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  2095. begin
  2096. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  2097. end;
  2098. procedure momentskewkurtosis(
  2099. const data: pExtended;
  2100. Const N: Integer;
  2101. out m1: float;
  2102. out m2: float;
  2103. out m3: float;
  2104. out m4: float;
  2105. out skew: float;
  2106. out kurtosis: float
  2107. );
  2108. procedure CalcDevSums2to4(data: PExtended; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  2109. var
  2110. tm2, tm3, tm4, dev, dev2: float;
  2111. i: SizeInt;
  2112. m2to4Part0, m2to4Part1: TMoments2to4;
  2113. begin
  2114. if N >= RecursiveSumThreshold then
  2115. begin
  2116. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  2117. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  2118. for i := Low(TMoments2to4) to High(TMoments2to4) do
  2119. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  2120. end
  2121. else
  2122. begin
  2123. tm2 := 0;
  2124. tm3 := 0;
  2125. tm4 := 0;
  2126. for i := 0 to N - 1 do
  2127. begin
  2128. dev := data[i] - m1;
  2129. dev2 := sqr(dev);
  2130. tm2 := tm2 + dev2;
  2131. tm3 := tm3 + dev2 * dev;
  2132. tm4 := tm4 + sqr(dev2);
  2133. end;
  2134. m2to4[2] := tm2;
  2135. m2to4[3] := tm3;
  2136. m2to4[4] := tm4;
  2137. end;
  2138. end;
  2139. var
  2140. reciprocalN: float;
  2141. m2to4: TMoments2to4;
  2142. begin
  2143. m1 := 0;
  2144. reciprocalN := 1/N;
  2145. m1 := reciprocalN * sum(data, N);
  2146. CalcDevSums2to4(data, N, m1, m2to4);
  2147. m2 := reciprocalN * m2to4[2];
  2148. m3 := reciprocalN * m2to4[3];
  2149. m4 := reciprocalN * m2to4[4];
  2150. skew := m3 / (sqrt(m2)*m2);
  2151. kurtosis := m4 / (m2 * m2);
  2152. end;
  2153. function norm(const data : array of Extended) : float; inline;
  2154. begin
  2155. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  2156. end;
  2157. function norm(const data : PExtended; Const N : Integer) : float;
  2158. begin
  2159. norm:=sqrt(sumofsquares(data,N));
  2160. end;
  2161. {$endif FPC_HAS_TYPE_EXTENDED}
  2162. function MinIntValue(const Data: array of Integer): Integer;
  2163. var
  2164. I: SizeInt;
  2165. begin
  2166. Result := Data[Low(Data)];
  2167. For I := Succ(Low(Data)) To High(Data) Do
  2168. If Data[I] < Result Then Result := Data[I];
  2169. end;
  2170. function MaxIntValue(const Data: array of Integer): Integer;
  2171. var
  2172. I: SizeInt;
  2173. begin
  2174. Result := Data[Low(Data)];
  2175. For I := Succ(Low(Data)) To High(Data) Do
  2176. If Data[I] > Result Then Result := Data[I];
  2177. end;
  2178. function MinValue(const Data: array of Integer): Integer; inline;
  2179. begin
  2180. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  2181. end;
  2182. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  2183. var
  2184. I: SizeInt;
  2185. begin
  2186. Result := Data[0];
  2187. For I := 1 To N-1 do
  2188. If Data[I] < Result Then Result := Data[I];
  2189. end;
  2190. function MaxValue(const Data: array of Integer): Integer; inline;
  2191. begin
  2192. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  2193. end;
  2194. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  2195. var
  2196. i : SizeInt;
  2197. begin
  2198. { get an initial value }
  2199. maxvalue:=data[0];
  2200. for i:=1 to N-1 do
  2201. if data[i]>maxvalue then
  2202. maxvalue:=data[i];
  2203. end;
  2204. {$ifdef FPC_HAS_TYPE_SINGLE}
  2205. function minvalue(const data : array of Single) : Single; inline;
  2206. begin
  2207. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  2208. end;
  2209. function minvalue(const data : PSingle; Const N : Integer) : Single;
  2210. var
  2211. i : SizeInt;
  2212. begin
  2213. { get an initial value }
  2214. minvalue:=data[0];
  2215. for i:=1 to N-1 do
  2216. if data[i]<minvalue then
  2217. minvalue:=data[i];
  2218. end;
  2219. function maxvalue(const data : array of Single) : Single; inline;
  2220. begin
  2221. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  2222. end;
  2223. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  2224. var
  2225. i : SizeInt;
  2226. begin
  2227. { get an initial value }
  2228. maxvalue:=data[0];
  2229. for i:=1 to N-1 do
  2230. if data[i]>maxvalue then
  2231. maxvalue:=data[i];
  2232. end;
  2233. {$endif FPC_HAS_TYPE_SINGLE}
  2234. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2235. function minvalue(const data : array of Double) : Double; inline;
  2236. begin
  2237. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  2238. end;
  2239. function minvalue(const data : PDouble; Const N : Integer) : Double;
  2240. var
  2241. i : SizeInt;
  2242. begin
  2243. { get an initial value }
  2244. minvalue:=data[0];
  2245. for i:=1 to N-1 do
  2246. if data[i]<minvalue then
  2247. minvalue:=data[i];
  2248. end;
  2249. function maxvalue(const data : array of Double) : Double; inline;
  2250. begin
  2251. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  2252. end;
  2253. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  2254. var
  2255. i : SizeInt;
  2256. begin
  2257. { get an initial value }
  2258. maxvalue:=data[0];
  2259. for i:=1 to N-1 do
  2260. if data[i]>maxvalue then
  2261. maxvalue:=data[i];
  2262. end;
  2263. {$endif FPC_HAS_TYPE_DOUBLE}
  2264. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2265. function minvalue(const data : array of Extended) : Extended; inline;
  2266. begin
  2267. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  2268. end;
  2269. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  2270. var
  2271. i : SizeInt;
  2272. begin
  2273. { get an initial value }
  2274. minvalue:=data[0];
  2275. for i:=1 to N-1 do
  2276. if data[i]<minvalue then
  2277. minvalue:=data[i];
  2278. end;
  2279. function maxvalue(const data : array of Extended) : Extended; inline;
  2280. begin
  2281. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  2282. end;
  2283. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  2284. var
  2285. i : SizeInt;
  2286. begin
  2287. { get an initial value }
  2288. maxvalue:=data[0];
  2289. for i:=1 to N-1 do
  2290. if data[i]>maxvalue then
  2291. maxvalue:=data[i];
  2292. end;
  2293. {$endif FPC_HAS_TYPE_EXTENDED}
  2294. function Min(a, b: Integer): Integer;inline;
  2295. begin
  2296. if a < b then
  2297. Result := a
  2298. else
  2299. Result := b;
  2300. end;
  2301. function Max(a, b: Integer): Integer;inline;
  2302. begin
  2303. if a > b then
  2304. Result := a
  2305. else
  2306. Result := b;
  2307. end;
  2308. {
  2309. function Min(a, b: Cardinal): Cardinal;inline;
  2310. begin
  2311. if a < b then
  2312. Result := a
  2313. else
  2314. Result := b;
  2315. end;
  2316. function Max(a, b: Cardinal): Cardinal;inline;
  2317. begin
  2318. if a > b then
  2319. Result := a
  2320. else
  2321. Result := b;
  2322. end;
  2323. }
  2324. function Min(a, b: Int64): Int64;inline;
  2325. begin
  2326. if a < b then
  2327. Result := a
  2328. else
  2329. Result := b;
  2330. end;
  2331. function Max(a, b: Int64): Int64;inline;
  2332. begin
  2333. if a > b then
  2334. Result := a
  2335. else
  2336. Result := b;
  2337. end;
  2338. function Min(a, b: QWord): QWord; inline;
  2339. begin
  2340. if a < b then
  2341. Result := a
  2342. else
  2343. Result := b;
  2344. end;
  2345. function Max(a, b: QWord): Qword;inline;
  2346. begin
  2347. if a > b then
  2348. Result := a
  2349. else
  2350. Result := b;
  2351. end;
  2352. {$ifdef FPC_HAS_TYPE_SINGLE}
  2353. function Min(a, b: Single): Single;inline;
  2354. begin
  2355. if a < b then
  2356. Result := a
  2357. else
  2358. Result := b;
  2359. end;
  2360. function Max(a, b: Single): Single;inline;
  2361. begin
  2362. if a > b then
  2363. Result := a
  2364. else
  2365. Result := b;
  2366. end;
  2367. {$endif FPC_HAS_TYPE_SINGLE}
  2368. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2369. function Min(a, b: Double): Double;inline;
  2370. begin
  2371. if a < b then
  2372. Result := a
  2373. else
  2374. Result := b;
  2375. end;
  2376. function Max(a, b: Double): Double;inline;
  2377. begin
  2378. if a > b then
  2379. Result := a
  2380. else
  2381. Result := b;
  2382. end;
  2383. {$endif FPC_HAS_TYPE_DOUBLE}
  2384. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2385. function Min(a, b: Extended): Extended;inline;
  2386. begin
  2387. if a < b then
  2388. Result := a
  2389. else
  2390. Result := b;
  2391. end;
  2392. function Max(a, b: Extended): Extended;inline;
  2393. begin
  2394. if a > b then
  2395. Result := a
  2396. else
  2397. Result := b;
  2398. end;
  2399. {$endif FPC_HAS_TYPE_EXTENDED}
  2400. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  2401. begin
  2402. Result:=(AValue>=AMin) and (AValue<=AMax);
  2403. end;
  2404. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  2405. begin
  2406. Result:=(AValue>=AMin) and (AValue<=AMax);
  2407. end;
  2408. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2409. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  2410. begin
  2411. Result:=(AValue>=AMin) and (AValue<=AMax);
  2412. end;
  2413. {$endif FPC_HAS_TYPE_DOUBLE}
  2414. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  2415. begin
  2416. Result:=AValue;
  2417. If Result<AMin then
  2418. Result:=AMin;
  2419. if Result>AMax then
  2420. Result:=AMax;
  2421. end;
  2422. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  2423. begin
  2424. Result:=AValue;
  2425. If Result<AMin then
  2426. Result:=AMin;
  2427. if Result>AMax then
  2428. Result:=AMax;
  2429. end;
  2430. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2431. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  2432. begin
  2433. Result:=AValue;
  2434. If Result<AMin then
  2435. Result:=AMin;
  2436. if Result>AMax then
  2437. Result:=AMax;
  2438. end;
  2439. {$endif FPC_HAS_TYPE_DOUBLE}
  2440. Const
  2441. EZeroResolution = Extended(1E-16);
  2442. DZeroResolution = Double(1E-12);
  2443. SZeroResolution = Single(1E-4);
  2444. function IsZero(const A: Single; Epsilon: Single): Boolean;
  2445. begin
  2446. if (Epsilon=0) then
  2447. Epsilon:=SZeroResolution;
  2448. Result:=Abs(A)<=Epsilon;
  2449. end;
  2450. function IsZero(const A: Single): Boolean;inline;
  2451. begin
  2452. Result:=IsZero(A,single(SZeroResolution));
  2453. end;
  2454. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2455. function IsZero(const A: Double; Epsilon: Double): Boolean;
  2456. begin
  2457. if (Epsilon=0) then
  2458. Epsilon:=DZeroResolution;
  2459. Result:=Abs(A)<=Epsilon;
  2460. end;
  2461. function IsZero(const A: Double): Boolean;inline;
  2462. begin
  2463. Result:=IsZero(A,DZeroResolution);
  2464. end;
  2465. {$endif FPC_HAS_TYPE_DOUBLE}
  2466. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2467. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  2468. begin
  2469. if (Epsilon=0) then
  2470. Epsilon:=EZeroResolution;
  2471. Result:=Abs(A)<=Epsilon;
  2472. end;
  2473. function IsZero(const A: Extended): Boolean;inline;
  2474. begin
  2475. Result:=IsZero(A,EZeroResolution);
  2476. end;
  2477. {$endif FPC_HAS_TYPE_EXTENDED}
  2478. type
  2479. TSplitDouble = packed record
  2480. cards: Array[0..1] of cardinal;
  2481. end;
  2482. TSplitExtended = packed record
  2483. cards: Array[0..1] of cardinal;
  2484. w: word;
  2485. end;
  2486. function IsNan(const d : Single): Boolean; overload;
  2487. begin
  2488. result:=(longword(d) and $7fffffff)>$7f800000;
  2489. end;
  2490. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2491. function IsNan(const d : Double): Boolean;
  2492. var
  2493. fraczero, expMaximal: boolean;
  2494. begin
  2495. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2496. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  2497. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  2498. (TSplitDouble(d).cards[1] = 0);
  2499. {$else FPC_BIG_ENDIAN}
  2500. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  2501. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  2502. (TSplitDouble(d).cards[0] = 0);
  2503. {$endif FPC_BIG_ENDIAN}
  2504. Result:=expMaximal and not(fraczero);
  2505. end;
  2506. {$endif FPC_HAS_TYPE_DOUBLE}
  2507. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2508. function IsNan(const d : Extended): Boolean; overload;
  2509. var
  2510. fraczero, expMaximal: boolean;
  2511. begin
  2512. {$ifdef FPC_BIG_ENDIAN}
  2513. {$error no support for big endian extended type yet}
  2514. {$else FPC_BIG_ENDIAN}
  2515. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  2516. fraczero := (TSplitExtended(d).cards[0] = 0) and
  2517. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  2518. {$endif FPC_BIG_ENDIAN}
  2519. Result:=expMaximal and not(fraczero);
  2520. end;
  2521. {$endif FPC_HAS_TYPE_EXTENDED}
  2522. function IsInfinite(const d : Single): Boolean; overload;
  2523. begin
  2524. result:=(longword(d) and $7fffffff)=$7f800000;
  2525. end;
  2526. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2527. function IsInfinite(const d : Double): Boolean; overload;
  2528. var
  2529. fraczero, expMaximal: boolean;
  2530. begin
  2531. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2532. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  2533. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  2534. (TSplitDouble(d).cards[1] = 0);
  2535. {$else FPC_BIG_ENDIAN}
  2536. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  2537. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  2538. (TSplitDouble(d).cards[0] = 0);
  2539. {$endif FPC_BIG_ENDIAN}
  2540. Result:=expMaximal and fraczero;
  2541. end;
  2542. {$endif FPC_HAS_TYPE_DOUBLE}
  2543. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2544. function IsInfinite(const d : Extended): Boolean; overload;
  2545. var
  2546. fraczero, expMaximal: boolean;
  2547. begin
  2548. {$ifdef FPC_BIG_ENDIAN}
  2549. {$error no support for big endian extended type yet}
  2550. {$else FPC_BIG_ENDIAN}
  2551. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  2552. fraczero := (TSplitExtended(d).cards[0] = 0) and
  2553. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  2554. {$endif FPC_BIG_ENDIAN}
  2555. Result:=expMaximal and fraczero;
  2556. end;
  2557. {$endif FPC_HAS_TYPE_EXTENDED}
  2558. function copysign(x,y: float): float;
  2559. begin
  2560. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  2561. {$error copysign not yet implemented for float128}
  2562. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  2563. TSplitExtended(x).w:=(TSplitExtended(x).w and $7fff) or (TSplitExtended(y).w and $8000);
  2564. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  2565. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2566. TSplitDouble(x).cards[0]:=(TSplitDouble(x).cards[0] and $7fffffff) or (TSplitDouble(y).cards[0] and longword($80000000));
  2567. {$else}
  2568. TSplitDouble(x).cards[1]:=(TSplitDouble(x).cards[1] and $7fffffff) or (TSplitDouble(y).cards[1] and longword($80000000));
  2569. {$endif}
  2570. {$else}
  2571. longword(x):=longword(x and $7fffffff) or (longword(y) and longword($80000000));
  2572. {$endif}
  2573. result:=x;
  2574. end;
  2575. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2576. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  2577. begin
  2578. if (Epsilon=0) then
  2579. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  2580. if (A>B) then
  2581. Result:=((A-B)<=Epsilon)
  2582. else
  2583. Result:=((B-A)<=Epsilon);
  2584. end;
  2585. function SameValue(const A, B: Extended): Boolean;inline;
  2586. begin
  2587. Result:=SameValue(A,B,0.0);
  2588. end;
  2589. {$endif FPC_HAS_TYPE_EXTENDED}
  2590. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2591. function SameValue(const A, B: Double): Boolean;inline;
  2592. begin
  2593. Result:=SameValue(A,B,0.0);
  2594. end;
  2595. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  2596. begin
  2597. if (Epsilon=0) then
  2598. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  2599. if (A>B) then
  2600. Result:=((A-B)<=Epsilon)
  2601. else
  2602. Result:=((B-A)<=Epsilon);
  2603. end;
  2604. {$endif FPC_HAS_TYPE_DOUBLE}
  2605. function SameValue(const A, B: Single): Boolean;inline;
  2606. begin
  2607. Result:=SameValue(A,B,0);
  2608. end;
  2609. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  2610. begin
  2611. if (Epsilon=0) then
  2612. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  2613. if (A>B) then
  2614. Result:=((A-B)<=Epsilon)
  2615. else
  2616. Result:=((B-A)<=Epsilon);
  2617. end;
  2618. // Some CPUs probably allow a faster way of doing this in a single operation...
  2619. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  2620. {$ifndef FPC_MATH_HAS_DIVMOD}
  2621. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  2622. begin
  2623. if Dividend < 0 then
  2624. begin
  2625. { Use DivMod with >=0 dividend }
  2626. Dividend:=-Dividend;
  2627. { The documented behavior of Pascal's div/mod operators and DivMod
  2628. on negative dividends is to return Result closer to zero and
  2629. a negative Remainder. Which means that we can just negate both
  2630. Result and Remainder, and all it's Ok. }
  2631. Result:=-(Dividend Div Divisor);
  2632. Remainder:=-(Dividend+(Result*Divisor));
  2633. end
  2634. else
  2635. begin
  2636. Result:=Dividend Div Divisor;
  2637. Remainder:=Dividend-(Result*Divisor);
  2638. end;
  2639. end;
  2640. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  2641. begin
  2642. if Dividend < 0 then
  2643. begin
  2644. { Use DivMod with >=0 dividend }
  2645. Dividend:=-Dividend;
  2646. { The documented behavior of Pascal's div/mod operators and DivMod
  2647. on negative dividends is to return Result closer to zero and
  2648. a negative Remainder. Which means that we can just negate both
  2649. Result and Remainder, and all it's Ok. }
  2650. Result:=-(Dividend Div Divisor);
  2651. Remainder:=-(Dividend+(Result*Divisor));
  2652. end
  2653. else
  2654. begin
  2655. Result:=Dividend Div Divisor;
  2656. Remainder:=Dividend-(Result*Divisor);
  2657. end;
  2658. end;
  2659. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  2660. begin
  2661. Result:=Dividend Div Divisor;
  2662. Remainder:=Dividend-(Result*Divisor);
  2663. end;
  2664. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  2665. begin
  2666. if Dividend < 0 then
  2667. begin
  2668. { Use DivMod with >=0 dividend }
  2669. Dividend:=-Dividend;
  2670. { The documented behavior of Pascal's div/mod operators and DivMod
  2671. on negative dividends is to return Result closer to zero and
  2672. a negative Remainder. Which means that we can just negate both
  2673. Result and Remainder, and all it's Ok. }
  2674. Result:=-(Dividend Div Divisor);
  2675. Remainder:=-(Dividend+(Result*Divisor));
  2676. end
  2677. else
  2678. begin
  2679. Result:=Dividend Div Divisor;
  2680. Remainder:=Dividend-(Result*Divisor);
  2681. end;
  2682. end;
  2683. {$endif FPC_MATH_HAS_DIVMOD}
  2684. { Floating point modulo}
  2685. {$ifdef FPC_HAS_TYPE_SINGLE}
  2686. function FMod(const a, b: Single): Single;inline;overload;
  2687. begin
  2688. result:= a-b * Int(a/b);
  2689. end;
  2690. {$endif FPC_HAS_TYPE_SINGLE}
  2691. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2692. function FMod(const a, b: Double): Double;inline;overload;
  2693. begin
  2694. result:= a-b * Int(a/b);
  2695. end;
  2696. {$endif FPC_HAS_TYPE_DOUBLE}
  2697. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2698. function FMod(const a, b: Extended): Extended;inline;overload;
  2699. begin
  2700. result:= a-b * Int(a/b);
  2701. end;
  2702. {$endif FPC_HAS_TYPE_EXTENDED}
  2703. operator mod(const a,b:float) c:float;inline;
  2704. begin
  2705. c:= a-b * Int(a/b);
  2706. if SameValue(abs(c),abs(b)) then
  2707. c:=0.0;
  2708. end;
  2709. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  2710. begin
  2711. if val then result:=iftrue else result:=iffalse;
  2712. end;
  2713. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  2714. begin
  2715. if val then result:=iftrue else result:=iffalse;
  2716. end;
  2717. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  2718. begin
  2719. if val then result:=iftrue else result:=iffalse;
  2720. end;
  2721. // dilemma here. asm can do the two comparisons in one go?
  2722. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  2723. function CompareValue(const A, B : Integer): TValueRelationship;
  2724. begin
  2725. result:=GreaterThanValue;
  2726. if a=b then
  2727. result:=EqualsValue
  2728. else
  2729. if a<b then
  2730. result:=LessThanValue;
  2731. end;
  2732. function CompareValue(const A, B: Int64): TValueRelationship;
  2733. begin
  2734. result:=GreaterThanValue;
  2735. if a=b then
  2736. result:=EqualsValue
  2737. else
  2738. if a<b then
  2739. result:=LessThanValue;
  2740. end;
  2741. function CompareValue(const A, B: QWord): TValueRelationship;
  2742. begin
  2743. result:=GreaterThanValue;
  2744. if a=b then
  2745. result:=EqualsValue
  2746. else
  2747. if a<b then
  2748. result:=LessThanValue;
  2749. end;
  2750. {$ifdef FPC_HAS_TYPE_SINGLE}
  2751. function CompareValue(const A, B: Single; delta: Single = 0.0): TValueRelationship;
  2752. begin
  2753. result:=GreaterThanValue;
  2754. if abs(a-b)<=delta then
  2755. result:=EqualsValue
  2756. else
  2757. if a<b then
  2758. result:=LessThanValue;
  2759. end;
  2760. {$endif}
  2761. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2762. function CompareValue(const A, B: Double; delta: Double = 0.0): TValueRelationship;
  2763. begin
  2764. result:=GreaterThanValue;
  2765. if abs(a-b)<=delta then
  2766. result:=EqualsValue
  2767. else
  2768. if a<b then
  2769. result:=LessThanValue;
  2770. end;
  2771. {$endif}
  2772. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2773. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  2774. begin
  2775. result:=GreaterThanValue;
  2776. if abs(a-b)<=delta then
  2777. result:=EqualsValue
  2778. else
  2779. if a<b then
  2780. result:=LessThanValue;
  2781. end;
  2782. {$endif}
  2783. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2784. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  2785. var
  2786. RV : Double;
  2787. begin
  2788. RV:=IntPower(10,Digits);
  2789. Result:=Round(AValue/RV)*RV;
  2790. end;
  2791. {$endif}
  2792. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2793. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  2794. var
  2795. RV : Extended;
  2796. begin
  2797. RV:=IntPower(10,Digits);
  2798. Result:=Round(AValue/RV)*RV;
  2799. end;
  2800. {$endif}
  2801. {$ifdef FPC_HAS_TYPE_SINGLE}
  2802. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  2803. var
  2804. RV : Single;
  2805. begin
  2806. RV:=IntPower(10,Digits);
  2807. Result:=Round(AValue/RV)*RV;
  2808. end;
  2809. {$endif}
  2810. {$ifdef FPC_HAS_TYPE_SINGLE}
  2811. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  2812. var
  2813. RV : Single;
  2814. begin
  2815. RV := IntPower(10, -Digits);
  2816. if AValue < 0 then
  2817. Result := Int((AValue*RV) - 0.5)/RV
  2818. else
  2819. Result := Int((AValue*RV) + 0.5)/RV;
  2820. end;
  2821. {$endif}
  2822. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2823. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  2824. var
  2825. RV : Double;
  2826. begin
  2827. RV := IntPower(10, -Digits);
  2828. if AValue < 0 then
  2829. Result := Int((AValue*RV) - 0.5)/RV
  2830. else
  2831. Result := Int((AValue*RV) + 0.5)/RV;
  2832. end;
  2833. {$endif}
  2834. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2835. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  2836. var
  2837. RV : Extended;
  2838. begin
  2839. RV := IntPower(10, -Digits);
  2840. if AValue < 0 then
  2841. Result := Int((AValue*RV) - 0.5)/RV
  2842. else
  2843. Result := Int((AValue*RV) + 0.5)/RV;
  2844. end;
  2845. {$endif}
  2846. function RandomFrom(const AValues: array of Double): Double; overload;
  2847. begin
  2848. result:=AValues[random(High(AValues)+1)];
  2849. end;
  2850. function RandomFrom(const AValues: array of Integer): Integer; overload;
  2851. begin
  2852. result:=AValues[random(High(AValues)+1)];
  2853. end;
  2854. function RandomFrom(const AValues: array of Int64): Int64; overload;
  2855. begin
  2856. result:=AValues[random(High(AValues)+1)];
  2857. end;
  2858. {$if FPC_FULLVERSION >=30101}
  2859. generic function RandomFrom<T>(const AValues:array of T):T;
  2860. begin
  2861. result:=AValues[random(High(AValues)+1)];
  2862. end;
  2863. {$endif}
  2864. function FutureValue(ARate: Float; NPeriods: Integer;
  2865. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  2866. var
  2867. q, qn, factor: Float;
  2868. begin
  2869. if ARate = 0 then
  2870. Result := -APresentValue - APayment * NPeriods
  2871. else begin
  2872. q := 1.0 + ARate;
  2873. qn := power(q, NPeriods);
  2874. factor := (qn - 1) / (q - 1);
  2875. if APaymentTime = ptStartOfPeriod then
  2876. factor := factor * q;
  2877. Result := -(APresentValue * qn + APayment*factor);
  2878. end;
  2879. end;
  2880. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  2881. APaymentTime: TPaymentTime): Float;
  2882. { The interest rate cannot be calculated analytically. We solve the equation
  2883. numerically by means of the Newton method:
  2884. - guess value for the interest reate
  2885. - calculate at which interest rate the tangent of the curve fv(rate)
  2886. (straight line!) has the requested future vale.
  2887. - use this rate for the next iteration. }
  2888. const
  2889. DELTA = 0.001;
  2890. EPS = 1E-9; // required precision of interest rate (after typ. 6 iterations)
  2891. MAXIT = 20; // max iteration count to protect agains non-convergence
  2892. var
  2893. r1, r2, dr: Float;
  2894. fv1, fv2: Float;
  2895. iteration: Integer;
  2896. begin
  2897. iteration := 0;
  2898. r1 := 0.05; // inital guess
  2899. repeat
  2900. r2 := r1 + DELTA;
  2901. fv1 := FutureValue(r1, NPeriods, APayment, APresentValue, APaymentTime);
  2902. fv2 := FutureValue(r2, NPeriods, APayment, APresentValue, APaymentTime);
  2903. dr := (AFutureValue - fv1) / (fv2 - fv1) * delta; // tangent at fv(r)
  2904. r1 := r1 + dr; // next guess
  2905. inc(iteration);
  2906. until (abs(dr) < EPS) or (iteration >= MAXIT);
  2907. Result := r1;
  2908. end;
  2909. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  2910. APaymentTime: TPaymentTime): Float;
  2911. { Solve the cash flow equation (1) for q^n and take the logarithm }
  2912. var
  2913. q, x1, x2: Float;
  2914. begin
  2915. if ARate = 0 then
  2916. Result := -(APresentValue + AFutureValue) / APayment
  2917. else begin
  2918. q := 1.0 + ARate;
  2919. if APaymentTime = ptStartOfPeriod then
  2920. APayment := APayment * q;
  2921. x1 := APayment - AFutureValue * ARate;
  2922. x2 := APayment + APresentValue * ARate;
  2923. if (x2 = 0) // we have to divide by x2
  2924. or (sign(x1) * sign(x2) < 0) // the argument of the log is negative
  2925. then
  2926. Result := Infinity
  2927. else begin
  2928. Result := ln(x1/x2) / ln(q);
  2929. end;
  2930. end;
  2931. end;
  2932. function Payment(ARate: Float; NPeriods: Integer;
  2933. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  2934. var
  2935. q, qn, factor: Float;
  2936. begin
  2937. if ARate = 0 then
  2938. Result := -(AFutureValue + APresentValue) / NPeriods
  2939. else begin
  2940. q := 1.0 + ARate;
  2941. qn := power(q, NPeriods);
  2942. factor := (qn - 1) / (q - 1);
  2943. if APaymentTime = ptStartOfPeriod then
  2944. factor := factor * q;
  2945. Result := -(AFutureValue + APresentValue * qn) / factor;
  2946. end;
  2947. end;
  2948. function PresentValue(ARate: Float; NPeriods: Integer;
  2949. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  2950. var
  2951. q, qn, factor: Float;
  2952. begin
  2953. if ARate = 0.0 then
  2954. Result := -AFutureValue - APayment * NPeriods
  2955. else begin
  2956. q := 1.0 + ARate;
  2957. qn := power(q, NPeriods);
  2958. factor := (qn - 1) / (q - 1);
  2959. if APaymentTime = ptStartOfPeriod then
  2960. factor := factor * q;
  2961. Result := -(AFutureValue + APayment*factor) / qn;
  2962. end;
  2963. end;
  2964. {$else}
  2965. implementation
  2966. {$endif FPUNONE}
  2967. end.