math.pp 97 KB

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