math.pp 96 KB

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