math.pp 69 KB

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