math.pp 93 KB

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