math.pp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745
  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. { Population (aka uncorrected) variance and standard deviation }
  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. { Population (aka uncorrected) variance and standard deviation }
  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. { Population (aka uncorrected) variance and standard deviation }
  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. procedure MeanAndTotalVariance
  1147. (const data: PSingle; N: LongInt; var mu, variance: float);
  1148. var i: LongInt;
  1149. begin
  1150. mu := Mean( data, N );
  1151. variance := 0;
  1152. for i := 0 to N - 1 do
  1153. variance := variance + Sqr( data[i] - mu );
  1154. end;
  1155. function stddev(const data : array of Single) : float; inline;
  1156. begin
  1157. Result:=Stddev(PSingle(@Data[0]),High(Data)+1);
  1158. end;
  1159. function stddev(const data : PSingle; Const N : Integer) : float;
  1160. begin
  1161. StdDev:=Sqrt(Variance(Data,N));
  1162. end;
  1163. procedure meanandstddev(const data : array of Single;
  1164. var mean,stddev : float); inline;
  1165. begin
  1166. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  1167. end;
  1168. procedure meanandstddev
  1169. ( const data: PSingle;
  1170. const N: Longint;
  1171. var mean,
  1172. stdDev: Float
  1173. );
  1174. var totalVariance: float;
  1175. begin
  1176. MeanAndTotalVariance( data, N, mean, totalVariance );
  1177. if N < 2 then stdDev := 0
  1178. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1179. end;
  1180. function variance(const data : array of Single) : float; inline;
  1181. begin
  1182. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  1183. end;
  1184. function variance(const data : PSingle; Const N : Integer) : float;
  1185. begin
  1186. If N=1 then
  1187. Result:=0
  1188. else
  1189. Result:=TotalVariance(Data,N)/(N-1);
  1190. end;
  1191. function totalvariance(const data : array of Single) : float; inline;
  1192. begin
  1193. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  1194. end;
  1195. function totalvariance(const data : PSingle; const N : Integer) : float;
  1196. var mu: float;
  1197. begin
  1198. MeanAndTotalVariance( data, N, mu, result );
  1199. end;
  1200. function popnstddev(const data : array of Single) : float;
  1201. begin
  1202. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  1203. end;
  1204. function popnstddev(const data : PSingle; Const N : Integer) : float;
  1205. begin
  1206. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1207. end;
  1208. function popnvariance(const data : array of Single) : float; inline;
  1209. begin
  1210. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  1211. end;
  1212. function popnvariance(const data : PSingle; Const N : Integer) : float;
  1213. begin
  1214. PopnVariance:=TotalVariance(Data,N)/N;
  1215. end;
  1216. procedure momentskewkurtosis(const data : array of single;
  1217. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1218. begin
  1219. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1220. end;
  1221. procedure momentskewkurtosis(
  1222. const data: pSingle;
  1223. Const N: integer;
  1224. out m1: float;
  1225. out m2: float;
  1226. out m3: float;
  1227. out m4: float;
  1228. out skew: float;
  1229. out kurtosis: float
  1230. );
  1231. var
  1232. i: integer;
  1233. value : psingle;
  1234. deviation, deviation2: single;
  1235. reciprocalN: float;
  1236. begin
  1237. m1 := 0;
  1238. reciprocalN := 1/N;
  1239. value := data;
  1240. for i := 0 to N-1 do
  1241. begin
  1242. m1 := m1 + value^;
  1243. inc(value);
  1244. end;
  1245. m1 := reciprocalN * m1;
  1246. m2 := 0;
  1247. m3 := 0;
  1248. m4 := 0;
  1249. value := data;
  1250. for i := 0 to N-1 do
  1251. begin
  1252. deviation := (value^-m1);
  1253. deviation2 := deviation * deviation;
  1254. m2 := m2 + deviation2;
  1255. m3 := m3 + deviation2 * deviation;
  1256. m4 := m4 + deviation2 * deviation2;
  1257. inc(value);
  1258. end;
  1259. m2 := reciprocalN * m2;
  1260. m3 := reciprocalN * m3;
  1261. m4 := reciprocalN * m4;
  1262. skew := m3 / (sqrt(m2)*m2);
  1263. kurtosis := m4 / (m2 * m2);
  1264. end;
  1265. function norm(const data : array of Single) : float; inline;
  1266. begin
  1267. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  1268. end;
  1269. function norm(const data : PSingle; Const N : Integer) : float;
  1270. begin
  1271. norm:=sqrt(sumofsquares(data,N));
  1272. end;
  1273. {$endif FPC_HAS_TYPE_SINGLE}
  1274. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1275. procedure MeanAndTotalVariance
  1276. (const data: PDouble; N: LongInt; var mu, variance: float);
  1277. var i: LongInt;
  1278. begin
  1279. mu := Mean( data, N );
  1280. variance := 0;
  1281. for i := 0 to N - 1 do
  1282. variance := variance + Sqr( data[i] - mu );
  1283. end;
  1284. function stddev(const data : array of Double) : float; inline;
  1285. begin
  1286. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  1287. end;
  1288. function stddev(const data : PDouble; Const N : Integer) : float;
  1289. begin
  1290. StdDev:=Sqrt(Variance(Data,N));
  1291. end;
  1292. procedure meanandstddev(const data : array of Double;
  1293. var mean,stddev : float);
  1294. begin
  1295. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  1296. end;
  1297. procedure meanandstddev
  1298. ( const data: PDouble;
  1299. const N: Longint;
  1300. var mean,
  1301. stdDev: Float
  1302. );
  1303. var totalVariance: float;
  1304. begin
  1305. MeanAndTotalVariance( data, N, mean, totalVariance );
  1306. if N < 2 then stdDev := 0
  1307. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  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 mu: float;
  1326. begin
  1327. MeanAndTotalVariance( data, N, mu, result );
  1328. end;
  1329. function popnstddev(const data : array of Double) : float;
  1330. begin
  1331. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  1332. end;
  1333. function popnstddev(const data : PDouble; Const N : Integer) : float;
  1334. begin
  1335. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1336. end;
  1337. function popnvariance(const data : array of Double) : float; inline;
  1338. begin
  1339. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  1340. end;
  1341. function popnvariance(const data : PDouble; Const N : Integer) : float;
  1342. begin
  1343. PopnVariance:=TotalVariance(Data,N)/N;
  1344. end;
  1345. procedure momentskewkurtosis(const data : array of Double;
  1346. out m1,m2,m3,m4,skew,kurtosis : float);
  1347. begin
  1348. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1349. end;
  1350. procedure momentskewkurtosis(
  1351. const data: pdouble;
  1352. Const N: integer;
  1353. out m1: float;
  1354. out m2: float;
  1355. out m3: float;
  1356. out m4: float;
  1357. out skew: float;
  1358. out kurtosis: float
  1359. );
  1360. var
  1361. i: integer;
  1362. value : pdouble;
  1363. deviation, deviation2: double;
  1364. reciprocalN: float;
  1365. begin
  1366. m1 := 0;
  1367. reciprocalN := 1/N;
  1368. value := data;
  1369. for i := 0 to N-1 do
  1370. begin
  1371. m1 := m1 + value^;
  1372. inc(value);
  1373. end;
  1374. m1 := reciprocalN * m1;
  1375. m2 := 0;
  1376. m3 := 0;
  1377. m4 := 0;
  1378. value := data;
  1379. for i := 0 to N-1 do
  1380. begin
  1381. deviation := (value^-m1);
  1382. deviation2 := deviation * deviation;
  1383. m2 := m2 + deviation2;
  1384. m3 := m3 + deviation2 * deviation;
  1385. m4 := m4 + deviation2 * deviation2;
  1386. inc(value);
  1387. end;
  1388. m2 := reciprocalN * m2;
  1389. m3 := reciprocalN * m3;
  1390. m4 := reciprocalN * m4;
  1391. skew := m3 / (sqrt(m2)*m2);
  1392. kurtosis := m4 / (m2 * m2);
  1393. end;
  1394. function norm(const data : array of Double) : float; inline;
  1395. begin
  1396. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  1397. end;
  1398. function norm(const data : PDouble; Const N : Integer) : float;
  1399. begin
  1400. norm:=sqrt(sumofsquares(data,N));
  1401. end;
  1402. {$endif FPC_HAS_TYPE_DOUBLE}
  1403. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1404. procedure MeanAndTotalVariance
  1405. (const data: PExtended; N: LongInt; var mu, variance: float);
  1406. var i: LongInt;
  1407. begin
  1408. mu := Mean( data, N );
  1409. variance := 0;
  1410. for i := 0 to N - 1 do
  1411. variance := variance + Sqr( data[i] - mu );
  1412. end;
  1413. function stddev(const data : array of Extended) : float; inline;
  1414. begin
  1415. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  1416. end;
  1417. function stddev(const data : PExtended; Const N : Integer) : float;
  1418. begin
  1419. StdDev:=Sqrt(Variance(Data,N));
  1420. end;
  1421. procedure meanandstddev(const data : array of Extended;
  1422. var mean,stddev : float); inline;
  1423. begin
  1424. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  1425. end;
  1426. procedure meanandstddev
  1427. ( const data: PExtended;
  1428. const N: Longint;
  1429. var mean,
  1430. stdDev: Float
  1431. );
  1432. var totalVariance: float;
  1433. begin
  1434. MeanAndTotalVariance( data, N, mean, totalVariance );
  1435. if N < 2 then stdDev := 0
  1436. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1437. end;
  1438. function variance(const data : array of Extended) : float; inline;
  1439. begin
  1440. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  1441. end;
  1442. function variance(const data : PExtended; Const N : Integer) : float;
  1443. begin
  1444. If N=1 then
  1445. Result:=0
  1446. else
  1447. Result:=TotalVariance(Data,N)/(N-1);
  1448. end;
  1449. function totalvariance(const data : array of Extended) : float; inline;
  1450. begin
  1451. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  1452. end;
  1453. function totalvariance(const data : PExtended;Const N : Integer) : float;
  1454. var mu: float;
  1455. begin
  1456. MeanAndTotalVariance( data, N, mu, result );
  1457. end;
  1458. function popnstddev(const data : array of Extended) : float;
  1459. begin
  1460. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  1461. end;
  1462. function popnstddev(const data : PExtended; Const N : Integer) : float;
  1463. begin
  1464. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1465. end;
  1466. function popnvariance(const data : array of Extended) : float; inline;
  1467. begin
  1468. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  1469. end;
  1470. function popnvariance(const data : PExtended; Const N : Integer) : float;
  1471. begin
  1472. PopnVariance:=TotalVariance(Data,N)/N;
  1473. end;
  1474. procedure momentskewkurtosis(const data : array of Extended;
  1475. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1476. begin
  1477. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1478. end;
  1479. procedure momentskewkurtosis(
  1480. const data: pExtended;
  1481. Const N: integer;
  1482. out m1: float;
  1483. out m2: float;
  1484. out m3: float;
  1485. out m4: float;
  1486. out skew: float;
  1487. out kurtosis: float
  1488. );
  1489. var
  1490. i: integer;
  1491. value : pextended;
  1492. deviation, deviation2: extended;
  1493. reciprocalN: float;
  1494. begin
  1495. m1 := 0;
  1496. reciprocalN := 1/N;
  1497. value := data;
  1498. for i := 0 to N-1 do
  1499. begin
  1500. m1 := m1 + value^;
  1501. inc(value);
  1502. end;
  1503. m1 := reciprocalN * m1;
  1504. m2 := 0;
  1505. m3 := 0;
  1506. m4 := 0;
  1507. value := data;
  1508. for i := 0 to N-1 do
  1509. begin
  1510. deviation := (value^-m1);
  1511. deviation2 := deviation * deviation;
  1512. m2 := m2 + deviation2;
  1513. m3 := m3 + deviation2 * deviation;
  1514. m4 := m4 + deviation2 * deviation2;
  1515. inc(value);
  1516. end;
  1517. m2 := reciprocalN * m2;
  1518. m3 := reciprocalN * m3;
  1519. m4 := reciprocalN * m4;
  1520. skew := m3 / (sqrt(m2)*m2);
  1521. kurtosis := m4 / (m2 * m2);
  1522. end;
  1523. function norm(const data : array of Extended) : float; inline;
  1524. begin
  1525. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  1526. end;
  1527. function norm(const data : PExtended; Const N : Integer) : float;
  1528. begin
  1529. norm:=sqrt(sumofsquares(data,N));
  1530. end;
  1531. {$endif FPC_HAS_TYPE_EXTENDED}
  1532. function MinIntValue(const Data: array of Integer): Integer;
  1533. var
  1534. I: Integer;
  1535. begin
  1536. Result := Data[Low(Data)];
  1537. For I := Succ(Low(Data)) To High(Data) Do
  1538. If Data[I] < Result Then Result := Data[I];
  1539. end;
  1540. function MaxIntValue(const Data: array of Integer): Integer;
  1541. var
  1542. I: Integer;
  1543. begin
  1544. Result := Data[Low(Data)];
  1545. For I := Succ(Low(Data)) To High(Data) Do
  1546. If Data[I] > Result Then Result := Data[I];
  1547. end;
  1548. function MinValue(const Data: array of Integer): Integer; inline;
  1549. begin
  1550. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  1551. end;
  1552. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  1553. var
  1554. I: Integer;
  1555. begin
  1556. Result := Data[0];
  1557. For I := 1 To N-1 do
  1558. If Data[I] < Result Then Result := Data[I];
  1559. end;
  1560. function MaxValue(const Data: array of Integer): Integer; inline;
  1561. begin
  1562. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  1563. end;
  1564. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  1565. var
  1566. i : longint;
  1567. begin
  1568. { get an initial value }
  1569. maxvalue:=data[0];
  1570. for i:=1 to N-1 do
  1571. if data[i]>maxvalue then
  1572. maxvalue:=data[i];
  1573. end;
  1574. {$ifdef FPC_HAS_TYPE_SINGLE}
  1575. function minvalue(const data : array of Single) : Single; inline;
  1576. begin
  1577. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  1578. end;
  1579. function minvalue(const data : PSingle; Const N : Integer) : Single;
  1580. var
  1581. i : longint;
  1582. begin
  1583. { get an initial value }
  1584. minvalue:=data[0];
  1585. for i:=1 to N-1 do
  1586. if data[i]<minvalue then
  1587. minvalue:=data[i];
  1588. end;
  1589. function maxvalue(const data : array of Single) : Single; inline;
  1590. begin
  1591. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  1592. end;
  1593. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  1594. var
  1595. i : longint;
  1596. begin
  1597. { get an initial value }
  1598. maxvalue:=data[0];
  1599. for i:=1 to N-1 do
  1600. if data[i]>maxvalue then
  1601. maxvalue:=data[i];
  1602. end;
  1603. {$endif FPC_HAS_TYPE_SINGLE}
  1604. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1605. function minvalue(const data : array of Double) : Double; inline;
  1606. begin
  1607. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  1608. end;
  1609. function minvalue(const data : PDouble; Const N : Integer) : Double;
  1610. var
  1611. i : longint;
  1612. begin
  1613. { get an initial value }
  1614. minvalue:=data[0];
  1615. for i:=1 to N-1 do
  1616. if data[i]<minvalue then
  1617. minvalue:=data[i];
  1618. end;
  1619. function maxvalue(const data : array of Double) : Double; inline;
  1620. begin
  1621. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  1622. end;
  1623. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  1624. var
  1625. i : longint;
  1626. begin
  1627. { get an initial value }
  1628. maxvalue:=data[0];
  1629. for i:=1 to N-1 do
  1630. if data[i]>maxvalue then
  1631. maxvalue:=data[i];
  1632. end;
  1633. {$endif FPC_HAS_TYPE_DOUBLE}
  1634. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1635. function minvalue(const data : array of Extended) : Extended; inline;
  1636. begin
  1637. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  1638. end;
  1639. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  1640. var
  1641. i : longint;
  1642. begin
  1643. { get an initial value }
  1644. minvalue:=data[0];
  1645. for i:=1 to N-1 do
  1646. if data[i]<minvalue then
  1647. minvalue:=data[i];
  1648. end;
  1649. function maxvalue(const data : array of Extended) : Extended; inline;
  1650. begin
  1651. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  1652. end;
  1653. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  1654. var
  1655. i : longint;
  1656. begin
  1657. { get an initial value }
  1658. maxvalue:=data[0];
  1659. for i:=1 to N-1 do
  1660. if data[i]>maxvalue then
  1661. maxvalue:=data[i];
  1662. end;
  1663. {$endif FPC_HAS_TYPE_EXTENDED}
  1664. function Min(a, b: Integer): Integer;inline;
  1665. begin
  1666. if a < b then
  1667. Result := a
  1668. else
  1669. Result := b;
  1670. end;
  1671. function Max(a, b: Integer): Integer;inline;
  1672. begin
  1673. if a > b then
  1674. Result := a
  1675. else
  1676. Result := b;
  1677. end;
  1678. {
  1679. function Min(a, b: Cardinal): Cardinal;inline;
  1680. begin
  1681. if a < b then
  1682. Result := a
  1683. else
  1684. Result := b;
  1685. end;
  1686. function Max(a, b: Cardinal): Cardinal;inline;
  1687. begin
  1688. if a > b then
  1689. Result := a
  1690. else
  1691. Result := b;
  1692. end;
  1693. }
  1694. function Min(a, b: Int64): Int64;inline;
  1695. begin
  1696. if a < b then
  1697. Result := a
  1698. else
  1699. Result := b;
  1700. end;
  1701. function Max(a, b: Int64): Int64;inline;
  1702. begin
  1703. if a > b then
  1704. Result := a
  1705. else
  1706. Result := b;
  1707. end;
  1708. {$ifdef FPC_HAS_TYPE_SINGLE}
  1709. function Min(a, b: Single): Single;inline;
  1710. begin
  1711. if a < b then
  1712. Result := a
  1713. else
  1714. Result := b;
  1715. end;
  1716. function Max(a, b: Single): Single;inline;
  1717. begin
  1718. if a > b then
  1719. Result := a
  1720. else
  1721. Result := b;
  1722. end;
  1723. {$endif FPC_HAS_TYPE_SINGLE}
  1724. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1725. function Min(a, b: Double): Double;inline;
  1726. begin
  1727. if a < b then
  1728. Result := a
  1729. else
  1730. Result := b;
  1731. end;
  1732. function Max(a, b: Double): Double;inline;
  1733. begin
  1734. if a > b then
  1735. Result := a
  1736. else
  1737. Result := b;
  1738. end;
  1739. {$endif FPC_HAS_TYPE_DOUBLE}
  1740. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1741. function Min(a, b: Extended): Extended;inline;
  1742. begin
  1743. if a < b then
  1744. Result := a
  1745. else
  1746. Result := b;
  1747. end;
  1748. function Max(a, b: Extended): Extended;inline;
  1749. begin
  1750. if a > b then
  1751. Result := a
  1752. else
  1753. Result := b;
  1754. end;
  1755. {$endif FPC_HAS_TYPE_EXTENDED}
  1756. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  1757. begin
  1758. Result:=(AValue>=AMin) and (AValue<=AMax);
  1759. end;
  1760. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  1761. begin
  1762. Result:=(AValue>=AMin) and (AValue<=AMax);
  1763. end;
  1764. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1765. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  1766. begin
  1767. Result:=(AValue>=AMin) and (AValue<=AMax);
  1768. end;
  1769. {$endif FPC_HAS_TYPE_DOUBLE}
  1770. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  1771. begin
  1772. Result:=AValue;
  1773. If Result<AMin then
  1774. Result:=AMin;
  1775. if Result>AMax then
  1776. Result:=AMax;
  1777. end;
  1778. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  1779. begin
  1780. Result:=AValue;
  1781. If Result<AMin then
  1782. Result:=AMin;
  1783. if Result>AMax then
  1784. Result:=AMax;
  1785. end;
  1786. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1787. function EnsureRange(const AValue, AMin, AMax: Double): Double;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. {$endif FPC_HAS_TYPE_DOUBLE}
  1796. Const
  1797. EZeroResolution = 1E-16;
  1798. DZeroResolution = 1E-12;
  1799. SZeroResolution = 1E-4;
  1800. function IsZero(const A: Single; Epsilon: Single): Boolean;
  1801. begin
  1802. if (Epsilon=0) then
  1803. Epsilon:=SZeroResolution;
  1804. Result:=Abs(A)<=Epsilon;
  1805. end;
  1806. function IsZero(const A: Single): Boolean;inline;
  1807. begin
  1808. Result:=IsZero(A,single(SZeroResolution));
  1809. end;
  1810. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1811. function IsZero(const A: Double; Epsilon: Double): Boolean;
  1812. begin
  1813. if (Epsilon=0) then
  1814. Epsilon:=DZeroResolution;
  1815. Result:=Abs(A)<=Epsilon;
  1816. end;
  1817. function IsZero(const A: Double): Boolean;inline;
  1818. begin
  1819. Result:=IsZero(A,DZeroResolution);
  1820. end;
  1821. {$endif FPC_HAS_TYPE_DOUBLE}
  1822. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1823. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  1824. begin
  1825. if (Epsilon=0) then
  1826. Epsilon:=EZeroResolution;
  1827. Result:=Abs(A)<=Epsilon;
  1828. end;
  1829. function IsZero(const A: Extended): Boolean;inline;
  1830. begin
  1831. Result:=IsZero(A,EZeroResolution);
  1832. end;
  1833. {$endif FPC_HAS_TYPE_EXTENDED}
  1834. type
  1835. TSplitDouble = packed record
  1836. cards: Array[0..1] of cardinal;
  1837. end;
  1838. TSplitExtended = packed record
  1839. cards: Array[0..1] of cardinal;
  1840. w: word;
  1841. end;
  1842. function IsNan(const d : Single): Boolean; overload;
  1843. begin
  1844. result:=(longword(d) and $7fffffff)>$7f800000;
  1845. end;
  1846. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1847. function IsNan(const d : Double): Boolean;
  1848. var
  1849. fraczero, expMaximal: boolean;
  1850. begin
  1851. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1852. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1853. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1854. (TSplitDouble(d).cards[1] = 0);
  1855. {$else FPC_BIG_ENDIAN}
  1856. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1857. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1858. (TSplitDouble(d).cards[0] = 0);
  1859. {$endif FPC_BIG_ENDIAN}
  1860. Result:=expMaximal and not(fraczero);
  1861. end;
  1862. {$endif FPC_HAS_TYPE_DOUBLE}
  1863. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1864. function IsNan(const d : Extended): Boolean; overload;
  1865. var
  1866. fraczero, expMaximal: boolean;
  1867. begin
  1868. {$ifdef FPC_BIG_ENDIAN}
  1869. {$error no support for big endian extended type yet}
  1870. {$else FPC_BIG_ENDIAN}
  1871. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  1872. fraczero := (TSplitExtended(d).cards[0] = 0) and
  1873. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  1874. {$endif FPC_BIG_ENDIAN}
  1875. Result:=expMaximal and not(fraczero);
  1876. end;
  1877. {$endif FPC_HAS_TYPE_EXTENDED}
  1878. function IsInfinite(const d : Double): Boolean;
  1879. var
  1880. fraczero, expMaximal: boolean;
  1881. begin
  1882. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1883. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1884. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1885. (TSplitDouble(d).cards[1] = 0);
  1886. {$else FPC_BIG_ENDIAN}
  1887. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1888. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1889. (TSplitDouble(d).cards[0] = 0);
  1890. {$endif FPC_BIG_ENDIAN}
  1891. Result:=expMaximal and fraczero;
  1892. end;
  1893. function copysign(x,y: float): float;
  1894. begin
  1895. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  1896. {$error copysign not yet implemented for float128}
  1897. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  1898. TSplitExtended(x).w:=(TSplitExtended(x).w and $7fff) or (TSplitExtended(y).w and $8000);
  1899. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  1900. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1901. TSplitDouble(x).cards[0]:=(TSplitDouble(x).cards[0] and $7fffffff) or (TSplitDouble(y).cards[0] and longword($80000000));
  1902. {$else}
  1903. TSplitDouble(x).cards[1]:=(TSplitDouble(x).cards[1] and $7fffffff) or (TSplitDouble(y).cards[1] and longword($80000000));
  1904. {$endif}
  1905. {$else}
  1906. longword(x):=longword(x and $7fffffff) or (longword(y) and longword($80000000));
  1907. {$endif}
  1908. result:=x;
  1909. end;
  1910. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1911. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1912. begin
  1913. if (Epsilon=0) then
  1914. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1915. if (A>B) then
  1916. Result:=((A-B)<=Epsilon)
  1917. else
  1918. Result:=((B-A)<=Epsilon);
  1919. end;
  1920. function SameValue(const A, B: Extended): Boolean;inline;
  1921. begin
  1922. Result:=SameValue(A,B,0.0);
  1923. end;
  1924. {$endif FPC_HAS_TYPE_EXTENDED}
  1925. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1926. function SameValue(const A, B: Double): Boolean;inline;
  1927. begin
  1928. Result:=SameValue(A,B,0.0);
  1929. end;
  1930. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1931. begin
  1932. if (Epsilon=0) then
  1933. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1934. if (A>B) then
  1935. Result:=((A-B)<=Epsilon)
  1936. else
  1937. Result:=((B-A)<=Epsilon);
  1938. end;
  1939. {$endif FPC_HAS_TYPE_DOUBLE}
  1940. function SameValue(const A, B: Single): Boolean;inline;
  1941. begin
  1942. Result:=SameValue(A,B,0);
  1943. end;
  1944. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  1945. begin
  1946. if (Epsilon=0) then
  1947. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  1948. if (A>B) then
  1949. Result:=((A-B)<=Epsilon)
  1950. else
  1951. Result:=((B-A)<=Epsilon);
  1952. end;
  1953. // Some CPUs probably allow a faster way of doing this in a single operation...
  1954. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  1955. {$ifndef FPC_MATH_HAS_DIVMOD}
  1956. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  1957. begin
  1958. if Dividend < 0 then
  1959. begin
  1960. { Use DivMod with >=0 dividend }
  1961. Dividend:=-Dividend;
  1962. { The documented behavior of Pascal's div/mod operators and DivMod
  1963. on negative dividends is to return Result closer to zero and
  1964. a negative Remainder. Which means that we can just negate both
  1965. Result and Remainder, and all it's Ok. }
  1966. Result:=-(Dividend Div Divisor);
  1967. Remainder:=-(Dividend+(Result*Divisor));
  1968. end
  1969. else
  1970. begin
  1971. Result:=Dividend Div Divisor;
  1972. Remainder:=Dividend-(Result*Divisor);
  1973. end;
  1974. end;
  1975. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  1976. begin
  1977. if Dividend < 0 then
  1978. begin
  1979. { Use DivMod with >=0 dividend }
  1980. Dividend:=-Dividend;
  1981. { The documented behavior of Pascal's div/mod operators and DivMod
  1982. on negative dividends is to return Result closer to zero and
  1983. a negative Remainder. Which means that we can just negate both
  1984. Result and Remainder, and all it's Ok. }
  1985. Result:=-(Dividend Div Divisor);
  1986. Remainder:=-(Dividend+(Result*Divisor));
  1987. end
  1988. else
  1989. begin
  1990. Result:=Dividend Div Divisor;
  1991. Remainder:=Dividend-(Result*Divisor);
  1992. end;
  1993. end;
  1994. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  1995. begin
  1996. Result:=Dividend Div Divisor;
  1997. Remainder:=Dividend-(Result*Divisor);
  1998. end;
  1999. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  2000. begin
  2001. if Dividend < 0 then
  2002. begin
  2003. { Use DivMod with >=0 dividend }
  2004. Dividend:=-Dividend;
  2005. { The documented behavior of Pascal's div/mod operators and DivMod
  2006. on negative dividends is to return Result closer to zero and
  2007. a negative Remainder. Which means that we can just negate both
  2008. Result and Remainder, and all it's Ok. }
  2009. Result:=-(Dividend Div Divisor);
  2010. Remainder:=-(Dividend+(Result*Divisor));
  2011. end
  2012. else
  2013. begin
  2014. Result:=Dividend Div Divisor;
  2015. Remainder:=Dividend-(Result*Divisor);
  2016. end;
  2017. end;
  2018. {$endif FPC_MATH_HAS_DIVMOD}
  2019. { Floating point modulo}
  2020. {$ifdef FPC_HAS_TYPE_SINGLE}
  2021. function FMod(const a, b: Single): Single;inline;overload;
  2022. begin
  2023. result:= a-b * Int(a/b);
  2024. end;
  2025. {$endif FPC_HAS_TYPE_SINGLE}
  2026. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2027. function FMod(const a, b: Double): Double;inline;overload;
  2028. begin
  2029. result:= a-b * Int(a/b);
  2030. end;
  2031. {$endif FPC_HAS_TYPE_DOUBLE}
  2032. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2033. function FMod(const a, b: Extended): Extended;inline;overload;
  2034. begin
  2035. result:= a-b * Int(a/b);
  2036. end;
  2037. {$endif FPC_HAS_TYPE_EXTENDED}
  2038. operator mod(const a,b:float) c:float;inline;
  2039. begin
  2040. c:= a-b * Int(a/b);
  2041. if SameValue(abs(c),abs(b)) then
  2042. c:=0.0;
  2043. end;
  2044. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  2045. begin
  2046. if val then result:=iftrue else result:=iffalse;
  2047. end;
  2048. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  2049. begin
  2050. if val then result:=iftrue else result:=iffalse;
  2051. end;
  2052. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  2053. begin
  2054. if val then result:=iftrue else result:=iffalse;
  2055. end;
  2056. // dilemma here. asm can do the two comparisons in one go?
  2057. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  2058. function CompareValue(const A, B : Integer): TValueRelationship;
  2059. begin
  2060. result:=GreaterThanValue;
  2061. if a=b then
  2062. result:=EqualsValue
  2063. else
  2064. if a<b then
  2065. result:=LessThanValue;
  2066. end;
  2067. function CompareValue(const A, B: Int64): TValueRelationship;
  2068. begin
  2069. result:=GreaterThanValue;
  2070. if a=b then
  2071. result:=EqualsValue
  2072. else
  2073. if a<b then
  2074. result:=LessThanValue;
  2075. end;
  2076. function CompareValue(const A, B: QWord): TValueRelationship;
  2077. begin
  2078. result:=GreaterThanValue;
  2079. if a=b then
  2080. result:=EqualsValue
  2081. else
  2082. if a<b then
  2083. result:=LessThanValue;
  2084. end;
  2085. {$ifdef FPC_HAS_TYPE_SINGLE}
  2086. function CompareValue(const A, B: Single; delta: Single = 0.0): TValueRelationship;
  2087. begin
  2088. result:=GreaterThanValue;
  2089. if abs(a-b)<=delta then
  2090. result:=EqualsValue
  2091. else
  2092. if a<b then
  2093. result:=LessThanValue;
  2094. end;
  2095. {$endif}
  2096. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2097. function CompareValue(const A, B: Double; delta: Double = 0.0): TValueRelationship;
  2098. begin
  2099. result:=GreaterThanValue;
  2100. if abs(a-b)<=delta then
  2101. result:=EqualsValue
  2102. else
  2103. if a<b then
  2104. result:=LessThanValue;
  2105. end;
  2106. {$endif}
  2107. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2108. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  2109. begin
  2110. result:=GreaterThanValue;
  2111. if abs(a-b)<=delta then
  2112. result:=EqualsValue
  2113. else
  2114. if a<b then
  2115. result:=LessThanValue;
  2116. end;
  2117. {$endif}
  2118. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2119. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  2120. var
  2121. RV : Double;
  2122. begin
  2123. RV:=IntPower(10,Digits);
  2124. Result:=Round(AValue/RV)*RV;
  2125. end;
  2126. {$endif}
  2127. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2128. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  2129. var
  2130. RV : Extended;
  2131. begin
  2132. RV:=IntPower(10,Digits);
  2133. Result:=Round(AValue/RV)*RV;
  2134. end;
  2135. {$endif}
  2136. {$ifdef FPC_HAS_TYPE_SINGLE}
  2137. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  2138. var
  2139. RV : Single;
  2140. begin
  2141. RV:=IntPower(10,Digits);
  2142. Result:=Round(AValue/RV)*RV;
  2143. end;
  2144. {$endif}
  2145. {$ifdef FPC_HAS_TYPE_SINGLE}
  2146. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  2147. var
  2148. RV : Single;
  2149. begin
  2150. RV := IntPower(10, -Digits);
  2151. if AValue < 0 then
  2152. Result := Int((AValue*RV) - 0.5)/RV
  2153. else
  2154. Result := Int((AValue*RV) + 0.5)/RV;
  2155. end;
  2156. {$endif}
  2157. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2158. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  2159. var
  2160. RV : Double;
  2161. begin
  2162. RV := IntPower(10, -Digits);
  2163. if AValue < 0 then
  2164. Result := Int((AValue*RV) - 0.5)/RV
  2165. else
  2166. Result := Int((AValue*RV) + 0.5)/RV;
  2167. end;
  2168. {$endif}
  2169. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2170. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  2171. var
  2172. RV : Extended;
  2173. begin
  2174. RV := IntPower(10, -Digits);
  2175. if AValue < 0 then
  2176. Result := Int((AValue*RV) - 0.5)/RV
  2177. else
  2178. Result := Int((AValue*RV) + 0.5)/RV;
  2179. end;
  2180. {$endif}
  2181. function RandomFrom(const AValues: array of Double): Double; overload;
  2182. begin
  2183. result:=AValues[random(High(AValues)+1)];
  2184. end;
  2185. function RandomFrom(const AValues: array of Integer): Integer; overload;
  2186. begin
  2187. result:=AValues[random(High(AValues)+1)];
  2188. end;
  2189. function RandomFrom(const AValues: array of Int64): Int64; overload;
  2190. begin
  2191. result:=AValues[random(High(AValues)+1)];
  2192. end;
  2193. {$if FPC_FULLVERSION >=30101}
  2194. generic function RandomFrom<T>(const AValues:array of T):T;
  2195. begin
  2196. result:=AValues[random(High(AValues)+1)];
  2197. end;
  2198. {$endif}
  2199. function FutureValue(ARate: Float; NPeriods: Integer;
  2200. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  2201. var
  2202. q, qn, factor: Float;
  2203. begin
  2204. if ARate = 0 then
  2205. Result := -APresentValue - APayment * NPeriods
  2206. else begin
  2207. q := 1.0 + ARate;
  2208. qn := power(q, NPeriods);
  2209. factor := (qn - 1) / (q - 1);
  2210. if APaymentTime = ptStartOfPeriod then
  2211. factor := factor * q;
  2212. Result := -(APresentValue * qn + APayment*factor);
  2213. end;
  2214. end;
  2215. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  2216. APaymentTime: TPaymentTime): Float;
  2217. { The interest rate cannot be calculated analytically. We solve the equation
  2218. numerically by means of the Newton method:
  2219. - guess value for the interest reate
  2220. - calculate at which interest rate the tangent of the curve fv(rate)
  2221. (straight line!) has the requested future vale.
  2222. - use this rate for the next iteration. }
  2223. const
  2224. DELTA = 0.001;
  2225. EPS = 1E-9; // required precision of interest rate (after typ. 6 iterations)
  2226. MAXIT = 20; // max iteration count to protect agains non-convergence
  2227. var
  2228. r1, r2, dr: Float;
  2229. fv1, fv2: Float;
  2230. iteration: Integer;
  2231. begin
  2232. iteration := 0;
  2233. r1 := 0.05; // inital guess
  2234. repeat
  2235. r2 := r1 + DELTA;
  2236. fv1 := FutureValue(r1, NPeriods, APayment, APresentValue, APaymentTime);
  2237. fv2 := FutureValue(r2, NPeriods, APayment, APresentValue, APaymentTime);
  2238. dr := (AFutureValue - fv1) / (fv2 - fv1) * delta; // tangent at fv(r)
  2239. r1 := r1 + dr; // next guess
  2240. inc(iteration);
  2241. until (abs(dr) < EPS) or (iteration >= MAXIT);
  2242. Result := r1;
  2243. end;
  2244. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  2245. APaymentTime: TPaymentTime): Float;
  2246. { Solve the cash flow equation (1) for q^n and take the logarithm }
  2247. var
  2248. q, x1, x2: Float;
  2249. begin
  2250. if ARate = 0 then
  2251. Result := -(APresentValue + AFutureValue) / APayment
  2252. else begin
  2253. q := 1.0 + ARate;
  2254. if APaymentTime = ptStartOfPeriod then
  2255. APayment := APayment * q;
  2256. x1 := APayment - AFutureValue * ARate;
  2257. x2 := APayment + APresentValue * ARate;
  2258. if (x2 = 0) // we have to divide by x2
  2259. or (sign(x1) * sign(x2) < 0) // the argument of the log is negative
  2260. then
  2261. Result := Infinity
  2262. else begin
  2263. Result := ln(x1/x2) / ln(q);
  2264. end;
  2265. end;
  2266. end;
  2267. function Payment(ARate: Float; NPeriods: Integer;
  2268. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  2269. var
  2270. q, qn, factor: Float;
  2271. begin
  2272. if ARate = 0 then
  2273. Result := -(AFutureValue + APresentValue) / NPeriods
  2274. else begin
  2275. q := 1.0 + ARate;
  2276. qn := power(q, NPeriods);
  2277. factor := (qn - 1) / (q - 1);
  2278. if APaymentTime = ptStartOfPeriod then
  2279. factor := factor * q;
  2280. Result := -(AFutureValue + APresentValue * qn) / factor;
  2281. end;
  2282. end;
  2283. function PresentValue(ARate: Float; NPeriods: Integer;
  2284. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  2285. var
  2286. q, qn, factor: Float;
  2287. begin
  2288. if ARate = 0.0 then
  2289. Result := -AFutureValue - APayment * NPeriods
  2290. else begin
  2291. q := 1.0 + ARate;
  2292. qn := power(q, NPeriods);
  2293. factor := (qn - 1) / (q - 1);
  2294. if APaymentTime = ptStartOfPeriod then
  2295. factor := factor * q;
  2296. Result := -(AFutureValue + APayment*factor) / qn;
  2297. end;
  2298. end;
  2299. {$else}
  2300. implementation
  2301. {$endif FPUNONE}
  2302. end.