math.pp 66 KB

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