math.pp 97 KB

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