math.pp 97 KB

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