math.pp 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {-------------------------------------------------------------------------
  12. Using functions from AMath/DAMath libraries, which are covered by the
  13. following license:
  14. (C) Copyright 2009-2013 Wolfgang Ehrhardt
  15. This software is provided 'as-is', without any express or implied warranty.
  16. In no event will the authors be held liable for any damages arising from
  17. the use of this software.
  18. Permission is granted to anyone to use this software for any purpose,
  19. including commercial applications, and to alter it and redistribute it
  20. freely, subject to the following restrictions:
  21. 1. The origin of this software must not be misrepresented; you must not
  22. claim that you wrote the original software. If you use this software in
  23. a product, an acknowledgment in the product documentation would be
  24. appreciated but is not required.
  25. 2. Altered source versions must be plainly marked as such, and must not be
  26. misrepresented as being the original software.
  27. 3. This notice may not be removed or altered from any source distribution.
  28. ----------------------------------------------------------------------------}
  29. {
  30. This unit is an equivalent to the Delphi Math unit
  31. (with some improvements)
  32. What's to do:
  33. o some statistical functions
  34. o optimizations
  35. }
  36. {$MODE objfpc}
  37. {$inline on }
  38. {$GOTO on}
  39. unit Math;
  40. interface
  41. {$ifndef FPUNONE}
  42. uses
  43. sysutils;
  44. {$IFDEF FPDOC_MATH}
  45. Type
  46. Float = MaxFloatType;
  47. Const
  48. MinFloat = 0;
  49. MaxFloat = 0;
  50. {$ENDIF}
  51. { Ranges of the IEEE floating point types, including denormals }
  52. {$ifdef FPC_HAS_TYPE_SINGLE}
  53. const
  54. { values according to
  55. https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Single-precision_examples
  56. }
  57. MinSingle = 1.1754943508e-38;
  58. MaxSingle = 3.4028234664e+38;
  59. {$endif FPC_HAS_TYPE_SINGLE}
  60. {$ifdef FPC_HAS_TYPE_DOUBLE}
  61. const
  62. { values according to
  63. https://en.wikipedia.org/wiki/Double-precision_floating-point_format#Double-precision_examples
  64. }
  65. MinDouble = 2.2250738585072014e-308;
  66. MaxDouble = 1.7976931348623157e+308;
  67. {$endif FPC_HAS_TYPE_DOUBLE}
  68. {$ifdef FPC_HAS_TYPE_EXTENDED}
  69. const
  70. MinExtended = 3.4e-4932;
  71. MaxExtended = 1.1e+4932;
  72. {$endif FPC_HAS_TYPE_EXTENDED}
  73. {$ifdef FPC_HAS_TYPE_COMP}
  74. const
  75. MinComp = -9.223372036854775807e+18;
  76. MaxComp = 9.223372036854775807e+18;
  77. {$endif FPC_HAS_TYPE_COMP}
  78. { the original delphi functions use extended as argument, }
  79. { but I would prefer double, because 8 bytes is a very }
  80. { natural size for the processor }
  81. { WARNING : changing float type will }
  82. { break all assembler code PM }
  83. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  84. type
  85. Float = Float128;
  86. const
  87. MinFloat = MinFloat128;
  88. MaxFloat = MaxFloat128;
  89. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  90. type
  91. Float = extended;
  92. const
  93. MinFloat = MinExtended;
  94. MaxFloat = MaxExtended;
  95. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  96. type
  97. Float = double;
  98. const
  99. MinFloat = MinDouble;
  100. MaxFloat = MaxDouble;
  101. {$elseif defined(FPC_HAS_TYPE_SINGLE)}
  102. type
  103. Float = single;
  104. const
  105. MinFloat = MinSingle;
  106. MaxFloat = MaxSingle;
  107. {$else}
  108. {$fatal At least one floating point type must be supported}
  109. {$endif}
  110. type
  111. PFloat = ^Float;
  112. PInteger = ObjPas.PInteger;
  113. TPaymentTime = (ptEndOfPeriod,ptStartOfPeriod);
  114. EInvalidArgument = class(ematherror);
  115. TValueRelationship = -1..1;
  116. const
  117. EqualsValue = 0;
  118. LessThanValue = Low(TValueRelationship);
  119. GreaterThanValue = High(TValueRelationship);
  120. {$push}
  121. {$R-}
  122. {$Q-}
  123. NaN = 0.0/0.0;
  124. Infinity = 1.0/0.0;
  125. NegInfinity = -1.0/0.0;
  126. {$pop}
  127. {$IFDEF FPDOC_MATH}
  128. // This must be after the above defines.
  129. {$DEFINE FPC_HAS_TYPE_SINGLE}
  130. {$DEFINE FPC_HAS_TYPE_DOUBLE}
  131. {$DEFINE FPC_HAS_TYPE_EXTENDED}
  132. {$DEFINE FPC_HAS_TYPE_COMP}
  133. {$ENDIF}
  134. { Min/max determination }
  135. function MinIntValue(const Data: array of Integer): Integer;
  136. function MaxIntValue(const Data: array of Integer): Integer;
  137. { Extra, not present in Delphi, but used frequently }
  138. function Min(a, b: Integer): Integer;inline; overload;
  139. function Max(a, b: Integer): Integer;inline; overload;
  140. { this causes more trouble than it solves
  141. function Min(a, b: Cardinal): Cardinal; overload;
  142. function Max(a, b: Cardinal): Cardinal; overload;
  143. }
  144. function Min(a, b: Int64): Int64;inline; overload;
  145. function Max(a, b: Int64): Int64;inline; overload;
  146. function Min(a, b: QWord): QWord;inline; overload;
  147. function Max(a, b: QWord): QWord;inline; overload;
  148. {$ifdef FPC_HAS_TYPE_SINGLE}
  149. function Min(a, b: Single): Single;inline; overload;
  150. function Max(a, b: Single): Single;inline; overload;
  151. {$endif FPC_HAS_TYPE_SINGLE}
  152. {$ifdef FPC_HAS_TYPE_DOUBLE}
  153. function Min(a, b: Double): Double;inline; overload;
  154. function Max(a, b: Double): Double;inline; overload;
  155. {$endif FPC_HAS_TYPE_DOUBLE}
  156. {$ifdef FPC_HAS_TYPE_EXTENDED}
  157. function Min(a, b: Extended): Extended;inline; overload;
  158. function Max(a, b: Extended): Extended;inline; overload;
  159. {$endif FPC_HAS_TYPE_EXTENDED}
  160. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline; overload;
  161. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline; overload;
  162. {$ifdef FPC_HAS_TYPE_DOUBLE}
  163. function InRange(const AValue, AMin, AMax: Double): Boolean;inline; overload;
  164. {$endif FPC_HAS_TYPE_DOUBLE}
  165. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline; overload;
  166. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline; overload;
  167. {$ifdef FPC_HAS_TYPE_DOUBLE}
  168. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline; overload;
  169. {$endif FPC_HAS_TYPE_DOUBLE}
  170. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  171. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  172. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  173. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  174. { Floating point modulo}
  175. {$ifdef FPC_HAS_TYPE_SINGLE}
  176. function FMod(const a, b: Single): Single;inline;overload;
  177. {$endif FPC_HAS_TYPE_SINGLE}
  178. {$ifdef FPC_HAS_TYPE_DOUBLE}
  179. function FMod(const a, b: Double): Double;inline;overload;
  180. {$endif FPC_HAS_TYPE_DOUBLE}
  181. {$ifdef FPC_HAS_TYPE_EXTENDED}
  182. function FMod(const a, b: Extended): Extended;inline;overload;
  183. {$endif FPC_HAS_TYPE_EXTENDED}
  184. operator mod(const a,b:float) c:float;inline;
  185. // Sign functions
  186. Type
  187. TValueSign = -1..1;
  188. const
  189. NegativeValue = Low(TValueSign);
  190. ZeroValue = 0;
  191. PositiveValue = High(TValueSign);
  192. function Sign(const AValue: Integer): TValueSign;inline; overload;
  193. function Sign(const AValue: Int64): TValueSign;inline; overload;
  194. {$ifdef FPC_HAS_TYPE_SINGLE}
  195. function Sign(const AValue: Single): TValueSign;inline; overload;
  196. {$endif}
  197. function Sign(const AValue: Double): TValueSign;inline; overload;
  198. {$ifdef FPC_HAS_TYPE_EXTENDED}
  199. function Sign(const AValue: Extended): TValueSign;inline; overload;
  200. {$endif}
  201. function IsZero(const A: Single; Epsilon: Single): Boolean; overload;
  202. function IsZero(const A: Single): Boolean;inline; overload;
  203. {$ifdef FPC_HAS_TYPE_DOUBLE}
  204. function IsZero(const A: Double; Epsilon: Double): Boolean; overload;
  205. function IsZero(const A: Double): Boolean;inline; overload;
  206. {$endif FPC_HAS_TYPE_DOUBLE}
  207. {$ifdef FPC_HAS_TYPE_EXTENDED}
  208. function IsZero(const A: Extended; Epsilon: Extended): Boolean; overload;
  209. function IsZero(const A: Extended): Boolean;inline; overload;
  210. {$endif FPC_HAS_TYPE_EXTENDED}
  211. function IsNan(const d : Single): Boolean; overload;
  212. {$ifdef FPC_HAS_TYPE_DOUBLE}
  213. function IsNan(const d : Double): Boolean; overload;
  214. {$endif FPC_HAS_TYPE_DOUBLE}
  215. {$ifdef FPC_HAS_TYPE_EXTENDED}
  216. function IsNan(const d : Extended): Boolean; overload;
  217. {$endif FPC_HAS_TYPE_EXTENDED}
  218. function IsInfinite(const d : Single): Boolean; overload;
  219. {$ifdef FPC_HAS_TYPE_DOUBLE}
  220. function IsInfinite(const d : Double): Boolean; overload;
  221. {$endif FPC_HAS_TYPE_DOUBLE}
  222. {$ifdef FPC_HAS_TYPE_EXTENDED}
  223. function IsInfinite(const d : Extended): Boolean; overload;
  224. {$endif FPC_HAS_TYPE_EXTENDED}
  225. {$ifdef FPC_HAS_TYPE_EXTENDED}
  226. function SameValue(const A, B: Extended): Boolean;inline; overload;
  227. {$endif}
  228. {$ifdef FPC_HAS_TYPE_DOUBLE}
  229. function SameValue(const A, B: Double): Boolean;inline; overload;
  230. {$endif}
  231. function SameValue(const A, B: Single): Boolean;inline; overload;
  232. {$ifdef FPC_HAS_TYPE_EXTENDED}
  233. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean; overload;
  234. {$endif}
  235. {$ifdef FPC_HAS_TYPE_DOUBLE}
  236. function SameValue(const A, B: Double; Epsilon: Double): Boolean; overload;
  237. {$endif}
  238. function SameValue(const A, B: Single; Epsilon: Single): Boolean; overload;
  239. type
  240. TRoundToRange = -37..37;
  241. {$ifdef FPC_HAS_TYPE_DOUBLE}
  242. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  243. {$endif}
  244. {$ifdef FPC_HAS_TYPE_EXTENDED}
  245. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  246. {$endif}
  247. {$ifdef FPC_HAS_TYPE_SINGLE}
  248. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  249. {$endif}
  250. {$ifdef FPC_HAS_TYPE_SINGLE}
  251. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  252. {$endif}
  253. {$ifdef FPC_HAS_TYPE_DOUBLE}
  254. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  255. {$endif}
  256. {$ifdef FPC_HAS_TYPE_EXTENDED}
  257. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  258. {$endif}
  259. { angle conversion }
  260. function DegToRad(deg : float) : float;inline;
  261. function RadToDeg(rad : float) : float;inline;
  262. function GradToRad(grad : float) : float;inline;
  263. function RadToGrad(rad : float) : float;inline;
  264. function DegToGrad(deg : float) : float;inline;
  265. function GradToDeg(grad : float) : float;inline;
  266. { one cycle are 2*Pi rad }
  267. function CycleToRad(cycle : float) : float;inline;
  268. function RadToCycle(rad : float) : float;inline;
  269. {$ifdef FPC_HAS_TYPE_SINGLE}
  270. Function DegNormalize(deg : single) : single; inline;
  271. {$ENDIF}
  272. {$ifdef FPC_HAS_TYPE_DOUBLE}
  273. Function DegNormalize(deg : double) : double; inline;
  274. {$ENDIF}
  275. {$ifdef FPC_HAS_TYPE_EXTENDED}
  276. Function DegNormalize(deg : extended) : extended; inline;
  277. {$ENDIF}
  278. { trigoniometric functions }
  279. function Tan(x : float) : float;
  280. function Cotan(x : float) : float;
  281. function Cot(x : float) : float; inline;
  282. {$ifdef FPC_HAS_TYPE_SINGLE}
  283. procedure SinCos(theta : single;out sinus,cosinus : single);
  284. {$endif}
  285. {$ifdef FPC_HAS_TYPE_DOUBLE}
  286. procedure SinCos(theta : double;out sinus,cosinus : double);
  287. {$endif}
  288. {$ifdef FPC_HAS_TYPE_EXTENDED}
  289. procedure SinCos(theta : extended;out sinus,cosinus : extended);
  290. {$endif}
  291. function Secant(x : float) : float; inline;
  292. function Cosecant(x : float) : float; inline;
  293. function Sec(x : float) : float; inline;
  294. function Csc(x : float) : float; inline;
  295. { inverse functions }
  296. function ArcCos(x : float) : float;
  297. function ArcSin(x : float) : float;
  298. { calculates arctan(y/x) and returns an angle in the correct quadrant }
  299. function ArcTan2(y,x : float) : float;
  300. { hyperbolic functions }
  301. function CosH(x : float) : float;
  302. function SinH(x : float) : float;
  303. function TanH(x : float) : float;
  304. { area functions }
  305. { delphi names: }
  306. function ArcCosH(x : float) : float;inline;
  307. function ArcSinH(x : float) : float;inline;
  308. function ArcTanH(x : float) : float;inline;
  309. { IMHO the function should be called as follows (FK) }
  310. function ArCosH(x : float) : float;
  311. function ArSinH(x : float) : float;
  312. function ArTanH(x : float) : float;
  313. { triangle functions }
  314. { returns the length of the hypotenuse of a right triangle }
  315. { if x and y are the other sides }
  316. function Hypot(x,y : float) : float;
  317. { logarithm functions }
  318. function Log10(x : float) : float;
  319. function Log2(x : float) : float;
  320. function LogN(n,x : float) : float;
  321. { returns natural logarithm of x+1, accurate for x values near zero }
  322. function LnXP1(x : float) : float;
  323. { exponential functions }
  324. function Power(base,exponent : float) : float;
  325. { base^exponent }
  326. function IntPower(base : float;const exponent : Integer) : float;
  327. operator ** (bas,expo : float) e: float; inline;
  328. operator ** (bas,expo : int64) i: int64; inline;
  329. { number converting }
  330. { rounds x towards positive infinity }
  331. function Ceil(x : float) : Integer;
  332. function Ceil64(x: float): Int64;
  333. { rounds x towards negative infinity }
  334. function Floor(x : float) : Integer;
  335. function Floor64(x: float): Int64;
  336. { misc. functions }
  337. { splits x into mantissa and exponent (to base 2) }
  338. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  339. { returns x*(2^p) }
  340. function Ldexp(x : float; const p : Integer) : float;
  341. { statistical functions }
  342. {$ifdef FPC_HAS_TYPE_SINGLE}
  343. function Mean(const data : array of Single) : float;
  344. function Sum(const data : array of Single) : float;inline;
  345. function Mean(const data : PSingle; Const N : longint) : float;
  346. function Sum(const data : PSingle; Const N : Longint) : float;
  347. {$endif FPC_HAS_TYPE_SINGLE}
  348. {$ifdef FPC_HAS_TYPE_DOUBLE}
  349. function Mean(const data : array of double) : float;inline;
  350. function Sum(const data : array of double) : float;inline;
  351. function Mean(const data : PDouble; Const N : longint) : float;
  352. function Sum(const data : PDouble; Const N : Longint) : float;
  353. {$endif FPC_HAS_TYPE_DOUBLE}
  354. {$ifdef FPC_HAS_TYPE_EXTENDED}
  355. function Mean(const data : array of Extended) : float;
  356. function Sum(const data : array of Extended) : float;inline;
  357. function Mean(const data : PExtended; Const N : longint) : float;
  358. function Sum(const data : PExtended; Const N : Longint) : float;
  359. {$endif FPC_HAS_TYPE_EXTENDED}
  360. function SumInt(const data : PInt64;Const N : longint) : Int64;
  361. function SumInt(const data : array of Int64) : Int64;inline;
  362. function Mean(const data : PInt64; const N : Longint):Float;
  363. function Mean(const data: array of Int64):Float;
  364. function SumInt(const data : PInteger; Const N : longint) : Int64;
  365. function SumInt(const data : array of Integer) : Int64;inline;
  366. function Mean(const data : PInteger; const N : Longint):Float;
  367. function Mean(const data: array of Integer):Float;
  368. {$ifdef FPC_HAS_TYPE_SINGLE}
  369. function SumOfSquares(const data : array of Single) : float;inline;
  370. function SumOfSquares(const data : PSingle; Const N : Integer) : float;
  371. { calculates the sum and the sum of squares of data }
  372. procedure SumsAndSquares(const data : array of Single;
  373. var sum,sumofsquares : float);inline;
  374. procedure SumsAndSquares(const data : PSingle; Const N : Integer;
  375. var sum,sumofsquares : float);
  376. {$endif FPC_HAS_TYPE_SINGLE}
  377. {$ifdef FPC_HAS_TYPE_DOUBLE}
  378. function SumOfSquares(const data : array of double) : float;
  379. function SumOfSquares(const data : PDouble; Const N : Integer) : float;
  380. { calculates the sum and the sum of squares of data }
  381. procedure SumsAndSquares(const data : array of Double;
  382. var sum,sumofsquares : float);inline;
  383. procedure SumsAndSquares(const data : PDouble; Const N : Integer;
  384. var sum,sumofsquares : float);
  385. {$endif FPC_HAS_TYPE_DOUBLE}
  386. {$ifdef FPC_HAS_TYPE_EXTENDED}
  387. function SumOfSquares(const data : array of Extended) : float;inline;
  388. function SumOfSquares(const data : PExtended; Const N : Integer) : float;
  389. { calculates the sum and the sum of squares of data }
  390. procedure SumsAndSquares(const data : array of Extended;
  391. var sum,sumofsquares : float);inline;
  392. procedure SumsAndSquares(const data : PExtended; Const N : Integer;
  393. var sum,sumofsquares : float);
  394. {$endif FPC_HAS_TYPE_EXTENDED}
  395. {$ifdef FPC_HAS_TYPE_SINGLE}
  396. function MinValue(const data : array of Single) : Single;inline;
  397. function MinValue(const data : PSingle; Const N : Integer) : Single;
  398. function MaxValue(const data : array of Single) : Single;inline;
  399. function MaxValue(const data : PSingle; Const N : Integer) : Single;
  400. {$endif FPC_HAS_TYPE_SINGLE}
  401. {$ifdef FPC_HAS_TYPE_DOUBLE}
  402. function MinValue(const data : array of Double) : Double;inline;
  403. function MinValue(const data : PDouble; Const N : Integer) : Double;
  404. function MaxValue(const data : array of Double) : Double;inline;
  405. function MaxValue(const data : PDouble; Const N : Integer) : Double;
  406. {$endif FPC_HAS_TYPE_DOUBLE}
  407. {$ifdef FPC_HAS_TYPE_EXTENDED}
  408. function MinValue(const data : array of Extended) : Extended;inline;
  409. function MinValue(const data : PExtended; Const N : Integer) : Extended;
  410. function MaxValue(const data : array of Extended) : Extended;inline;
  411. function MaxValue(const data : PExtended; Const N : Integer) : Extended;
  412. {$endif FPC_HAS_TYPE_EXTENDED}
  413. function MinValue(const data : array of integer) : Integer;inline;
  414. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  415. function MaxValue(const data : array of integer) : Integer;inline;
  416. function MaxValue(const data : PInteger; Const N : Integer) : Integer;
  417. { returns random values with gaussian distribution }
  418. function RandG(mean,stddev : float) : float;
  419. function RandomRange(const aFrom, aTo: Integer): Integer;
  420. function RandomRange(const aFrom, aTo: Int64): Int64;
  421. {$ifdef FPC_HAS_TYPE_SINGLE}
  422. { calculates the standard deviation }
  423. function StdDev(const data : array of Single) : float;inline;
  424. function StdDev(const data : PSingle; Const N : Integer) : float;
  425. { calculates the mean and stddev }
  426. procedure MeanAndStdDev(const data : array of Single;
  427. var mean,stddev : float);inline;
  428. procedure MeanAndStdDev(const data : PSingle;
  429. Const N : Longint;var mean,stddev : float);
  430. function Variance(const data : array of Single) : float;inline;
  431. function TotalVariance(const data : array of Single) : float;inline;
  432. function Variance(const data : PSingle; Const N : Integer) : float;
  433. function TotalVariance(const data : PSingle; Const N : Integer) : float;
  434. { Population (aka uncorrected) variance and standard deviation }
  435. function PopnStdDev(const data : array of Single) : float;inline;
  436. function PopnStdDev(const data : PSingle; Const N : Integer) : float;
  437. function PopnVariance(const data : PSingle; Const N : Integer) : float;
  438. function PopnVariance(const data : array of Single) : float;inline;
  439. procedure MomentSkewKurtosis(const data : array of Single;
  440. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  441. procedure MomentSkewKurtosis(const data : PSingle; Const N : Integer;
  442. out m1,m2,m3,m4,skew,kurtosis : float);
  443. { geometrical function }
  444. { returns the euclidean L2 norm }
  445. function Norm(const data : array of Single) : float;inline;
  446. function Norm(const data : PSingle; Const N : Integer) : float;
  447. {$endif FPC_HAS_TYPE_SINGLE}
  448. {$ifdef FPC_HAS_TYPE_DOUBLE}
  449. { calculates the standard deviation }
  450. function StdDev(const data : array of Double) : float;inline;
  451. function StdDev(const data : PDouble; Const N : Integer) : float;
  452. { calculates the mean and stddev }
  453. procedure MeanAndStdDev(const data : array of Double;
  454. var mean,stddev : float);inline;
  455. procedure MeanAndStdDev(const data : PDouble;
  456. Const N : Longint;var mean,stddev : float);
  457. function Variance(const data : array of Double) : float;inline;
  458. function TotalVariance(const data : array of Double) : float;inline;
  459. function Variance(const data : PDouble; Const N : Integer) : float;
  460. function TotalVariance(const data : PDouble; Const N : Integer) : float;
  461. { Population (aka uncorrected) variance and standard deviation }
  462. function PopnStdDev(const data : array of Double) : float;inline;
  463. function PopnStdDev(const data : PDouble; Const N : Integer) : float;
  464. function PopnVariance(const data : PDouble; Const N : Integer) : float;
  465. function PopnVariance(const data : array of Double) : float;inline;
  466. procedure MomentSkewKurtosis(const data : array of Double;
  467. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  468. procedure MomentSkewKurtosis(const data : PDouble; Const N : Integer;
  469. out m1,m2,m3,m4,skew,kurtosis : float);
  470. { geometrical function }
  471. { returns the euclidean L2 norm }
  472. function Norm(const data : array of double) : float;inline;
  473. function Norm(const data : PDouble; Const N : Integer) : float;
  474. {$endif FPC_HAS_TYPE_DOUBLE}
  475. {$ifdef FPC_HAS_TYPE_EXTENDED}
  476. { calculates the standard deviation }
  477. function StdDev(const data : array of Extended) : float;inline;
  478. function StdDev(const data : PExtended; Const N : Integer) : float;
  479. { calculates the mean and stddev }
  480. procedure MeanAndStdDev(const data : array of Extended;
  481. var mean,stddev : float);inline;
  482. procedure MeanAndStdDev(const data : PExtended;
  483. Const N : Longint;var mean,stddev : float);
  484. function Variance(const data : array of Extended) : float;inline;
  485. function TotalVariance(const data : array of Extended) : float;inline;
  486. function Variance(const data : PExtended; Const N : Integer) : float;
  487. function TotalVariance(const data : PExtended; Const N : Integer) : float;
  488. { Population (aka uncorrected) variance and standard deviation }
  489. function PopnStdDev(const data : array of Extended) : float;inline;
  490. function PopnStdDev(const data : PExtended; Const N : Integer) : float;
  491. function PopnVariance(const data : PExtended; Const N : Integer) : float;
  492. function PopnVariance(const data : array of Extended) : float;inline;
  493. procedure MomentSkewKurtosis(const data : array of Extended;
  494. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  495. procedure MomentSkewKurtosis(const data : PExtended; Const N : Integer;
  496. out m1,m2,m3,m4,skew,kurtosis : float);
  497. { geometrical function }
  498. { returns the euclidean L2 norm }
  499. function Norm(const data : array of Extended) : float;inline;
  500. function Norm(const data : PExtended; Const N : Integer) : float;
  501. {$endif FPC_HAS_TYPE_EXTENDED}
  502. { Financial functions }
  503. function FutureValue(ARate: Float; NPeriods: Integer;
  504. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  505. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  506. APaymentTime: TPaymentTime): Float;
  507. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  508. APaymentTime: TPaymentTime): Float;
  509. function Payment(ARate: Float; NPeriods: Integer;
  510. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  511. function PresentValue(ARate: Float; NPeriods: Integer;
  512. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  513. { Misc functions }
  514. function IfThen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer; inline; overload;
  515. function IfThen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64; inline; overload;
  516. function IfThen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double; inline; overload;
  517. function CompareValue ( const A, B : Integer) : TValueRelationship; inline;
  518. function CompareValue ( const A, B : Int64) : TValueRelationship; inline;
  519. function CompareValue ( const A, B : QWord) : TValueRelationship; inline;
  520. {$ifdef FPC_HAS_TYPE_SINGLE}
  521. function CompareValue ( const A, B : Single; delta : Single = 0.0 ) : TValueRelationship; inline;
  522. {$endif}
  523. {$ifdef FPC_HAS_TYPE_DOUBLE}
  524. function CompareValue ( const A, B : Double; delta : Double = 0.0) : TValueRelationship; inline;
  525. {$endif}
  526. {$ifdef FPC_HAS_TYPE_EXTENDED}
  527. function CompareValue ( const A, B : Extended; delta : Extended = 0.0 ) : TValueRelationship; inline;
  528. {$endif}
  529. function RandomFrom(const AValues: array of Double): Double; overload;
  530. function RandomFrom(const AValues: array of Integer): Integer; overload;
  531. function RandomFrom(const AValues: array of Int64): Int64; overload;
  532. {$if FPC_FULLVERSION >=30101}
  533. generic function RandomFrom<T>(const AValues:array of T):T;
  534. {$endif}
  535. { cpu specific stuff }
  536. type
  537. TFPURoundingMode = system.TFPURoundingMode;
  538. TFPUPrecisionMode = system.TFPUPrecisionMode;
  539. TFPUException = system.TFPUException;
  540. TFPUExceptionMask = system.TFPUExceptionMask;
  541. function GetRoundMode: TFPURoundingMode;
  542. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  543. function GetPrecisionMode: TFPUPrecisionMode;
  544. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  545. function GetExceptionMask: TFPUExceptionMask;
  546. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  547. procedure ClearExceptions(RaisePending: Boolean =true);
  548. implementation
  549. function copysign(x,y: float): float; forward; { returns abs(x)*sign(y) }
  550. { include cpu specific stuff }
  551. {$i mathu.inc}
  552. ResourceString
  553. SMathError = 'Math Error : %s';
  554. SInvalidArgument = 'Invalid argument';
  555. Procedure DoMathError(Const S : String);
  556. begin
  557. Raise EMathError.CreateFmt(SMathError,[S]);
  558. end;
  559. Procedure InvalidArgument;
  560. begin
  561. Raise EInvalidArgument.Create(SInvalidArgument);
  562. end;
  563. function Sign(const AValue: Integer): TValueSign;inline;
  564. begin
  565. result:=TValueSign(
  566. SarLongint(AValue,sizeof(AValue)*8-1) or { gives -1 for negative values, 0 otherwise }
  567. (longint(-AValue) shr (sizeof(AValue)*8-1)) { gives 1 for positive values, 0 otherwise }
  568. );
  569. end;
  570. function Sign(const AValue: Int64): TValueSign;inline;
  571. begin
  572. {$ifdef cpu64}
  573. result:=TValueSign(
  574. SarInt64(AValue,sizeof(AValue)*8-1) or
  575. (-AValue shr (sizeof(AValue)*8-1))
  576. );
  577. {$else cpu64}
  578. If Avalue<0 then
  579. Result:=NegativeValue
  580. else If Avalue>0 then
  581. Result:=PositiveValue
  582. else
  583. Result:=ZeroValue;
  584. {$endif}
  585. end;
  586. {$ifdef FPC_HAS_TYPE_SINGLE}
  587. function Sign(const AValue: Single): TValueSign;inline;
  588. begin
  589. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  590. end;
  591. {$endif}
  592. function Sign(const AValue: Double): TValueSign;inline;
  593. begin
  594. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  595. end;
  596. {$ifdef FPC_HAS_TYPE_EXTENDED}
  597. function Sign(const AValue: Extended): TValueSign;inline;
  598. begin
  599. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  600. end;
  601. {$endif}
  602. function degtorad(deg : float) : float;inline;
  603. begin
  604. degtorad:=deg*(pi/180.0);
  605. end;
  606. function radtodeg(rad : float) : float;inline;
  607. begin
  608. radtodeg:=rad*(180.0/pi);
  609. end;
  610. function gradtorad(grad : float) : float;inline;
  611. begin
  612. gradtorad:=grad*(pi/200.0);
  613. end;
  614. function radtograd(rad : float) : float;inline;
  615. begin
  616. radtograd:=rad*(200.0/pi);
  617. end;
  618. function degtograd(deg : float) : float;inline;
  619. begin
  620. degtograd:=deg*(200.0/180.0);
  621. end;
  622. function gradtodeg(grad : float) : float;inline;
  623. begin
  624. gradtodeg:=grad*(180.0/200.0);
  625. end;
  626. function cycletorad(cycle : float) : float;inline;
  627. begin
  628. cycletorad:=(2*pi)*cycle;
  629. end;
  630. function radtocycle(rad : float) : float;inline;
  631. begin
  632. { avoid division }
  633. radtocycle:=rad*(1/(2*pi));
  634. end;
  635. {$ifdef FPC_HAS_TYPE_SINGLE}
  636. Function DegNormalize(deg : single) : single;
  637. begin
  638. Result:=Deg-Int(Deg/360)*360;
  639. If Result<0 then Result:=Result+360;
  640. end;
  641. {$ENDIF}
  642. {$ifdef FPC_HAS_TYPE_DOUBLE}
  643. Function DegNormalize(deg : double) : double; inline;
  644. begin
  645. Result:=Deg-Int(Deg/360)*360;
  646. If (Result<0) then Result:=Result+360;
  647. end;
  648. {$ENDIF}
  649. {$ifdef FPC_HAS_TYPE_EXTENDED}
  650. Function DegNormalize(deg : extended) : extended; inline;
  651. begin
  652. Result:=Deg-Int(Deg/360)*360;
  653. If Result<0 then Result:=Result+360;
  654. end;
  655. {$ENDIF}
  656. {$ifndef FPC_MATH_HAS_TAN}
  657. function tan(x : float) : float;
  658. var
  659. _sin,_cos : float;
  660. begin
  661. sincos(x,_sin,_cos);
  662. tan:=_sin/_cos;
  663. end;
  664. {$endif FPC_MATH_HAS_TAN}
  665. {$ifndef FPC_MATH_HAS_COTAN}
  666. function cotan(x : float) : float;
  667. var
  668. _sin,_cos : float;
  669. begin
  670. sincos(x,_sin,_cos);
  671. cotan:=_cos/_sin;
  672. end;
  673. {$endif FPC_MATH_HAS_COTAN}
  674. function cot(x : float) : float; inline;
  675. begin
  676. cot := cotan(x);
  677. end;
  678. {$ifndef FPC_MATH_HAS_SINCOS}
  679. {$ifdef FPC_HAS_TYPE_SINGLE}
  680. procedure sincos(theta : single;out sinus,cosinus : single);
  681. begin
  682. sinus:=sin(theta);
  683. cosinus:=cos(theta);
  684. end;
  685. {$endif}
  686. {$ifdef FPC_HAS_TYPE_DOUBLE}
  687. procedure sincos(theta : double;out sinus,cosinus : double);
  688. begin
  689. sinus:=sin(theta);
  690. cosinus:=cos(theta);
  691. end;
  692. {$endif}
  693. {$ifdef FPC_HAS_TYPE_EXTENDED}
  694. procedure sincos(theta : extended;out sinus,cosinus : extended);
  695. begin
  696. sinus:=sin(theta);
  697. cosinus:=cos(theta);
  698. end;
  699. {$endif}
  700. {$endif FPC_MATH_HAS_SINCOS}
  701. function secant(x : float) : float; inline;
  702. begin
  703. secant := 1 / cos(x);
  704. end;
  705. function cosecant(x : float) : float; inline;
  706. begin
  707. cosecant := 1 / sin(x);
  708. end;
  709. function sec(x : float) : float; inline;
  710. begin
  711. sec := secant(x);
  712. end;
  713. function csc(x : float) : float; inline;
  714. begin
  715. csc := cosecant(x);
  716. end;
  717. { arcsin and arccos functions from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  718. function arcsin(x : float) : float;
  719. begin
  720. arcsin:=arctan2(x,sqrt((1.0-x)*(1.0+x)));
  721. end;
  722. function Arccos(x : Float) : Float;
  723. begin
  724. if abs(x)=1.0 then
  725. if x<0.0 then
  726. arccos:=Pi
  727. else
  728. arccos:=0
  729. else
  730. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  731. end;
  732. {$ifndef FPC_MATH_HAS_ARCTAN2}
  733. function arctan2(y,x : float) : float;
  734. begin
  735. if x=0 then
  736. begin
  737. if y=0 then
  738. result:=0.0
  739. else if y>0 then
  740. result:=pi/2
  741. else
  742. result:=-pi/2;
  743. end
  744. else
  745. begin
  746. result:=ArcTan(y/x);
  747. if x<0 then
  748. if y<0 then
  749. result:=result-pi
  750. else
  751. result:=result+pi;
  752. end;
  753. end;
  754. {$endif FPC_MATH_HAS_ARCTAN2}
  755. function cosh(x : float) : float;
  756. var
  757. temp : float;
  758. begin
  759. temp:=exp(x);
  760. cosh:=0.5*(temp+1.0/temp);
  761. end;
  762. function sinh(x : float) : float;
  763. var
  764. temp : float;
  765. begin
  766. temp:=exp(x);
  767. { copysign ensures that sinh(-0.0)=-0.0 }
  768. sinh:=copysign(0.5*(temp-1.0/temp),x);
  769. end;
  770. Const MaxTanh = 5678.22249441322; // Ln(MaxExtended)/2
  771. function tanh(x : float) : float;
  772. var Temp : float;
  773. begin
  774. if x>MaxTanh then exit(1.0)
  775. else if x<-MaxTanh then exit (-1.0);
  776. temp:=exp(-2*x);
  777. tanh:=(1-temp)/(1+temp)
  778. end;
  779. function arccosh(x : float) : float; inline;
  780. begin
  781. arccosh:=arcosh(x);
  782. end;
  783. function arcsinh(x : float) : float;inline;
  784. begin
  785. arcsinh:=arsinh(x);
  786. end;
  787. function arctanh(x : float) : float;inline;
  788. begin
  789. arctanh:=artanh(x);
  790. end;
  791. function arcosh(x : float) : float;
  792. begin
  793. { Provides accuracy about 4*eps near 1.0 }
  794. arcosh:=Ln(x+Sqrt((x-1.0)*(x+1.0)));
  795. end;
  796. function arsinh(x : float) : float;
  797. var
  798. z: float;
  799. begin
  800. z:=abs(x);
  801. z:=Ln(z+Sqrt(1+z*z));
  802. { copysign ensures that arsinh(-Inf)=-Inf and arsinh(-0.0)=-0.0 }
  803. arsinh:=copysign(z,x);
  804. end;
  805. function artanh(x : float) : float;
  806. begin
  807. artanh:=(lnxp1(x)-lnxp1(-x))*0.5;
  808. end;
  809. { hypot function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  810. function hypot(x,y : float) : float;
  811. begin
  812. x:=abs(x);
  813. y:=abs(y);
  814. if (x>y) then
  815. hypot:=x*sqrt(1.0+sqr(y/x))
  816. else if (x>0.0) then
  817. hypot:=y*sqrt(1.0+sqr(x/y))
  818. else
  819. hypot:=y;
  820. end;
  821. function log10(x : float) : float;
  822. begin
  823. log10:=ln(x)*0.43429448190325182765; { 1/ln(10) }
  824. end;
  825. {$ifndef FPC_MATH_HAS_LOG2}
  826. function log2(x : float) : float;
  827. begin
  828. log2:=ln(x)*1.4426950408889634079; { 1/ln(2) }
  829. end;
  830. {$endif FPC_MATH_HAS_LOG2}
  831. function logn(n,x : float) : float;
  832. begin
  833. logn:=ln(x)/ln(n);
  834. end;
  835. { lnxp1 function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  836. function lnxp1(x : float) : float;
  837. var
  838. y: float;
  839. begin
  840. if (x>=4.0) then
  841. lnxp1:=ln(1.0+x)
  842. else
  843. begin
  844. y:=1.0+x;
  845. if (y=1.0) then
  846. lnxp1:=x
  847. else
  848. begin
  849. lnxp1:=ln(y); { lnxp1(-1) = ln(0) = -Inf }
  850. if y>0.0 then
  851. lnxp1:=lnxp1+(x-(y-1.0))/y;
  852. end;
  853. end;
  854. end;
  855. function power(base,exponent : float) : float;
  856. begin
  857. if Exponent=0.0 then
  858. result:=1.0
  859. else if (base=0.0) and (exponent>0.0) then
  860. result:=0.0
  861. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  862. result:=intpower(base,trunc(exponent))
  863. else
  864. result:=exp(exponent * ln (base));
  865. end;
  866. function intpower(base : float;const exponent : Integer) : float;
  867. var
  868. i : longint;
  869. begin
  870. if (base = 0.0) and (exponent = 0) then
  871. result:=1
  872. else
  873. begin
  874. if exponent<0 then
  875. base:=1.0/base;
  876. i:=abs(exponent);
  877. intpower:=1.0;
  878. while i>0 do
  879. begin
  880. while (i and 1)=0 do
  881. begin
  882. i:=i shr 1;
  883. base:=sqr(base);
  884. end;
  885. i:=i-1;
  886. intpower:=intpower*base;
  887. end;
  888. end;
  889. end;
  890. operator ** (bas,expo : float) e: float; inline;
  891. begin
  892. e:=power(bas,expo);
  893. end;
  894. operator ** (bas,expo : int64) i: int64; inline;
  895. begin
  896. i:=round(intpower(bas,expo));
  897. end;
  898. function ceil(x : float) : integer;
  899. begin
  900. Result:=Trunc(x)+ord(Frac(x)>0);
  901. end;
  902. function ceil64(x: float): Int64;
  903. begin
  904. Result:=Trunc(x)+ord(Frac(x)>0);
  905. end;
  906. function floor(x : float) : integer;
  907. begin
  908. Result:=Trunc(x)-ord(Frac(x)<0);
  909. end;
  910. function floor64(x: float): Int64;
  911. begin
  912. Result:=Trunc(x)-ord(Frac(x)<0);
  913. end;
  914. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  915. begin
  916. Exponent:=0;
  917. if (X<>0) then
  918. if (abs(X)<0.5) then
  919. repeat
  920. X:=X*2;
  921. Dec(Exponent);
  922. until (abs(X)>=0.5)
  923. else
  924. while (abs(X)>=1) do
  925. begin
  926. X:=X/2;
  927. Inc(Exponent);
  928. end;
  929. Mantissa:=X;
  930. end;
  931. function ldexp(x : float;const p : Integer) : float;
  932. begin
  933. ldexp:=x*intpower(2.0,p);
  934. end;
  935. {$ifdef FPC_HAS_TYPE_SINGLE}
  936. function mean(const data : array of Single) : float;
  937. begin
  938. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  939. end;
  940. function mean(const data : PSingle; Const N : longint) : float;
  941. begin
  942. mean:=sum(Data,N);
  943. mean:=mean/N;
  944. end;
  945. function sum(const data : array of Single) : float;inline;
  946. begin
  947. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  948. end;
  949. function sum(const data : PSingle;Const N : longint) : float;
  950. var
  951. i : SizeInt;
  952. begin
  953. sum:=0.0;
  954. for i:=0 to N-1 do
  955. sum:=sum+data[i];
  956. end;
  957. {$endif FPC_HAS_TYPE_SINGLE}
  958. {$ifdef FPC_HAS_TYPE_DOUBLE}
  959. function mean(const data : array of Double) : float; inline;
  960. begin
  961. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  962. end;
  963. function mean(const data : PDouble; Const N : longint) : float;
  964. begin
  965. mean:=sum(Data,N);
  966. mean:=mean/N;
  967. end;
  968. function sum(const data : array of Double) : float; inline;
  969. begin
  970. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  971. end;
  972. function sum(const data : PDouble;Const N : longint) : float;
  973. var
  974. i : SizeInt;
  975. begin
  976. sum:=0.0;
  977. for i:=0 to N-1 do
  978. sum:=sum+data[i];
  979. end;
  980. {$endif FPC_HAS_TYPE_DOUBLE}
  981. {$ifdef FPC_HAS_TYPE_EXTENDED}
  982. function mean(const data : array of Extended) : float;
  983. begin
  984. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  985. end;
  986. function mean(const data : PExtended; Const N : longint) : float;
  987. begin
  988. mean:=sum(Data,N);
  989. mean:=mean/N;
  990. end;
  991. function sum(const data : array of Extended) : float; inline;
  992. begin
  993. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  994. end;
  995. function sum(const data : PExtended;Const N : longint) : float;
  996. var
  997. i : SizeInt;
  998. begin
  999. sum:=0.0;
  1000. for i:=0 to N-1 do
  1001. sum:=sum+data[i];
  1002. end;
  1003. {$endif FPC_HAS_TYPE_EXTENDED}
  1004. function sumInt(const data : PInt64;Const N : longint) : Int64;
  1005. var
  1006. i : SizeInt;
  1007. begin
  1008. sumInt:=0;
  1009. for i:=0 to N-1 do
  1010. sumInt:=sumInt+data[i];
  1011. end;
  1012. function sumInt(const data : array of Int64) : Int64; inline;
  1013. begin
  1014. Result:=SumInt(PInt64(@Data[0]),High(Data)+1);
  1015. end;
  1016. function mean(const data : PInt64; const N : Longint):Float;
  1017. begin
  1018. mean:=sumInt(Data,N);
  1019. mean:=mean/N;
  1020. end;
  1021. function mean(const data: array of Int64):Float;
  1022. begin
  1023. mean:=mean(PInt64(@data[0]),High(Data)+1);
  1024. end;
  1025. function sumInt(const data : PInteger; Const N : longint) : Int64;
  1026. var
  1027. i : SizeInt;
  1028. begin
  1029. sumInt:=0;
  1030. for i:=0 to N-1 do
  1031. sumInt:=sumInt+data[i];
  1032. end;
  1033. function sumInt(const data : array of Integer) : Int64;inline;
  1034. begin
  1035. Result:=sumInt(PInteger(@Data[0]),High(Data)+1);
  1036. end;
  1037. function mean(const data : PInteger; const N : Longint):Float;
  1038. begin
  1039. mean:=sumInt(Data,N);
  1040. mean:=mean/N;
  1041. end;
  1042. function mean(const data: array of Integer):Float;
  1043. begin
  1044. mean:=mean(PInteger(@data[0]),High(Data)+1);
  1045. end;
  1046. {$ifdef FPC_HAS_TYPE_SINGLE}
  1047. function sumofsquares(const data : array of Single) : float; inline;
  1048. begin
  1049. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  1050. end;
  1051. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  1052. var
  1053. i : SizeInt;
  1054. begin
  1055. sumofsquares:=0.0;
  1056. for i:=0 to N-1 do
  1057. sumofsquares:=sumofsquares+sqr(data[i]);
  1058. end;
  1059. procedure sumsandsquares(const data : array of Single;
  1060. var sum,sumofsquares : float); inline;
  1061. begin
  1062. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1063. end;
  1064. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  1065. var sum,sumofsquares : float);
  1066. var
  1067. i : SizeInt;
  1068. temp : float;
  1069. begin
  1070. sumofsquares:=0.0;
  1071. sum:=0.0;
  1072. for i:=0 to N-1 do
  1073. begin
  1074. temp:=data[i];
  1075. sumofsquares:=sumofsquares+sqr(temp);
  1076. sum:=sum+temp;
  1077. end;
  1078. end;
  1079. {$endif FPC_HAS_TYPE_SINGLE}
  1080. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1081. function sumofsquares(const data : array of Double) : float; inline;
  1082. begin
  1083. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  1084. end;
  1085. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  1086. var
  1087. i : SizeInt;
  1088. begin
  1089. sumofsquares:=0.0;
  1090. for i:=0 to N-1 do
  1091. sumofsquares:=sumofsquares+sqr(data[i]);
  1092. end;
  1093. procedure sumsandsquares(const data : array of Double;
  1094. var sum,sumofsquares : float);
  1095. begin
  1096. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1097. end;
  1098. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  1099. var sum,sumofsquares : float);
  1100. var
  1101. i : SizeInt;
  1102. temp : float;
  1103. begin
  1104. sumofsquares:=0.0;
  1105. sum:=0.0;
  1106. for i:=0 to N-1 do
  1107. begin
  1108. temp:=data[i];
  1109. sumofsquares:=sumofsquares+sqr(temp);
  1110. sum:=sum+temp;
  1111. end;
  1112. end;
  1113. {$endif FPC_HAS_TYPE_DOUBLE}
  1114. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1115. function sumofsquares(const data : array of Extended) : float; inline;
  1116. begin
  1117. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  1118. end;
  1119. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  1120. var
  1121. i : SizeInt;
  1122. begin
  1123. sumofsquares:=0.0;
  1124. for i:=0 to N-1 do
  1125. sumofsquares:=sumofsquares+sqr(data[i]);
  1126. end;
  1127. procedure sumsandsquares(const data : array of Extended;
  1128. var sum,sumofsquares : float); inline;
  1129. begin
  1130. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1131. end;
  1132. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  1133. var sum,sumofsquares : float);
  1134. var
  1135. i : SizeInt;
  1136. temp : float;
  1137. begin
  1138. sumofsquares:=0.0;
  1139. sum:=0.0;
  1140. for i:=0 to N-1 do
  1141. begin
  1142. temp:=data[i];
  1143. sumofsquares:=sumofsquares+sqr(temp);
  1144. sum:=sum+temp;
  1145. end;
  1146. end;
  1147. {$endif FPC_HAS_TYPE_EXTENDED}
  1148. function randg(mean,stddev : float) : float;
  1149. Var U1,S2 : Float;
  1150. begin
  1151. repeat
  1152. u1:= 2*random-1;
  1153. S2:=Sqr(U1)+sqr(2*random-1);
  1154. until s2<1;
  1155. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  1156. end;
  1157. function RandomRange(const aFrom, aTo: Integer): Integer;
  1158. begin
  1159. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1160. end;
  1161. function RandomRange(const aFrom, aTo: Int64): Int64;
  1162. begin
  1163. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1164. end;
  1165. {$ifdef FPC_HAS_TYPE_SINGLE}
  1166. procedure MeanAndTotalVariance
  1167. (const data: PSingle; N: LongInt; var mu, variance: float);
  1168. var i: SizeInt;
  1169. begin
  1170. mu := Mean( data, N );
  1171. variance := 0;
  1172. for i := 0 to N - 1 do
  1173. variance := variance + Sqr( data[i] - mu );
  1174. end;
  1175. function stddev(const data : array of Single) : float; inline;
  1176. begin
  1177. Result:=Stddev(PSingle(@Data[0]),High(Data)+1);
  1178. end;
  1179. function stddev(const data : PSingle; Const N : Integer) : float;
  1180. begin
  1181. StdDev:=Sqrt(Variance(Data,N));
  1182. end;
  1183. procedure meanandstddev(const data : array of Single;
  1184. var mean,stddev : float); inline;
  1185. begin
  1186. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  1187. end;
  1188. procedure meanandstddev
  1189. ( const data: PSingle;
  1190. const N: Longint;
  1191. var mean,
  1192. stdDev: Float
  1193. );
  1194. var totalVariance: float;
  1195. begin
  1196. MeanAndTotalVariance( data, N, mean, totalVariance );
  1197. if N < 2 then stdDev := 0
  1198. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1199. end;
  1200. function variance(const data : array of Single) : float; inline;
  1201. begin
  1202. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  1203. end;
  1204. function variance(const data : PSingle; Const N : Integer) : float;
  1205. begin
  1206. If N=1 then
  1207. Result:=0
  1208. else
  1209. Result:=TotalVariance(Data,N)/(N-1);
  1210. end;
  1211. function totalvariance(const data : array of Single) : float; inline;
  1212. begin
  1213. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  1214. end;
  1215. function totalvariance(const data : PSingle; const N : Integer) : float;
  1216. var mu: float;
  1217. begin
  1218. MeanAndTotalVariance( data, N, mu, result );
  1219. end;
  1220. function popnstddev(const data : array of Single) : float;
  1221. begin
  1222. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  1223. end;
  1224. function popnstddev(const data : PSingle; Const N : Integer) : float;
  1225. begin
  1226. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1227. end;
  1228. function popnvariance(const data : array of Single) : float; inline;
  1229. begin
  1230. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  1231. end;
  1232. function popnvariance(const data : PSingle; Const N : Integer) : float;
  1233. begin
  1234. PopnVariance:=TotalVariance(Data,N)/N;
  1235. end;
  1236. procedure momentskewkurtosis(const data : array of single;
  1237. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1238. begin
  1239. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1240. end;
  1241. procedure momentskewkurtosis(
  1242. const data: pSingle;
  1243. Const N: integer;
  1244. out m1: float;
  1245. out m2: float;
  1246. out m3: float;
  1247. out m4: float;
  1248. out skew: float;
  1249. out kurtosis: float
  1250. );
  1251. var
  1252. i: SizeInt;
  1253. value : psingle;
  1254. deviation, deviation2: single;
  1255. reciprocalN: float;
  1256. begin
  1257. m1 := 0;
  1258. reciprocalN := 1/N;
  1259. value := data;
  1260. for i := 0 to N-1 do
  1261. begin
  1262. m1 := m1 + value^;
  1263. inc(value);
  1264. end;
  1265. m1 := reciprocalN * m1;
  1266. m2 := 0;
  1267. m3 := 0;
  1268. m4 := 0;
  1269. value := data;
  1270. for i := 0 to N-1 do
  1271. begin
  1272. deviation := (value^-m1);
  1273. deviation2 := deviation * deviation;
  1274. m2 := m2 + deviation2;
  1275. m3 := m3 + deviation2 * deviation;
  1276. m4 := m4 + deviation2 * deviation2;
  1277. inc(value);
  1278. end;
  1279. m2 := reciprocalN * m2;
  1280. m3 := reciprocalN * m3;
  1281. m4 := reciprocalN * m4;
  1282. skew := m3 / (sqrt(m2)*m2);
  1283. kurtosis := m4 / (m2 * m2);
  1284. end;
  1285. function norm(const data : array of Single) : float; inline;
  1286. begin
  1287. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  1288. end;
  1289. function norm(const data : PSingle; Const N : Integer) : float;
  1290. begin
  1291. norm:=sqrt(sumofsquares(data,N));
  1292. end;
  1293. {$endif FPC_HAS_TYPE_SINGLE}
  1294. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1295. procedure MeanAndTotalVariance
  1296. (const data: PDouble; N: LongInt; var mu, variance: float);
  1297. var i: SizeInt;
  1298. begin
  1299. mu := Mean( data, N );
  1300. variance := 0;
  1301. for i := 0 to N - 1 do
  1302. variance := variance + Sqr( data[i] - mu );
  1303. end;
  1304. function stddev(const data : array of Double) : float; inline;
  1305. begin
  1306. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  1307. end;
  1308. function stddev(const data : PDouble; Const N : Integer) : float;
  1309. begin
  1310. StdDev:=Sqrt(Variance(Data,N));
  1311. end;
  1312. procedure meanandstddev(const data : array of Double;
  1313. var mean,stddev : float);
  1314. begin
  1315. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  1316. end;
  1317. procedure meanandstddev
  1318. ( const data: PDouble;
  1319. const N: Longint;
  1320. var mean,
  1321. stdDev: Float
  1322. );
  1323. var totalVariance: float;
  1324. begin
  1325. MeanAndTotalVariance( data, N, mean, totalVariance );
  1326. if N < 2 then stdDev := 0
  1327. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1328. end;
  1329. function variance(const data : array of Double) : float; inline;
  1330. begin
  1331. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  1332. end;
  1333. function variance(const data : PDouble; Const N : Integer) : float;
  1334. begin
  1335. If N=1 then
  1336. Result:=0
  1337. else
  1338. Result:=TotalVariance(Data,N)/(N-1);
  1339. end;
  1340. function totalvariance(const data : array of Double) : float; inline;
  1341. begin
  1342. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  1343. end;
  1344. function totalvariance(const data : PDouble; const N : Integer) : float;
  1345. var mu: float;
  1346. begin
  1347. MeanAndTotalVariance( data, N, mu, result );
  1348. end;
  1349. function popnstddev(const data : array of Double) : float;
  1350. begin
  1351. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  1352. end;
  1353. function popnstddev(const data : PDouble; Const N : Integer) : float;
  1354. begin
  1355. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1356. end;
  1357. function popnvariance(const data : array of Double) : float; inline;
  1358. begin
  1359. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  1360. end;
  1361. function popnvariance(const data : PDouble; Const N : Integer) : float;
  1362. begin
  1363. PopnVariance:=TotalVariance(Data,N)/N;
  1364. end;
  1365. procedure momentskewkurtosis(const data : array of Double;
  1366. out m1,m2,m3,m4,skew,kurtosis : float);
  1367. begin
  1368. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1369. end;
  1370. procedure momentskewkurtosis(
  1371. const data: pdouble;
  1372. Const N: integer;
  1373. out m1: float;
  1374. out m2: float;
  1375. out m3: float;
  1376. out m4: float;
  1377. out skew: float;
  1378. out kurtosis: float
  1379. );
  1380. var
  1381. i: SizeInt;
  1382. value : pdouble;
  1383. deviation, deviation2: double;
  1384. reciprocalN: float;
  1385. begin
  1386. m1 := 0;
  1387. reciprocalN := 1/N;
  1388. value := data;
  1389. for i := 0 to N-1 do
  1390. begin
  1391. m1 := m1 + value^;
  1392. inc(value);
  1393. end;
  1394. m1 := reciprocalN * m1;
  1395. m2 := 0;
  1396. m3 := 0;
  1397. m4 := 0;
  1398. value := data;
  1399. for i := 0 to N-1 do
  1400. begin
  1401. deviation := (value^-m1);
  1402. deviation2 := deviation * deviation;
  1403. m2 := m2 + deviation2;
  1404. m3 := m3 + deviation2 * deviation;
  1405. m4 := m4 + deviation2 * deviation2;
  1406. inc(value);
  1407. end;
  1408. m2 := reciprocalN * m2;
  1409. m3 := reciprocalN * m3;
  1410. m4 := reciprocalN * m4;
  1411. skew := m3 / (sqrt(m2)*m2);
  1412. kurtosis := m4 / (m2 * m2);
  1413. end;
  1414. function norm(const data : array of Double) : float; inline;
  1415. begin
  1416. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  1417. end;
  1418. function norm(const data : PDouble; Const N : Integer) : float;
  1419. begin
  1420. norm:=sqrt(sumofsquares(data,N));
  1421. end;
  1422. {$endif FPC_HAS_TYPE_DOUBLE}
  1423. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1424. procedure MeanAndTotalVariance
  1425. (const data: PExtended; N: LongInt; var mu, variance: float);
  1426. var i: SizeInt;
  1427. begin
  1428. mu := Mean( data, N );
  1429. variance := 0;
  1430. for i := 0 to N - 1 do
  1431. variance := variance + Sqr( data[i] - mu );
  1432. end;
  1433. function stddev(const data : array of Extended) : float; inline;
  1434. begin
  1435. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  1436. end;
  1437. function stddev(const data : PExtended; Const N : Integer) : float;
  1438. begin
  1439. StdDev:=Sqrt(Variance(Data,N));
  1440. end;
  1441. procedure meanandstddev(const data : array of Extended;
  1442. var mean,stddev : float); inline;
  1443. begin
  1444. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  1445. end;
  1446. procedure meanandstddev
  1447. ( const data: PExtended;
  1448. const N: Longint;
  1449. var mean,
  1450. stdDev: Float
  1451. );
  1452. var totalVariance: float;
  1453. begin
  1454. MeanAndTotalVariance( data, N, mean, totalVariance );
  1455. if N < 2 then stdDev := 0
  1456. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1457. end;
  1458. function variance(const data : array of Extended) : float; inline;
  1459. begin
  1460. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  1461. end;
  1462. function variance(const data : PExtended; Const N : Integer) : float;
  1463. begin
  1464. If N=1 then
  1465. Result:=0
  1466. else
  1467. Result:=TotalVariance(Data,N)/(N-1);
  1468. end;
  1469. function totalvariance(const data : array of Extended) : float; inline;
  1470. begin
  1471. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  1472. end;
  1473. function totalvariance(const data : PExtended;Const N : Integer) : float;
  1474. var mu: float;
  1475. begin
  1476. MeanAndTotalVariance( data, N, mu, result );
  1477. end;
  1478. function popnstddev(const data : array of Extended) : float;
  1479. begin
  1480. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  1481. end;
  1482. function popnstddev(const data : PExtended; Const N : Integer) : float;
  1483. begin
  1484. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1485. end;
  1486. function popnvariance(const data : array of Extended) : float; inline;
  1487. begin
  1488. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  1489. end;
  1490. function popnvariance(const data : PExtended; Const N : Integer) : float;
  1491. begin
  1492. PopnVariance:=TotalVariance(Data,N)/N;
  1493. end;
  1494. procedure momentskewkurtosis(const data : array of Extended;
  1495. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1496. begin
  1497. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1498. end;
  1499. procedure momentskewkurtosis(
  1500. const data: pExtended;
  1501. Const N: Integer;
  1502. out m1: float;
  1503. out m2: float;
  1504. out m3: float;
  1505. out m4: float;
  1506. out skew: float;
  1507. out kurtosis: float
  1508. );
  1509. var
  1510. i: integer;
  1511. value : pextended;
  1512. deviation, deviation2: extended;
  1513. reciprocalN: float;
  1514. begin
  1515. m1 := 0;
  1516. reciprocalN := 1/N;
  1517. value := data;
  1518. for i := 0 to N-1 do
  1519. begin
  1520. m1 := m1 + value^;
  1521. inc(value);
  1522. end;
  1523. m1 := reciprocalN * m1;
  1524. m2 := 0;
  1525. m3 := 0;
  1526. m4 := 0;
  1527. value := data;
  1528. for i := 0 to N-1 do
  1529. begin
  1530. deviation := (value^-m1);
  1531. deviation2 := deviation * deviation;
  1532. m2 := m2 + deviation2;
  1533. m3 := m3 + deviation2 * deviation;
  1534. m4 := m4 + deviation2 * deviation2;
  1535. inc(value);
  1536. end;
  1537. m2 := reciprocalN * m2;
  1538. m3 := reciprocalN * m3;
  1539. m4 := reciprocalN * m4;
  1540. skew := m3 / (sqrt(m2)*m2);
  1541. kurtosis := m4 / (m2 * m2);
  1542. end;
  1543. function norm(const data : array of Extended) : float; inline;
  1544. begin
  1545. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  1546. end;
  1547. function norm(const data : PExtended; Const N : Integer) : float;
  1548. begin
  1549. norm:=sqrt(sumofsquares(data,N));
  1550. end;
  1551. {$endif FPC_HAS_TYPE_EXTENDED}
  1552. function MinIntValue(const Data: array of Integer): Integer;
  1553. var
  1554. I: SizeInt;
  1555. begin
  1556. Result := Data[Low(Data)];
  1557. For I := Succ(Low(Data)) To High(Data) Do
  1558. If Data[I] < Result Then Result := Data[I];
  1559. end;
  1560. function MaxIntValue(const Data: array of Integer): Integer;
  1561. var
  1562. I: SizeInt;
  1563. begin
  1564. Result := Data[Low(Data)];
  1565. For I := Succ(Low(Data)) To High(Data) Do
  1566. If Data[I] > Result Then Result := Data[I];
  1567. end;
  1568. function MinValue(const Data: array of Integer): Integer; inline;
  1569. begin
  1570. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  1571. end;
  1572. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  1573. var
  1574. I: SizeInt;
  1575. begin
  1576. Result := Data[0];
  1577. For I := 1 To N-1 do
  1578. If Data[I] < Result Then Result := Data[I];
  1579. end;
  1580. function MaxValue(const Data: array of Integer): Integer; inline;
  1581. begin
  1582. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  1583. end;
  1584. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  1585. var
  1586. i : SizeInt;
  1587. begin
  1588. { get an initial value }
  1589. maxvalue:=data[0];
  1590. for i:=1 to N-1 do
  1591. if data[i]>maxvalue then
  1592. maxvalue:=data[i];
  1593. end;
  1594. {$ifdef FPC_HAS_TYPE_SINGLE}
  1595. function minvalue(const data : array of Single) : Single; inline;
  1596. begin
  1597. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  1598. end;
  1599. function minvalue(const data : PSingle; Const N : Integer) : Single;
  1600. var
  1601. i : SizeInt;
  1602. begin
  1603. { get an initial value }
  1604. minvalue:=data[0];
  1605. for i:=1 to N-1 do
  1606. if data[i]<minvalue then
  1607. minvalue:=data[i];
  1608. end;
  1609. function maxvalue(const data : array of Single) : Single; inline;
  1610. begin
  1611. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  1612. end;
  1613. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  1614. var
  1615. i : SizeInt;
  1616. begin
  1617. { get an initial value }
  1618. maxvalue:=data[0];
  1619. for i:=1 to N-1 do
  1620. if data[i]>maxvalue then
  1621. maxvalue:=data[i];
  1622. end;
  1623. {$endif FPC_HAS_TYPE_SINGLE}
  1624. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1625. function minvalue(const data : array of Double) : Double; inline;
  1626. begin
  1627. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  1628. end;
  1629. function minvalue(const data : PDouble; Const N : Integer) : Double;
  1630. var
  1631. i : SizeInt;
  1632. begin
  1633. { get an initial value }
  1634. minvalue:=data[0];
  1635. for i:=1 to N-1 do
  1636. if data[i]<minvalue then
  1637. minvalue:=data[i];
  1638. end;
  1639. function maxvalue(const data : array of Double) : Double; inline;
  1640. begin
  1641. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  1642. end;
  1643. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  1644. var
  1645. i : SizeInt;
  1646. begin
  1647. { get an initial value }
  1648. maxvalue:=data[0];
  1649. for i:=1 to N-1 do
  1650. if data[i]>maxvalue then
  1651. maxvalue:=data[i];
  1652. end;
  1653. {$endif FPC_HAS_TYPE_DOUBLE}
  1654. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1655. function minvalue(const data : array of Extended) : Extended; inline;
  1656. begin
  1657. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  1658. end;
  1659. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  1660. var
  1661. i : SizeInt;
  1662. begin
  1663. { get an initial value }
  1664. minvalue:=data[0];
  1665. for i:=1 to N-1 do
  1666. if data[i]<minvalue then
  1667. minvalue:=data[i];
  1668. end;
  1669. function maxvalue(const data : array of Extended) : Extended; inline;
  1670. begin
  1671. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  1672. end;
  1673. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  1674. var
  1675. i : SizeInt;
  1676. begin
  1677. { get an initial value }
  1678. maxvalue:=data[0];
  1679. for i:=1 to N-1 do
  1680. if data[i]>maxvalue then
  1681. maxvalue:=data[i];
  1682. end;
  1683. {$endif FPC_HAS_TYPE_EXTENDED}
  1684. function Min(a, b: Integer): Integer;inline;
  1685. begin
  1686. if a < b then
  1687. Result := a
  1688. else
  1689. Result := b;
  1690. end;
  1691. function Max(a, b: Integer): Integer;inline;
  1692. begin
  1693. if a > b then
  1694. Result := a
  1695. else
  1696. Result := b;
  1697. end;
  1698. {
  1699. function Min(a, b: Cardinal): Cardinal;inline;
  1700. begin
  1701. if a < b then
  1702. Result := a
  1703. else
  1704. Result := b;
  1705. end;
  1706. function Max(a, b: Cardinal): Cardinal;inline;
  1707. begin
  1708. if a > b then
  1709. Result := a
  1710. else
  1711. Result := b;
  1712. end;
  1713. }
  1714. function Min(a, b: Int64): Int64;inline;
  1715. begin
  1716. if a < b then
  1717. Result := a
  1718. else
  1719. Result := b;
  1720. end;
  1721. function Max(a, b: Int64): Int64;inline;
  1722. begin
  1723. if a > b then
  1724. Result := a
  1725. else
  1726. Result := b;
  1727. end;
  1728. function Min(a, b: QWord): QWord; inline;
  1729. begin
  1730. if a < b then
  1731. Result := a
  1732. else
  1733. Result := b;
  1734. end;
  1735. function Max(a, b: QWord): Qword;inline;
  1736. begin
  1737. if a > b then
  1738. Result := a
  1739. else
  1740. Result := b;
  1741. end;
  1742. {$ifdef FPC_HAS_TYPE_SINGLE}
  1743. function Min(a, b: Single): Single;inline;
  1744. begin
  1745. if a < b then
  1746. Result := a
  1747. else
  1748. Result := b;
  1749. end;
  1750. function Max(a, b: Single): Single;inline;
  1751. begin
  1752. if a > b then
  1753. Result := a
  1754. else
  1755. Result := b;
  1756. end;
  1757. {$endif FPC_HAS_TYPE_SINGLE}
  1758. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1759. function Min(a, b: Double): Double;inline;
  1760. begin
  1761. if a < b then
  1762. Result := a
  1763. else
  1764. Result := b;
  1765. end;
  1766. function Max(a, b: Double): Double;inline;
  1767. begin
  1768. if a > b then
  1769. Result := a
  1770. else
  1771. Result := b;
  1772. end;
  1773. {$endif FPC_HAS_TYPE_DOUBLE}
  1774. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1775. function Min(a, b: Extended): Extended;inline;
  1776. begin
  1777. if a < b then
  1778. Result := a
  1779. else
  1780. Result := b;
  1781. end;
  1782. function Max(a, b: Extended): Extended;inline;
  1783. begin
  1784. if a > b then
  1785. Result := a
  1786. else
  1787. Result := b;
  1788. end;
  1789. {$endif FPC_HAS_TYPE_EXTENDED}
  1790. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  1791. begin
  1792. Result:=(AValue>=AMin) and (AValue<=AMax);
  1793. end;
  1794. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  1795. begin
  1796. Result:=(AValue>=AMin) and (AValue<=AMax);
  1797. end;
  1798. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1799. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  1800. begin
  1801. Result:=(AValue>=AMin) and (AValue<=AMax);
  1802. end;
  1803. {$endif FPC_HAS_TYPE_DOUBLE}
  1804. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  1805. begin
  1806. Result:=AValue;
  1807. If Result<AMin then
  1808. Result:=AMin;
  1809. if Result>AMax then
  1810. Result:=AMax;
  1811. end;
  1812. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  1813. begin
  1814. Result:=AValue;
  1815. If Result<AMin then
  1816. Result:=AMin;
  1817. if Result>AMax then
  1818. Result:=AMax;
  1819. end;
  1820. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1821. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  1822. begin
  1823. Result:=AValue;
  1824. If Result<AMin then
  1825. Result:=AMin;
  1826. if Result>AMax then
  1827. Result:=AMax;
  1828. end;
  1829. {$endif FPC_HAS_TYPE_DOUBLE}
  1830. Const
  1831. EZeroResolution = 1E-16;
  1832. DZeroResolution = 1E-12;
  1833. SZeroResolution = 1E-4;
  1834. function IsZero(const A: Single; Epsilon: Single): Boolean;
  1835. begin
  1836. if (Epsilon=0) then
  1837. Epsilon:=SZeroResolution;
  1838. Result:=Abs(A)<=Epsilon;
  1839. end;
  1840. function IsZero(const A: Single): Boolean;inline;
  1841. begin
  1842. Result:=IsZero(A,single(SZeroResolution));
  1843. end;
  1844. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1845. function IsZero(const A: Double; Epsilon: Double): Boolean;
  1846. begin
  1847. if (Epsilon=0) then
  1848. Epsilon:=DZeroResolution;
  1849. Result:=Abs(A)<=Epsilon;
  1850. end;
  1851. function IsZero(const A: Double): Boolean;inline;
  1852. begin
  1853. Result:=IsZero(A,DZeroResolution);
  1854. end;
  1855. {$endif FPC_HAS_TYPE_DOUBLE}
  1856. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1857. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  1858. begin
  1859. if (Epsilon=0) then
  1860. Epsilon:=EZeroResolution;
  1861. Result:=Abs(A)<=Epsilon;
  1862. end;
  1863. function IsZero(const A: Extended): Boolean;inline;
  1864. begin
  1865. Result:=IsZero(A,EZeroResolution);
  1866. end;
  1867. {$endif FPC_HAS_TYPE_EXTENDED}
  1868. type
  1869. TSplitDouble = packed record
  1870. cards: Array[0..1] of cardinal;
  1871. end;
  1872. TSplitExtended = packed record
  1873. cards: Array[0..1] of cardinal;
  1874. w: word;
  1875. end;
  1876. function IsNan(const d : Single): Boolean; overload;
  1877. begin
  1878. result:=(longword(d) and $7fffffff)>$7f800000;
  1879. end;
  1880. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1881. function IsNan(const d : Double): Boolean;
  1882. var
  1883. fraczero, expMaximal: boolean;
  1884. begin
  1885. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1886. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1887. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1888. (TSplitDouble(d).cards[1] = 0);
  1889. {$else FPC_BIG_ENDIAN}
  1890. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1891. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1892. (TSplitDouble(d).cards[0] = 0);
  1893. {$endif FPC_BIG_ENDIAN}
  1894. Result:=expMaximal and not(fraczero);
  1895. end;
  1896. {$endif FPC_HAS_TYPE_DOUBLE}
  1897. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1898. function IsNan(const d : Extended): Boolean; overload;
  1899. var
  1900. fraczero, expMaximal: boolean;
  1901. begin
  1902. {$ifdef FPC_BIG_ENDIAN}
  1903. {$error no support for big endian extended type yet}
  1904. {$else FPC_BIG_ENDIAN}
  1905. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  1906. fraczero := (TSplitExtended(d).cards[0] = 0) and
  1907. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  1908. {$endif FPC_BIG_ENDIAN}
  1909. Result:=expMaximal and not(fraczero);
  1910. end;
  1911. {$endif FPC_HAS_TYPE_EXTENDED}
  1912. function IsInfinite(const d : Single): Boolean; overload;
  1913. begin
  1914. result:=(longword(d) and $7fffffff)=$7f800000;
  1915. end;
  1916. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1917. function IsInfinite(const d : Double): Boolean; overload;
  1918. var
  1919. fraczero, expMaximal: boolean;
  1920. begin
  1921. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1922. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1923. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1924. (TSplitDouble(d).cards[1] = 0);
  1925. {$else FPC_BIG_ENDIAN}
  1926. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1927. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1928. (TSplitDouble(d).cards[0] = 0);
  1929. {$endif FPC_BIG_ENDIAN}
  1930. Result:=expMaximal and fraczero;
  1931. end;
  1932. {$endif FPC_HAS_TYPE_DOUBLE}
  1933. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1934. function IsInfinite(const d : Extended): Boolean; overload;
  1935. var
  1936. fraczero, expMaximal: boolean;
  1937. begin
  1938. {$ifdef FPC_BIG_ENDIAN}
  1939. {$error no support for big endian extended type yet}
  1940. {$else FPC_BIG_ENDIAN}
  1941. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  1942. fraczero := (TSplitExtended(d).cards[0] = 0) and
  1943. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  1944. {$endif FPC_BIG_ENDIAN}
  1945. Result:=expMaximal and fraczero;
  1946. end;
  1947. {$endif FPC_HAS_TYPE_EXTENDED}
  1948. function copysign(x,y: float): float;
  1949. begin
  1950. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  1951. {$error copysign not yet implemented for float128}
  1952. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  1953. TSplitExtended(x).w:=(TSplitExtended(x).w and $7fff) or (TSplitExtended(y).w and $8000);
  1954. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  1955. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1956. TSplitDouble(x).cards[0]:=(TSplitDouble(x).cards[0] and $7fffffff) or (TSplitDouble(y).cards[0] and longword($80000000));
  1957. {$else}
  1958. TSplitDouble(x).cards[1]:=(TSplitDouble(x).cards[1] and $7fffffff) or (TSplitDouble(y).cards[1] and longword($80000000));
  1959. {$endif}
  1960. {$else}
  1961. longword(x):=longword(x and $7fffffff) or (longword(y) and longword($80000000));
  1962. {$endif}
  1963. result:=x;
  1964. end;
  1965. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1966. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1967. begin
  1968. if (Epsilon=0) then
  1969. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1970. if (A>B) then
  1971. Result:=((A-B)<=Epsilon)
  1972. else
  1973. Result:=((B-A)<=Epsilon);
  1974. end;
  1975. function SameValue(const A, B: Extended): Boolean;inline;
  1976. begin
  1977. Result:=SameValue(A,B,0.0);
  1978. end;
  1979. {$endif FPC_HAS_TYPE_EXTENDED}
  1980. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1981. function SameValue(const A, B: Double): Boolean;inline;
  1982. begin
  1983. Result:=SameValue(A,B,0.0);
  1984. end;
  1985. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1986. begin
  1987. if (Epsilon=0) then
  1988. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1989. if (A>B) then
  1990. Result:=((A-B)<=Epsilon)
  1991. else
  1992. Result:=((B-A)<=Epsilon);
  1993. end;
  1994. {$endif FPC_HAS_TYPE_DOUBLE}
  1995. function SameValue(const A, B: Single): Boolean;inline;
  1996. begin
  1997. Result:=SameValue(A,B,0);
  1998. end;
  1999. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  2000. begin
  2001. if (Epsilon=0) then
  2002. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  2003. if (A>B) then
  2004. Result:=((A-B)<=Epsilon)
  2005. else
  2006. Result:=((B-A)<=Epsilon);
  2007. end;
  2008. // Some CPUs probably allow a faster way of doing this in a single operation...
  2009. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  2010. {$ifndef FPC_MATH_HAS_DIVMOD}
  2011. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  2012. begin
  2013. if Dividend < 0 then
  2014. begin
  2015. { Use DivMod with >=0 dividend }
  2016. Dividend:=-Dividend;
  2017. { The documented behavior of Pascal's div/mod operators and DivMod
  2018. on negative dividends is to return Result closer to zero and
  2019. a negative Remainder. Which means that we can just negate both
  2020. Result and Remainder, and all it's Ok. }
  2021. Result:=-(Dividend Div Divisor);
  2022. Remainder:=-(Dividend+(Result*Divisor));
  2023. end
  2024. else
  2025. begin
  2026. Result:=Dividend Div Divisor;
  2027. Remainder:=Dividend-(Result*Divisor);
  2028. end;
  2029. end;
  2030. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  2031. begin
  2032. if Dividend < 0 then
  2033. begin
  2034. { Use DivMod with >=0 dividend }
  2035. Dividend:=-Dividend;
  2036. { The documented behavior of Pascal's div/mod operators and DivMod
  2037. on negative dividends is to return Result closer to zero and
  2038. a negative Remainder. Which means that we can just negate both
  2039. Result and Remainder, and all it's Ok. }
  2040. Result:=-(Dividend Div Divisor);
  2041. Remainder:=-(Dividend+(Result*Divisor));
  2042. end
  2043. else
  2044. begin
  2045. Result:=Dividend Div Divisor;
  2046. Remainder:=Dividend-(Result*Divisor);
  2047. end;
  2048. end;
  2049. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  2050. begin
  2051. Result:=Dividend Div Divisor;
  2052. Remainder:=Dividend-(Result*Divisor);
  2053. end;
  2054. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  2055. begin
  2056. if Dividend < 0 then
  2057. begin
  2058. { Use DivMod with >=0 dividend }
  2059. Dividend:=-Dividend;
  2060. { The documented behavior of Pascal's div/mod operators and DivMod
  2061. on negative dividends is to return Result closer to zero and
  2062. a negative Remainder. Which means that we can just negate both
  2063. Result and Remainder, and all it's Ok. }
  2064. Result:=-(Dividend Div Divisor);
  2065. Remainder:=-(Dividend+(Result*Divisor));
  2066. end
  2067. else
  2068. begin
  2069. Result:=Dividend Div Divisor;
  2070. Remainder:=Dividend-(Result*Divisor);
  2071. end;
  2072. end;
  2073. {$endif FPC_MATH_HAS_DIVMOD}
  2074. { Floating point modulo}
  2075. {$ifdef FPC_HAS_TYPE_SINGLE}
  2076. function FMod(const a, b: Single): Single;inline;overload;
  2077. begin
  2078. result:= a-b * Int(a/b);
  2079. end;
  2080. {$endif FPC_HAS_TYPE_SINGLE}
  2081. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2082. function FMod(const a, b: Double): Double;inline;overload;
  2083. begin
  2084. result:= a-b * Int(a/b);
  2085. end;
  2086. {$endif FPC_HAS_TYPE_DOUBLE}
  2087. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2088. function FMod(const a, b: Extended): Extended;inline;overload;
  2089. begin
  2090. result:= a-b * Int(a/b);
  2091. end;
  2092. {$endif FPC_HAS_TYPE_EXTENDED}
  2093. operator mod(const a,b:float) c:float;inline;
  2094. begin
  2095. c:= a-b * Int(a/b);
  2096. if SameValue(abs(c),abs(b)) then
  2097. c:=0.0;
  2098. end;
  2099. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  2100. begin
  2101. if val then result:=iftrue else result:=iffalse;
  2102. end;
  2103. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  2104. begin
  2105. if val then result:=iftrue else result:=iffalse;
  2106. end;
  2107. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  2108. begin
  2109. if val then result:=iftrue else result:=iffalse;
  2110. end;
  2111. // dilemma here. asm can do the two comparisons in one go?
  2112. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  2113. function CompareValue(const A, B : Integer): TValueRelationship;
  2114. begin
  2115. result:=GreaterThanValue;
  2116. if a=b then
  2117. result:=EqualsValue
  2118. else
  2119. if a<b then
  2120. result:=LessThanValue;
  2121. end;
  2122. function CompareValue(const A, B: Int64): TValueRelationship;
  2123. begin
  2124. result:=GreaterThanValue;
  2125. if a=b then
  2126. result:=EqualsValue
  2127. else
  2128. if a<b then
  2129. result:=LessThanValue;
  2130. end;
  2131. function CompareValue(const A, B: QWord): TValueRelationship;
  2132. begin
  2133. result:=GreaterThanValue;
  2134. if a=b then
  2135. result:=EqualsValue
  2136. else
  2137. if a<b then
  2138. result:=LessThanValue;
  2139. end;
  2140. {$ifdef FPC_HAS_TYPE_SINGLE}
  2141. function CompareValue(const A, B: Single; delta: Single = 0.0): TValueRelationship;
  2142. begin
  2143. result:=GreaterThanValue;
  2144. if abs(a-b)<=delta then
  2145. result:=EqualsValue
  2146. else
  2147. if a<b then
  2148. result:=LessThanValue;
  2149. end;
  2150. {$endif}
  2151. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2152. function CompareValue(const A, B: Double; delta: Double = 0.0): TValueRelationship;
  2153. begin
  2154. result:=GreaterThanValue;
  2155. if abs(a-b)<=delta then
  2156. result:=EqualsValue
  2157. else
  2158. if a<b then
  2159. result:=LessThanValue;
  2160. end;
  2161. {$endif}
  2162. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2163. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  2164. begin
  2165. result:=GreaterThanValue;
  2166. if abs(a-b)<=delta then
  2167. result:=EqualsValue
  2168. else
  2169. if a<b then
  2170. result:=LessThanValue;
  2171. end;
  2172. {$endif}
  2173. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2174. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  2175. var
  2176. RV : Double;
  2177. begin
  2178. RV:=IntPower(10,Digits);
  2179. Result:=Round(AValue/RV)*RV;
  2180. end;
  2181. {$endif}
  2182. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2183. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  2184. var
  2185. RV : Extended;
  2186. begin
  2187. RV:=IntPower(10,Digits);
  2188. Result:=Round(AValue/RV)*RV;
  2189. end;
  2190. {$endif}
  2191. {$ifdef FPC_HAS_TYPE_SINGLE}
  2192. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  2193. var
  2194. RV : Single;
  2195. begin
  2196. RV:=IntPower(10,Digits);
  2197. Result:=Round(AValue/RV)*RV;
  2198. end;
  2199. {$endif}
  2200. {$ifdef FPC_HAS_TYPE_SINGLE}
  2201. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  2202. var
  2203. RV : Single;
  2204. begin
  2205. RV := IntPower(10, -Digits);
  2206. if AValue < 0 then
  2207. Result := Int((AValue*RV) - 0.5)/RV
  2208. else
  2209. Result := Int((AValue*RV) + 0.5)/RV;
  2210. end;
  2211. {$endif}
  2212. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2213. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  2214. var
  2215. RV : Double;
  2216. begin
  2217. RV := IntPower(10, -Digits);
  2218. if AValue < 0 then
  2219. Result := Int((AValue*RV) - 0.5)/RV
  2220. else
  2221. Result := Int((AValue*RV) + 0.5)/RV;
  2222. end;
  2223. {$endif}
  2224. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2225. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  2226. var
  2227. RV : Extended;
  2228. begin
  2229. RV := IntPower(10, -Digits);
  2230. if AValue < 0 then
  2231. Result := Int((AValue*RV) - 0.5)/RV
  2232. else
  2233. Result := Int((AValue*RV) + 0.5)/RV;
  2234. end;
  2235. {$endif}
  2236. function RandomFrom(const AValues: array of Double): Double; overload;
  2237. begin
  2238. result:=AValues[random(High(AValues)+1)];
  2239. end;
  2240. function RandomFrom(const AValues: array of Integer): Integer; overload;
  2241. begin
  2242. result:=AValues[random(High(AValues)+1)];
  2243. end;
  2244. function RandomFrom(const AValues: array of Int64): Int64; overload;
  2245. begin
  2246. result:=AValues[random(High(AValues)+1)];
  2247. end;
  2248. {$if FPC_FULLVERSION >=30101}
  2249. generic function RandomFrom<T>(const AValues:array of T):T;
  2250. begin
  2251. result:=AValues[random(High(AValues)+1)];
  2252. end;
  2253. {$endif}
  2254. function FutureValue(ARate: Float; NPeriods: Integer;
  2255. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  2256. var
  2257. q, qn, factor: Float;
  2258. begin
  2259. if ARate = 0 then
  2260. Result := -APresentValue - APayment * NPeriods
  2261. else begin
  2262. q := 1.0 + ARate;
  2263. qn := power(q, NPeriods);
  2264. factor := (qn - 1) / (q - 1);
  2265. if APaymentTime = ptStartOfPeriod then
  2266. factor := factor * q;
  2267. Result := -(APresentValue * qn + APayment*factor);
  2268. end;
  2269. end;
  2270. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  2271. APaymentTime: TPaymentTime): Float;
  2272. { The interest rate cannot be calculated analytically. We solve the equation
  2273. numerically by means of the Newton method:
  2274. - guess value for the interest reate
  2275. - calculate at which interest rate the tangent of the curve fv(rate)
  2276. (straight line!) has the requested future vale.
  2277. - use this rate for the next iteration. }
  2278. const
  2279. DELTA = 0.001;
  2280. EPS = 1E-9; // required precision of interest rate (after typ. 6 iterations)
  2281. MAXIT = 20; // max iteration count to protect agains non-convergence
  2282. var
  2283. r1, r2, dr: Float;
  2284. fv1, fv2: Float;
  2285. iteration: Integer;
  2286. begin
  2287. iteration := 0;
  2288. r1 := 0.05; // inital guess
  2289. repeat
  2290. r2 := r1 + DELTA;
  2291. fv1 := FutureValue(r1, NPeriods, APayment, APresentValue, APaymentTime);
  2292. fv2 := FutureValue(r2, NPeriods, APayment, APresentValue, APaymentTime);
  2293. dr := (AFutureValue - fv1) / (fv2 - fv1) * delta; // tangent at fv(r)
  2294. r1 := r1 + dr; // next guess
  2295. inc(iteration);
  2296. until (abs(dr) < EPS) or (iteration >= MAXIT);
  2297. Result := r1;
  2298. end;
  2299. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  2300. APaymentTime: TPaymentTime): Float;
  2301. { Solve the cash flow equation (1) for q^n and take the logarithm }
  2302. var
  2303. q, x1, x2: Float;
  2304. begin
  2305. if ARate = 0 then
  2306. Result := -(APresentValue + AFutureValue) / APayment
  2307. else begin
  2308. q := 1.0 + ARate;
  2309. if APaymentTime = ptStartOfPeriod then
  2310. APayment := APayment * q;
  2311. x1 := APayment - AFutureValue * ARate;
  2312. x2 := APayment + APresentValue * ARate;
  2313. if (x2 = 0) // we have to divide by x2
  2314. or (sign(x1) * sign(x2) < 0) // the argument of the log is negative
  2315. then
  2316. Result := Infinity
  2317. else begin
  2318. Result := ln(x1/x2) / ln(q);
  2319. end;
  2320. end;
  2321. end;
  2322. function Payment(ARate: Float; NPeriods: Integer;
  2323. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  2324. var
  2325. q, qn, factor: Float;
  2326. begin
  2327. if ARate = 0 then
  2328. Result := -(AFutureValue + APresentValue) / NPeriods
  2329. else begin
  2330. q := 1.0 + ARate;
  2331. qn := power(q, NPeriods);
  2332. factor := (qn - 1) / (q - 1);
  2333. if APaymentTime = ptStartOfPeriod then
  2334. factor := factor * q;
  2335. Result := -(AFutureValue + APresentValue * qn) / factor;
  2336. end;
  2337. end;
  2338. function PresentValue(ARate: Float; NPeriods: Integer;
  2339. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  2340. var
  2341. q, qn, factor: Float;
  2342. begin
  2343. if ARate = 0.0 then
  2344. Result := -AFutureValue - APayment * NPeriods
  2345. else begin
  2346. q := 1.0 + ARate;
  2347. qn := power(q, NPeriods);
  2348. factor := (qn - 1) / (q - 1);
  2349. if APaymentTime = ptStartOfPeriod then
  2350. factor := factor * q;
  2351. Result := -(AFutureValue + APayment*factor) / qn;
  2352. end;
  2353. end;
  2354. {$else}
  2355. implementation
  2356. {$endif FPUNONE}
  2357. end.