math.pp 95 KB

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