math.pp 65 KB

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