math.pp 66 KB

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