math.pp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  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. function copysign(x,y: float): float; forward; { returns abs(x)*sign(y) }
  513. { include cpu specific stuff }
  514. {$i mathu.inc}
  515. ResourceString
  516. SMathError = 'Math Error : %s';
  517. SInvalidArgument = 'Invalid argument';
  518. Procedure DoMathError(Const S : String);
  519. begin
  520. Raise EMathError.CreateFmt(SMathError,[S]);
  521. end;
  522. Procedure InvalidArgument;
  523. begin
  524. Raise EInvalidArgument.Create(SInvalidArgument);
  525. end;
  526. function Sign(const AValue: Integer): TValueSign;inline;
  527. begin
  528. result:=TValueSign(
  529. SarLongint(AValue,sizeof(AValue)*8-1) or { gives -1 for negative values, 0 otherwise }
  530. (longint(-AValue) shr (sizeof(AValue)*8-1)) { gives 1 for positive values, 0 otherwise }
  531. );
  532. end;
  533. function Sign(const AValue: Int64): TValueSign;inline;
  534. begin
  535. {$ifdef cpu64}
  536. result:=TValueSign(
  537. SarInt64(AValue,sizeof(AValue)*8-1) or
  538. (-AValue shr (sizeof(AValue)*8-1))
  539. );
  540. {$else cpu64}
  541. If Avalue<0 then
  542. Result:=NegativeValue
  543. else If Avalue>0 then
  544. Result:=PositiveValue
  545. else
  546. Result:=ZeroValue;
  547. {$endif}
  548. end;
  549. {$ifdef FPC_HAS_TYPE_SINGLE}
  550. function Sign(const AValue: Single): TValueSign;inline;
  551. begin
  552. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  553. end;
  554. {$endif}
  555. function Sign(const AValue: Double): TValueSign;inline;
  556. begin
  557. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  558. end;
  559. {$ifdef FPC_HAS_TYPE_EXTENDED}
  560. function Sign(const AValue: Extended): TValueSign;inline;
  561. begin
  562. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  563. end;
  564. {$endif}
  565. function degtorad(deg : float) : float;inline;
  566. begin
  567. degtorad:=deg*(pi/180.0);
  568. end;
  569. function radtodeg(rad : float) : float;inline;
  570. begin
  571. radtodeg:=rad*(180.0/pi);
  572. end;
  573. function gradtorad(grad : float) : float;inline;
  574. begin
  575. gradtorad:=grad*(pi/200.0);
  576. end;
  577. function radtograd(rad : float) : float;inline;
  578. begin
  579. radtograd:=rad*(200.0/pi);
  580. end;
  581. function degtograd(deg : float) : float;inline;
  582. begin
  583. degtograd:=deg*(200.0/180.0);
  584. end;
  585. function gradtodeg(grad : float) : float;inline;
  586. begin
  587. gradtodeg:=grad*(180.0/200.0);
  588. end;
  589. function cycletorad(cycle : float) : float;inline;
  590. begin
  591. cycletorad:=(2*pi)*cycle;
  592. end;
  593. function radtocycle(rad : float) : float;inline;
  594. begin
  595. { avoid division }
  596. radtocycle:=rad*(1/(2*pi));
  597. end;
  598. {$ifdef FPC_HAS_TYPE_SINGLE}
  599. Function DegNormalize(deg : single) : single;
  600. begin
  601. Result:=Deg-Trunc(Deg/360)*360;
  602. If Result<0 then Result:=Result+360;
  603. end;
  604. {$ENDIF}
  605. {$ifdef FPC_HAS_TYPE_DOUBLE}
  606. Function DegNormalize(deg : double) : double; inline;
  607. begin
  608. Result:=Deg-Trunc(Deg/360)*360;
  609. If (Result<0) then Result:=Result+360;
  610. end;
  611. {$ENDIF}
  612. {$ifdef FPC_HAS_TYPE_EXTENDED}
  613. Function DegNormalize(deg : extended) : extended; inline;
  614. begin
  615. Result:=Deg-Trunc(Deg/360)*360;
  616. If Result<0 then Result:=Result+360;
  617. end;
  618. {$ENDIF}
  619. {$ifndef FPC_MATH_HAS_TAN}
  620. function tan(x : float) : float;
  621. var
  622. _sin,_cos : float;
  623. begin
  624. sincos(x,_sin,_cos);
  625. tan:=_sin/_cos;
  626. end;
  627. {$endif FPC_MATH_HAS_TAN}
  628. {$ifndef FPC_MATH_HAS_COTAN}
  629. function cotan(x : float) : float;
  630. var
  631. _sin,_cos : float;
  632. begin
  633. sincos(x,_sin,_cos);
  634. cotan:=_cos/_sin;
  635. end;
  636. {$endif FPC_MATH_HAS_COTAN}
  637. function cot(x : float) : float; inline;
  638. begin
  639. cot := cotan(x);
  640. end;
  641. {$ifndef FPC_MATH_HAS_SINCOS}
  642. {$ifdef FPC_HAS_TYPE_SINGLE}
  643. procedure sincos(theta : single;out sinus,cosinus : single);
  644. begin
  645. sinus:=sin(theta);
  646. cosinus:=cos(theta);
  647. end;
  648. {$endif}
  649. {$ifdef FPC_HAS_TYPE_DOUBLE}
  650. procedure sincos(theta : double;out sinus,cosinus : double);
  651. begin
  652. sinus:=sin(theta);
  653. cosinus:=cos(theta);
  654. end;
  655. {$endif}
  656. {$ifdef FPC_HAS_TYPE_EXTENDED}
  657. procedure sincos(theta : extended;out sinus,cosinus : extended);
  658. begin
  659. sinus:=sin(theta);
  660. cosinus:=cos(theta);
  661. end;
  662. {$endif}
  663. {$endif FPC_MATH_HAS_SINCOS}
  664. function secant(x : float) : float; inline;
  665. begin
  666. secant := 1 / cos(x);
  667. end;
  668. function cosecant(x : float) : float; inline;
  669. begin
  670. cosecant := 1 / sin(x);
  671. end;
  672. function sec(x : float) : float; inline;
  673. begin
  674. sec := secant(x);
  675. end;
  676. function csc(x : float) : float; inline;
  677. begin
  678. csc := cosecant(x);
  679. end;
  680. { arcsin and arccos functions from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  681. function arcsin(x : float) : float;
  682. begin
  683. arcsin:=arctan2(x,sqrt((1.0-x)*(1.0+x)));
  684. end;
  685. function Arccos(x : Float) : Float;
  686. begin
  687. if abs(x)=1.0 then
  688. if x<0.0 then
  689. arccos:=Pi
  690. else
  691. arccos:=0
  692. else
  693. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  694. end;
  695. {$ifndef FPC_MATH_HAS_ARCTAN2}
  696. function arctan2(y,x : float) : float;
  697. begin
  698. if (x=0) then
  699. begin
  700. if y=0 then
  701. arctan2:=0.0
  702. else if y>0 then
  703. arctan2:=pi/2
  704. else if y<0 then
  705. arctan2:=-pi/2;
  706. end
  707. else
  708. ArcTan2:=ArcTan(y/x);
  709. if x<0.0 then
  710. ArcTan2:=ArcTan2+pi;
  711. if ArcTan2>pi then
  712. ArcTan2:=ArcTan2-2*pi;
  713. end;
  714. {$endif FPC_MATH_HAS_ARCTAN2}
  715. function cosh(x : float) : float;
  716. var
  717. temp : float;
  718. begin
  719. temp:=exp(x);
  720. cosh:=0.5*(temp+1.0/temp);
  721. end;
  722. function sinh(x : float) : float;
  723. var
  724. temp : float;
  725. begin
  726. temp:=exp(x);
  727. { copysign ensures that sinh(-0.0)=-0.0 }
  728. sinh:=copysign(0.5*(temp-1.0/temp),x);
  729. end;
  730. Const MaxTanh = 5678.22249441322; // Ln(MaxExtended)/2
  731. function tanh(x : float) : float;
  732. var Temp : float;
  733. begin
  734. if x>MaxTanh then exit(1.0)
  735. else if x<-MaxTanh then exit (-1.0);
  736. temp:=exp(-2*x);
  737. tanh:=(1-temp)/(1+temp)
  738. end;
  739. function arccosh(x : float) : float; inline;
  740. begin
  741. arccosh:=arcosh(x);
  742. end;
  743. function arcsinh(x : float) : float;inline;
  744. begin
  745. arcsinh:=arsinh(x);
  746. end;
  747. function arctanh(x : float) : float;inline;
  748. begin
  749. arctanh:=artanh(x);
  750. end;
  751. function arcosh(x : float) : float;
  752. begin
  753. { Provides accuracy about 4*eps near 1.0 }
  754. arcosh:=Ln(x+Sqrt((x-1.0)*(x+1.0)));
  755. end;
  756. function arsinh(x : float) : float;
  757. var
  758. z: float;
  759. begin
  760. z:=abs(x);
  761. z:=Ln(z+Sqrt(1+z*z));
  762. { copysign ensures that arsinh(-Inf)=-Inf and arsinh(-0.0)=-0.0 }
  763. arsinh:=copysign(z,x);
  764. end;
  765. function artanh(x : float) : float;
  766. begin
  767. artanh:=(lnxp1(x)-lnxp1(-x))*0.5;
  768. end;
  769. { hypot function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  770. function hypot(x,y : float) : float;
  771. begin
  772. x:=abs(x);
  773. y:=abs(y);
  774. if (x>y) then
  775. hypot:=x*sqrt(1.0+sqr(y/x))
  776. else if (x>0.0) then
  777. hypot:=y*sqrt(1.0+sqr(x/y))
  778. else
  779. hypot:=y;
  780. end;
  781. function log10(x : float) : float;
  782. begin
  783. log10:=ln(x)*0.43429448190325182765; { 1/ln(10) }
  784. end;
  785. {$ifndef FPC_MATH_HAS_LOG2}
  786. function log2(x : float) : float;
  787. begin
  788. log2:=ln(x)*1.4426950408889634079; { 1/ln(2) }
  789. end;
  790. {$endif FPC_MATH_HAS_LOG2}
  791. function logn(n,x : float) : float;
  792. begin
  793. logn:=ln(x)/ln(n);
  794. end;
  795. { lnxp1 function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  796. function lnxp1(x : float) : float;
  797. var
  798. y: float;
  799. begin
  800. if (x>=4.0) then
  801. lnxp1:=ln(1.0+x)
  802. else
  803. begin
  804. y:=1.0+x;
  805. if (y=1.0) then
  806. lnxp1:=x
  807. else
  808. begin
  809. lnxp1:=ln(y); { lnxp1(-1) = ln(0) = -Inf }
  810. if y>0.0 then
  811. lnxp1:=lnxp1+(x-(y-1.0))/y;
  812. end;
  813. end;
  814. end;
  815. function power(base,exponent : float) : float;
  816. begin
  817. if Exponent=0.0 then
  818. result:=1.0
  819. else if (base=0.0) and (exponent>0.0) then
  820. result:=0.0
  821. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  822. result:=intpower(base,trunc(exponent))
  823. else
  824. result:=exp(exponent * ln (base));
  825. end;
  826. function intpower(base : float;const exponent : Integer) : float;
  827. var
  828. i : longint;
  829. begin
  830. if (base = 0.0) and (exponent = 0) then
  831. result:=1
  832. else
  833. begin
  834. i:=abs(exponent);
  835. intpower:=1.0;
  836. while i>0 do
  837. begin
  838. while (i and 1)=0 do
  839. begin
  840. i:=i shr 1;
  841. base:=sqr(base);
  842. end;
  843. i:=i-1;
  844. intpower:=intpower*base;
  845. end;
  846. if exponent<0 then
  847. intpower:=1.0/intpower;
  848. end;
  849. end;
  850. operator ** (bas,expo : float) e: float; inline;
  851. begin
  852. e:=power(bas,expo);
  853. end;
  854. operator ** (bas,expo : int64) i: int64; inline;
  855. begin
  856. i:=round(intpower(bas,expo));
  857. end;
  858. function ceil(x : float) : integer;
  859. begin
  860. Ceil:=Trunc(x);
  861. If Frac(x)>0 then
  862. Ceil:=Ceil+1;
  863. end;
  864. function ceil64(x: float): Int64;
  865. begin
  866. Ceil64:=Trunc(x);
  867. if Frac(x)>0 then
  868. Ceil64:=Ceil64+1;
  869. end;
  870. function floor(x : float) : integer;
  871. begin
  872. Floor:=Trunc(x);
  873. If Frac(x)<0 then
  874. Floor := Floor-1;
  875. end;
  876. function floor64(x: float): Int64;
  877. begin
  878. Floor64:=Trunc(x);
  879. if Frac(x)<0 then
  880. Floor64:=Floor64-1;
  881. end;
  882. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  883. begin
  884. Exponent:=0;
  885. if (X<>0) then
  886. if (abs(X)<0.5) then
  887. repeat
  888. X:=X*2;
  889. Dec(Exponent);
  890. until (abs(X)>=0.5)
  891. else
  892. while (abs(X)>=1) do
  893. begin
  894. X:=X/2;
  895. Inc(Exponent);
  896. end;
  897. Mantissa:=X;
  898. end;
  899. function ldexp(x : float;const p : Integer) : float;
  900. begin
  901. ldexp:=x*intpower(2.0,p);
  902. end;
  903. {$ifdef FPC_HAS_TYPE_SINGLE}
  904. function mean(const data : array of Single) : float;
  905. begin
  906. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  907. end;
  908. function mean(const data : PSingle; Const N : longint) : float;
  909. begin
  910. mean:=sum(Data,N);
  911. mean:=mean/N;
  912. end;
  913. function sum(const data : array of Single) : float;inline;
  914. begin
  915. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  916. end;
  917. function sum(const data : PSingle;Const N : longint) : float;
  918. var
  919. i : longint;
  920. begin
  921. sum:=0.0;
  922. for i:=0 to N-1 do
  923. sum:=sum+data[i];
  924. end;
  925. {$endif FPC_HAS_TYPE_SINGLE}
  926. {$ifdef FPC_HAS_TYPE_DOUBLE}
  927. function mean(const data : array of Double) : float; inline;
  928. begin
  929. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  930. end;
  931. function mean(const data : PDouble; Const N : longint) : float;
  932. begin
  933. mean:=sum(Data,N);
  934. mean:=mean/N;
  935. end;
  936. function sum(const data : array of Double) : float; inline;
  937. begin
  938. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  939. end;
  940. function sum(const data : PDouble;Const N : longint) : float;
  941. var
  942. i : longint;
  943. begin
  944. sum:=0.0;
  945. for i:=0 to N-1 do
  946. sum:=sum+data[i];
  947. end;
  948. {$endif FPC_HAS_TYPE_DOUBLE}
  949. {$ifdef FPC_HAS_TYPE_EXTENDED}
  950. function mean(const data : array of Extended) : float;
  951. begin
  952. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  953. end;
  954. function mean(const data : PExtended; Const N : longint) : float;
  955. begin
  956. mean:=sum(Data,N);
  957. mean:=mean/N;
  958. end;
  959. function sum(const data : array of Extended) : float; inline;
  960. begin
  961. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  962. end;
  963. function sum(const data : PExtended;Const N : longint) : float;
  964. var
  965. i : longint;
  966. begin
  967. sum:=0.0;
  968. for i:=0 to N-1 do
  969. sum:=sum+data[i];
  970. end;
  971. {$endif FPC_HAS_TYPE_EXTENDED}
  972. function sumInt(const data : PInt64;Const N : longint) : Int64;
  973. var
  974. i : longint;
  975. begin
  976. sumInt:=0;
  977. for i:=0 to N-1 do
  978. sumInt:=sumInt+data[i];
  979. end;
  980. function sumInt(const data : array of Int64) : Int64; inline;
  981. begin
  982. Result:=SumInt(@Data[0],High(Data)+1);
  983. end;
  984. {$ifdef FPC_HAS_TYPE_SINGLE}
  985. function sumofsquares(const data : array of Single) : float; inline;
  986. begin
  987. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  988. end;
  989. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  990. var
  991. i : longint;
  992. begin
  993. sumofsquares:=0.0;
  994. for i:=0 to N-1 do
  995. sumofsquares:=sumofsquares+sqr(data[i]);
  996. end;
  997. procedure sumsandsquares(const data : array of Single;
  998. var sum,sumofsquares : float); inline;
  999. begin
  1000. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1001. end;
  1002. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  1003. var sum,sumofsquares : float);
  1004. var
  1005. i : Integer;
  1006. temp : float;
  1007. begin
  1008. sumofsquares:=0.0;
  1009. sum:=0.0;
  1010. for i:=0 to N-1 do
  1011. begin
  1012. temp:=data[i];
  1013. sumofsquares:=sumofsquares+sqr(temp);
  1014. sum:=sum+temp;
  1015. end;
  1016. end;
  1017. {$endif FPC_HAS_TYPE_SINGLE}
  1018. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1019. function sumofsquares(const data : array of Double) : float; inline;
  1020. begin
  1021. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  1022. end;
  1023. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  1024. var
  1025. i : longint;
  1026. begin
  1027. sumofsquares:=0.0;
  1028. for i:=0 to N-1 do
  1029. sumofsquares:=sumofsquares+sqr(data[i]);
  1030. end;
  1031. procedure sumsandsquares(const data : array of Double;
  1032. var sum,sumofsquares : float);
  1033. begin
  1034. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1035. end;
  1036. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  1037. var sum,sumofsquares : float);
  1038. var
  1039. i : Integer;
  1040. temp : float;
  1041. begin
  1042. sumofsquares:=0.0;
  1043. sum:=0.0;
  1044. for i:=0 to N-1 do
  1045. begin
  1046. temp:=data[i];
  1047. sumofsquares:=sumofsquares+sqr(temp);
  1048. sum:=sum+temp;
  1049. end;
  1050. end;
  1051. {$endif FPC_HAS_TYPE_DOUBLE}
  1052. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1053. function sumofsquares(const data : array of Extended) : float; inline;
  1054. begin
  1055. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  1056. end;
  1057. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  1058. var
  1059. i : longint;
  1060. begin
  1061. sumofsquares:=0.0;
  1062. for i:=0 to N-1 do
  1063. sumofsquares:=sumofsquares+sqr(data[i]);
  1064. end;
  1065. procedure sumsandsquares(const data : array of Extended;
  1066. var sum,sumofsquares : float); inline;
  1067. begin
  1068. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1069. end;
  1070. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  1071. var sum,sumofsquares : float);
  1072. var
  1073. i : Integer;
  1074. temp : float;
  1075. begin
  1076. sumofsquares:=0.0;
  1077. sum:=0.0;
  1078. for i:=0 to N-1 do
  1079. begin
  1080. temp:=data[i];
  1081. sumofsquares:=sumofsquares+sqr(temp);
  1082. sum:=sum+temp;
  1083. end;
  1084. end;
  1085. {$endif FPC_HAS_TYPE_EXTENDED}
  1086. function randg(mean,stddev : float) : float;
  1087. Var U1,S2 : Float;
  1088. begin
  1089. repeat
  1090. u1:= 2*random-1;
  1091. S2:=Sqr(U1)+sqr(2*random-1);
  1092. until s2<1;
  1093. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  1094. end;
  1095. function RandomRange(const aFrom, aTo: Integer): Integer;
  1096. begin
  1097. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1098. end;
  1099. function RandomRange(const aFrom, aTo: Int64): Int64;
  1100. begin
  1101. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1102. end;
  1103. {$ifdef FPC_HAS_TYPE_SINGLE}
  1104. function stddev(const data : array of Single) : float; inline;
  1105. begin
  1106. Result:=Stddev(PSingle(@Data[0]),High(Data)+1);
  1107. end;
  1108. function stddev(const data : PSingle; Const N : Integer) : float;
  1109. begin
  1110. StdDev:=Sqrt(Variance(Data,N));
  1111. end;
  1112. procedure meanandstddev(const data : array of Single;
  1113. var mean,stddev : float); inline;
  1114. begin
  1115. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  1116. end;
  1117. procedure meanandstddev(const data : PSingle;
  1118. Const N : Longint;var mean,stddev : float);
  1119. Var I : longint;
  1120. begin
  1121. Mean:=0;
  1122. StdDev:=0;
  1123. For I:=0 to N-1 do
  1124. begin
  1125. Mean:=Mean+Data[i];
  1126. StdDev:=StdDev+Sqr(Data[i]);
  1127. end;
  1128. Mean:=Mean/N;
  1129. StdDev:=(StdDev-N*Sqr(Mean));
  1130. If N>1 then
  1131. StdDev:=Sqrt(Stddev/(N-1))
  1132. else
  1133. StdDev:=0;
  1134. end;
  1135. function variance(const data : array of Single) : float; inline;
  1136. begin
  1137. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  1138. end;
  1139. function variance(const data : PSingle; Const N : Integer) : float;
  1140. begin
  1141. If N=1 then
  1142. Result:=0
  1143. else
  1144. Result:=TotalVariance(Data,N)/(N-1);
  1145. end;
  1146. function totalvariance(const data : array of Single) : float; inline;
  1147. begin
  1148. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  1149. end;
  1150. function totalvariance(const data : PSingle;Const N : Integer) : float;
  1151. var S,SS : Float;
  1152. begin
  1153. If N=1 then
  1154. Result:=0
  1155. else
  1156. begin
  1157. SumsAndSquares(Data,N,S,SS);
  1158. Result := SS-Sqr(S)/N;
  1159. end;
  1160. end;
  1161. function popnstddev(const data : array of Single) : float;
  1162. begin
  1163. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  1164. end;
  1165. function popnstddev(const data : PSingle; Const N : Integer) : float;
  1166. begin
  1167. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1168. end;
  1169. function popnvariance(const data : array of Single) : float; inline;
  1170. begin
  1171. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  1172. end;
  1173. function popnvariance(const data : PSingle; Const N : Integer) : float;
  1174. begin
  1175. PopnVariance:=TotalVariance(Data,N)/N;
  1176. end;
  1177. procedure momentskewkurtosis(const data : array of single;
  1178. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1179. begin
  1180. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1181. end;
  1182. procedure momentskewkurtosis(
  1183. const data: pSingle;
  1184. Const N: integer;
  1185. out m1: float;
  1186. out m2: float;
  1187. out m3: float;
  1188. out m4: float;
  1189. out skew: float;
  1190. out kurtosis: float
  1191. );
  1192. var
  1193. i: integer;
  1194. value : psingle;
  1195. deviation, deviation2: single;
  1196. reciprocalN: float;
  1197. begin
  1198. m1 := 0;
  1199. reciprocalN := 1/N;
  1200. value := data;
  1201. for i := 0 to N-1 do
  1202. begin
  1203. m1 := m1 + value^;
  1204. inc(value);
  1205. end;
  1206. m1 := reciprocalN * m1;
  1207. m2 := 0;
  1208. m3 := 0;
  1209. m4 := 0;
  1210. value := data;
  1211. for i := 0 to N-1 do
  1212. begin
  1213. deviation := (value^-m1);
  1214. deviation2 := deviation * deviation;
  1215. m2 := m2 + deviation2;
  1216. m3 := m3 + deviation2 * deviation;
  1217. m4 := m4 + deviation2 * deviation2;
  1218. inc(value);
  1219. end;
  1220. m2 := reciprocalN * m2;
  1221. m3 := reciprocalN * m3;
  1222. m4 := reciprocalN * m4;
  1223. skew := m3 / (sqrt(m2)*m2);
  1224. kurtosis := m4 / (m2 * m2);
  1225. end;
  1226. function norm(const data : array of Single) : float; inline;
  1227. begin
  1228. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  1229. end;
  1230. function norm(const data : PSingle; Const N : Integer) : float;
  1231. begin
  1232. norm:=sqrt(sumofsquares(data,N));
  1233. end;
  1234. {$endif FPC_HAS_TYPE_SINGLE}
  1235. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1236. function stddev(const data : array of Double) : float; inline;
  1237. begin
  1238. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  1239. end;
  1240. function stddev(const data : PDouble; Const N : Integer) : float;
  1241. begin
  1242. StdDev:=Sqrt(Variance(Data,N));
  1243. end;
  1244. procedure meanandstddev(const data : array of Double;
  1245. var mean,stddev : float);
  1246. begin
  1247. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  1248. end;
  1249. procedure meanandstddev(const data : PDouble;
  1250. Const N : Longint;var mean,stddev : float);
  1251. Var I : longint;
  1252. begin
  1253. Mean:=0;
  1254. StdDev:=0;
  1255. For I:=0 to N-1 do
  1256. begin
  1257. Mean:=Mean+Data[i];
  1258. StdDev:=StdDev+Sqr(Data[i]);
  1259. end;
  1260. Mean:=Mean/N;
  1261. StdDev:=(StdDev-N*Sqr(Mean));
  1262. If N>1 then
  1263. StdDev:=Sqrt(Stddev/(N-1))
  1264. else
  1265. StdDev:=0;
  1266. end;
  1267. function variance(const data : array of Double) : float; inline;
  1268. begin
  1269. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  1270. end;
  1271. function variance(const data : PDouble; Const N : Integer) : float;
  1272. begin
  1273. If N=1 then
  1274. Result:=0
  1275. else
  1276. Result:=TotalVariance(Data,N)/(N-1);
  1277. end;
  1278. function totalvariance(const data : array of Double) : float; inline;
  1279. begin
  1280. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  1281. end;
  1282. function totalvariance(const data : PDouble;Const N : Integer) : float;
  1283. var S,SS : Float;
  1284. begin
  1285. If N=1 then
  1286. Result:=0
  1287. else
  1288. begin
  1289. SumsAndSquares(Data,N,S,SS);
  1290. Result := SS-Sqr(S)/N;
  1291. end;
  1292. end;
  1293. function popnstddev(const data : array of Double) : float;
  1294. begin
  1295. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  1296. end;
  1297. function popnstddev(const data : PDouble; Const N : Integer) : float;
  1298. begin
  1299. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1300. end;
  1301. function popnvariance(const data : array of Double) : float; inline;
  1302. begin
  1303. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  1304. end;
  1305. function popnvariance(const data : PDouble; Const N : Integer) : float;
  1306. begin
  1307. PopnVariance:=TotalVariance(Data,N)/N;
  1308. end;
  1309. procedure momentskewkurtosis(const data : array of Double;
  1310. out m1,m2,m3,m4,skew,kurtosis : float);
  1311. begin
  1312. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1313. end;
  1314. procedure momentskewkurtosis(
  1315. const data: pdouble;
  1316. Const N: integer;
  1317. out m1: float;
  1318. out m2: float;
  1319. out m3: float;
  1320. out m4: float;
  1321. out skew: float;
  1322. out kurtosis: float
  1323. );
  1324. var
  1325. i: integer;
  1326. value : pdouble;
  1327. deviation, deviation2: double;
  1328. reciprocalN: float;
  1329. begin
  1330. m1 := 0;
  1331. reciprocalN := 1/N;
  1332. value := data;
  1333. for i := 0 to N-1 do
  1334. begin
  1335. m1 := m1 + value^;
  1336. inc(value);
  1337. end;
  1338. m1 := reciprocalN * m1;
  1339. m2 := 0;
  1340. m3 := 0;
  1341. m4 := 0;
  1342. value := data;
  1343. for i := 0 to N-1 do
  1344. begin
  1345. deviation := (value^-m1);
  1346. deviation2 := deviation * deviation;
  1347. m2 := m2 + deviation2;
  1348. m3 := m3 + deviation2 * deviation;
  1349. m4 := m4 + deviation2 * deviation2;
  1350. inc(value);
  1351. end;
  1352. m2 := reciprocalN * m2;
  1353. m3 := reciprocalN * m3;
  1354. m4 := reciprocalN * m4;
  1355. skew := m3 / (sqrt(m2)*m2);
  1356. kurtosis := m4 / (m2 * m2);
  1357. end;
  1358. function norm(const data : array of Double) : float; inline;
  1359. begin
  1360. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  1361. end;
  1362. function norm(const data : PDouble; Const N : Integer) : float;
  1363. begin
  1364. norm:=sqrt(sumofsquares(data,N));
  1365. end;
  1366. {$endif FPC_HAS_TYPE_DOUBLE}
  1367. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1368. function stddev(const data : array of Extended) : float; inline;
  1369. begin
  1370. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  1371. end;
  1372. function stddev(const data : PExtended; Const N : Integer) : float;
  1373. begin
  1374. StdDev:=Sqrt(Variance(Data,N));
  1375. end;
  1376. procedure meanandstddev(const data : array of Extended;
  1377. var mean,stddev : float); inline;
  1378. begin
  1379. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  1380. end;
  1381. procedure meanandstddev(const data : PExtended;
  1382. Const N : Longint;var mean,stddev : float);
  1383. Var I : longint;
  1384. begin
  1385. Mean:=0;
  1386. StdDev:=0;
  1387. For I:=0 to N-1 do
  1388. begin
  1389. Mean:=Mean+Data[i];
  1390. StdDev:=StdDev+Sqr(Data[i]);
  1391. end;
  1392. Mean:=Mean/N;
  1393. StdDev:=(StdDev-N*Sqr(Mean));
  1394. If N>1 then
  1395. StdDev:=Sqrt(Stddev/(N-1))
  1396. else
  1397. StdDev:=0;
  1398. end;
  1399. function variance(const data : array of Extended) : float; inline;
  1400. begin
  1401. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  1402. end;
  1403. function variance(const data : PExtended; Const N : Integer) : float;
  1404. begin
  1405. If N=1 then
  1406. Result:=0
  1407. else
  1408. Result:=TotalVariance(Data,N)/(N-1);
  1409. end;
  1410. function totalvariance(const data : array of Extended) : float; inline;
  1411. begin
  1412. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  1413. end;
  1414. function totalvariance(const data : PExtended;Const N : Integer) : float;
  1415. var S,SS : Float;
  1416. begin
  1417. If N=1 then
  1418. Result:=0
  1419. else
  1420. begin
  1421. SumsAndSquares(Data,N,S,SS);
  1422. Result := SS-Sqr(S)/N;
  1423. end;
  1424. end;
  1425. function popnstddev(const data : array of Extended) : float;
  1426. begin
  1427. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  1428. end;
  1429. function popnstddev(const data : PExtended; Const N : Integer) : float;
  1430. begin
  1431. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1432. end;
  1433. function popnvariance(const data : array of Extended) : float; inline;
  1434. begin
  1435. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  1436. end;
  1437. function popnvariance(const data : PExtended; Const N : Integer) : float;
  1438. begin
  1439. PopnVariance:=TotalVariance(Data,N)/N;
  1440. end;
  1441. procedure momentskewkurtosis(const data : array of Extended;
  1442. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1443. begin
  1444. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1445. end;
  1446. procedure momentskewkurtosis(
  1447. const data: pExtended;
  1448. Const N: integer;
  1449. out m1: float;
  1450. out m2: float;
  1451. out m3: float;
  1452. out m4: float;
  1453. out skew: float;
  1454. out kurtosis: float
  1455. );
  1456. var
  1457. i: integer;
  1458. value : pextended;
  1459. deviation, deviation2: extended;
  1460. reciprocalN: float;
  1461. begin
  1462. m1 := 0;
  1463. reciprocalN := 1/N;
  1464. value := data;
  1465. for i := 0 to N-1 do
  1466. begin
  1467. m1 := m1 + value^;
  1468. inc(value);
  1469. end;
  1470. m1 := reciprocalN * m1;
  1471. m2 := 0;
  1472. m3 := 0;
  1473. m4 := 0;
  1474. value := data;
  1475. for i := 0 to N-1 do
  1476. begin
  1477. deviation := (value^-m1);
  1478. deviation2 := deviation * deviation;
  1479. m2 := m2 + deviation2;
  1480. m3 := m3 + deviation2 * deviation;
  1481. m4 := m4 + deviation2 * deviation2;
  1482. inc(value);
  1483. end;
  1484. m2 := reciprocalN * m2;
  1485. m3 := reciprocalN * m3;
  1486. m4 := reciprocalN * m4;
  1487. skew := m3 / (sqrt(m2)*m2);
  1488. kurtosis := m4 / (m2 * m2);
  1489. end;
  1490. function norm(const data : array of Extended) : float; inline;
  1491. begin
  1492. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  1493. end;
  1494. function norm(const data : PExtended; Const N : Integer) : float;
  1495. begin
  1496. norm:=sqrt(sumofsquares(data,N));
  1497. end;
  1498. {$endif FPC_HAS_TYPE_EXTENDED}
  1499. function MinIntValue(const Data: array of Integer): Integer;
  1500. var
  1501. I: Integer;
  1502. begin
  1503. Result := Data[Low(Data)];
  1504. For I := Succ(Low(Data)) To High(Data) Do
  1505. If Data[I] < Result Then Result := Data[I];
  1506. end;
  1507. function MaxIntValue(const Data: array of Integer): Integer;
  1508. var
  1509. I: Integer;
  1510. begin
  1511. Result := Data[Low(Data)];
  1512. For I := Succ(Low(Data)) To High(Data) Do
  1513. If Data[I] > Result Then Result := Data[I];
  1514. end;
  1515. function MinValue(const Data: array of Integer): Integer; inline;
  1516. begin
  1517. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  1518. end;
  1519. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  1520. var
  1521. I: Integer;
  1522. begin
  1523. Result := Data[0];
  1524. For I := 1 To N-1 do
  1525. If Data[I] < Result Then Result := Data[I];
  1526. end;
  1527. function MaxValue(const Data: array of Integer): Integer; inline;
  1528. begin
  1529. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  1530. end;
  1531. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  1532. var
  1533. i : longint;
  1534. begin
  1535. { get an initial value }
  1536. maxvalue:=data[0];
  1537. for i:=1 to N-1 do
  1538. if data[i]>maxvalue then
  1539. maxvalue:=data[i];
  1540. end;
  1541. {$ifdef FPC_HAS_TYPE_SINGLE}
  1542. function minvalue(const data : array of Single) : Single; inline;
  1543. begin
  1544. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  1545. end;
  1546. function minvalue(const data : PSingle; Const N : Integer) : Single;
  1547. var
  1548. i : longint;
  1549. begin
  1550. { get an initial value }
  1551. minvalue:=data[0];
  1552. for i:=1 to N-1 do
  1553. if data[i]<minvalue then
  1554. minvalue:=data[i];
  1555. end;
  1556. function maxvalue(const data : array of Single) : Single; inline;
  1557. begin
  1558. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  1559. end;
  1560. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  1561. var
  1562. i : longint;
  1563. begin
  1564. { get an initial value }
  1565. maxvalue:=data[0];
  1566. for i:=1 to N-1 do
  1567. if data[i]>maxvalue then
  1568. maxvalue:=data[i];
  1569. end;
  1570. {$endif FPC_HAS_TYPE_SINGLE}
  1571. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1572. function minvalue(const data : array of Double) : Double; inline;
  1573. begin
  1574. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  1575. end;
  1576. function minvalue(const data : PDouble; Const N : Integer) : Double;
  1577. var
  1578. i : longint;
  1579. begin
  1580. { get an initial value }
  1581. minvalue:=data[0];
  1582. for i:=1 to N-1 do
  1583. if data[i]<minvalue then
  1584. minvalue:=data[i];
  1585. end;
  1586. function maxvalue(const data : array of Double) : Double; inline;
  1587. begin
  1588. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  1589. end;
  1590. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  1591. var
  1592. i : longint;
  1593. begin
  1594. { get an initial value }
  1595. maxvalue:=data[0];
  1596. for i:=1 to N-1 do
  1597. if data[i]>maxvalue then
  1598. maxvalue:=data[i];
  1599. end;
  1600. {$endif FPC_HAS_TYPE_DOUBLE}
  1601. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1602. function minvalue(const data : array of Extended) : Extended; inline;
  1603. begin
  1604. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  1605. end;
  1606. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  1607. var
  1608. i : longint;
  1609. begin
  1610. { get an initial value }
  1611. minvalue:=data[0];
  1612. for i:=1 to N-1 do
  1613. if data[i]<minvalue then
  1614. minvalue:=data[i];
  1615. end;
  1616. function maxvalue(const data : array of Extended) : Extended; inline;
  1617. begin
  1618. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  1619. end;
  1620. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  1621. var
  1622. i : longint;
  1623. begin
  1624. { get an initial value }
  1625. maxvalue:=data[0];
  1626. for i:=1 to N-1 do
  1627. if data[i]>maxvalue then
  1628. maxvalue:=data[i];
  1629. end;
  1630. {$endif FPC_HAS_TYPE_EXTENDED}
  1631. function Min(a, b: Integer): Integer;inline;
  1632. begin
  1633. if a < b then
  1634. Result := a
  1635. else
  1636. Result := b;
  1637. end;
  1638. function Max(a, b: Integer): Integer;inline;
  1639. begin
  1640. if a > b then
  1641. Result := a
  1642. else
  1643. Result := b;
  1644. end;
  1645. {
  1646. function Min(a, b: Cardinal): Cardinal;inline;
  1647. begin
  1648. if a < b then
  1649. Result := a
  1650. else
  1651. Result := b;
  1652. end;
  1653. function Max(a, b: Cardinal): Cardinal;inline;
  1654. begin
  1655. if a > b then
  1656. Result := a
  1657. else
  1658. Result := b;
  1659. end;
  1660. }
  1661. function Min(a, b: Int64): Int64;inline;
  1662. begin
  1663. if a < b then
  1664. Result := a
  1665. else
  1666. Result := b;
  1667. end;
  1668. function Max(a, b: Int64): Int64;inline;
  1669. begin
  1670. if a > b then
  1671. Result := a
  1672. else
  1673. Result := b;
  1674. end;
  1675. {$ifdef FPC_HAS_TYPE_SINGLE}
  1676. function Min(a, b: Single): Single;inline;
  1677. begin
  1678. if a < b then
  1679. Result := a
  1680. else
  1681. Result := b;
  1682. end;
  1683. function Max(a, b: Single): Single;inline;
  1684. begin
  1685. if a > b then
  1686. Result := a
  1687. else
  1688. Result := b;
  1689. end;
  1690. {$endif FPC_HAS_TYPE_SINGLE}
  1691. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1692. function Min(a, b: Double): Double;inline;
  1693. begin
  1694. if a < b then
  1695. Result := a
  1696. else
  1697. Result := b;
  1698. end;
  1699. function Max(a, b: Double): Double;inline;
  1700. begin
  1701. if a > b then
  1702. Result := a
  1703. else
  1704. Result := b;
  1705. end;
  1706. {$endif FPC_HAS_TYPE_DOUBLE}
  1707. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1708. function Min(a, b: Extended): Extended;inline;
  1709. begin
  1710. if a < b then
  1711. Result := a
  1712. else
  1713. Result := b;
  1714. end;
  1715. function Max(a, b: Extended): Extended;inline;
  1716. begin
  1717. if a > b then
  1718. Result := a
  1719. else
  1720. Result := b;
  1721. end;
  1722. {$endif FPC_HAS_TYPE_EXTENDED}
  1723. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  1724. begin
  1725. Result:=(AValue>=AMin) and (AValue<=AMax);
  1726. end;
  1727. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  1728. begin
  1729. Result:=(AValue>=AMin) and (AValue<=AMax);
  1730. end;
  1731. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1732. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  1733. begin
  1734. Result:=(AValue>=AMin) and (AValue<=AMax);
  1735. end;
  1736. {$endif FPC_HAS_TYPE_DOUBLE}
  1737. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  1738. begin
  1739. Result:=AValue;
  1740. If Result<AMin then
  1741. Result:=AMin
  1742. else if Result>AMax then
  1743. Result:=AMax;
  1744. end;
  1745. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  1746. begin
  1747. Result:=AValue;
  1748. If Result<AMin then
  1749. Result:=AMin
  1750. else if Result>AMax then
  1751. Result:=AMax;
  1752. end;
  1753. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1754. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  1755. begin
  1756. Result:=AValue;
  1757. If Result<AMin then
  1758. Result:=AMin
  1759. else if Result>AMax then
  1760. Result:=AMax;
  1761. end;
  1762. {$endif FPC_HAS_TYPE_DOUBLE}
  1763. Const
  1764. EZeroResolution = 1E-16;
  1765. DZeroResolution = 1E-12;
  1766. SZeroResolution = 1E-4;
  1767. function IsZero(const A: Single; Epsilon: Single): Boolean;
  1768. begin
  1769. if (Epsilon=0) then
  1770. Epsilon:=SZeroResolution;
  1771. Result:=Abs(A)<=Epsilon;
  1772. end;
  1773. function IsZero(const A: Single): Boolean;inline;
  1774. begin
  1775. Result:=IsZero(A,single(SZeroResolution));
  1776. end;
  1777. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1778. function IsZero(const A: Double; Epsilon: Double): Boolean;
  1779. begin
  1780. if (Epsilon=0) then
  1781. Epsilon:=DZeroResolution;
  1782. Result:=Abs(A)<=Epsilon;
  1783. end;
  1784. function IsZero(const A: Double): Boolean;inline;
  1785. begin
  1786. Result:=IsZero(A,DZeroResolution);
  1787. end;
  1788. {$endif FPC_HAS_TYPE_DOUBLE}
  1789. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1790. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  1791. begin
  1792. if (Epsilon=0) then
  1793. Epsilon:=EZeroResolution;
  1794. Result:=Abs(A)<=Epsilon;
  1795. end;
  1796. function IsZero(const A: Extended): Boolean;inline;
  1797. begin
  1798. Result:=IsZero(A,EZeroResolution);
  1799. end;
  1800. {$endif FPC_HAS_TYPE_EXTENDED}
  1801. type
  1802. TSplitDouble = packed record
  1803. cards: Array[0..1] of cardinal;
  1804. end;
  1805. TSplitExtended = packed record
  1806. cards: Array[0..1] of cardinal;
  1807. w: word;
  1808. end;
  1809. function IsNan(const d : Single): Boolean; overload;
  1810. begin
  1811. result:=(longword(d) and $7fffffff)>$7f800000;
  1812. end;
  1813. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1814. function IsNan(const d : Double): Boolean;
  1815. var
  1816. fraczero, expMaximal: boolean;
  1817. begin
  1818. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1819. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1820. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1821. (TSplitDouble(d).cards[1] = 0);
  1822. {$else FPC_BIG_ENDIAN}
  1823. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1824. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1825. (TSplitDouble(d).cards[0] = 0);
  1826. {$endif FPC_BIG_ENDIAN}
  1827. Result:=expMaximal and not(fraczero);
  1828. end;
  1829. {$endif FPC_HAS_TYPE_DOUBLE}
  1830. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1831. function IsNan(const d : Extended): Boolean; overload;
  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. function copysign(x,y: float): float;
  1861. begin
  1862. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  1863. {$error copysign not yet implemented for float128}
  1864. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  1865. TSplitExtended(x).w:=(TSplitExtended(x).w and $7fff) or (TSplitExtended(y).w and $8000);
  1866. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  1867. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1868. TSplitDouble(x).cards[0]:=(TSplitDouble(x).cards[0] and $7fffffff) or (TSplitDouble(y).cards[0] and longword($80000000));
  1869. {$else}
  1870. TSplitDouble(x).cards[1]:=(TSplitDouble(x).cards[1] and $7fffffff) or (TSplitDouble(y).cards[1] and longword($80000000));
  1871. {$endif}
  1872. {$else}
  1873. longword(x):=longword(x and $7fffffff) or (longword(y) and longword($80000000));
  1874. {$endif}
  1875. result:=x;
  1876. end;
  1877. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1878. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1879. begin
  1880. if (Epsilon=0) then
  1881. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1882. if (A>B) then
  1883. Result:=((A-B)<=Epsilon)
  1884. else
  1885. Result:=((B-A)<=Epsilon);
  1886. end;
  1887. function SameValue(const A, B: Extended): Boolean;inline;
  1888. begin
  1889. Result:=SameValue(A,B,0.0);
  1890. end;
  1891. {$endif FPC_HAS_TYPE_EXTENDED}
  1892. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1893. function SameValue(const A, B: Double): Boolean;inline;
  1894. begin
  1895. Result:=SameValue(A,B,0.0);
  1896. end;
  1897. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1898. begin
  1899. if (Epsilon=0) then
  1900. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1901. if (A>B) then
  1902. Result:=((A-B)<=Epsilon)
  1903. else
  1904. Result:=((B-A)<=Epsilon);
  1905. end;
  1906. {$endif FPC_HAS_TYPE_DOUBLE}
  1907. function SameValue(const A, B: Single): Boolean;inline;
  1908. begin
  1909. Result:=SameValue(A,B,0);
  1910. end;
  1911. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  1912. begin
  1913. if (Epsilon=0) then
  1914. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  1915. if (A>B) then
  1916. Result:=((A-B)<=Epsilon)
  1917. else
  1918. Result:=((B-A)<=Epsilon);
  1919. end;
  1920. // Some CPUs probably allow a faster way of doing this in a single operation...
  1921. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  1922. {$ifndef FPC_MATH_HAS_DIVMOD}
  1923. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  1924. begin
  1925. if Dividend < 0 then
  1926. begin
  1927. { Use DivMod with >=0 dividend }
  1928. Dividend:=-Dividend;
  1929. { The documented behavior of Pascal's div/mod operators and DivMod
  1930. on negative dividends is to return Result closer to zero and
  1931. a negative Remainder. Which means that we can just negate both
  1932. Result and Remainder, and all it's Ok. }
  1933. Result:=-(Dividend Div Divisor);
  1934. Remainder:=-(Dividend+(Result*Divisor));
  1935. end
  1936. else
  1937. begin
  1938. Result:=Dividend Div Divisor;
  1939. Remainder:=Dividend-(Result*Divisor);
  1940. end;
  1941. end;
  1942. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  1943. begin
  1944. if Dividend < 0 then
  1945. begin
  1946. { Use DivMod with >=0 dividend }
  1947. Dividend:=-Dividend;
  1948. { The documented behavior of Pascal's div/mod operators and DivMod
  1949. on negative dividends is to return Result closer to zero and
  1950. a negative Remainder. Which means that we can just negate both
  1951. Result and Remainder, and all it's Ok. }
  1952. Result:=-(Dividend Div Divisor);
  1953. Remainder:=-(Dividend+(Result*Divisor));
  1954. end
  1955. else
  1956. begin
  1957. Result:=Dividend Div Divisor;
  1958. Remainder:=Dividend-(Result*Divisor);
  1959. end;
  1960. end;
  1961. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  1962. begin
  1963. Result:=Dividend Div Divisor;
  1964. Remainder:=Dividend-(Result*Divisor);
  1965. end;
  1966. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  1967. begin
  1968. if Dividend < 0 then
  1969. begin
  1970. { Use DivMod with >=0 dividend }
  1971. Dividend:=-Dividend;
  1972. { The documented behavior of Pascal's div/mod operators and DivMod
  1973. on negative dividends is to return Result closer to zero and
  1974. a negative Remainder. Which means that we can just negate both
  1975. Result and Remainder, and all it's Ok. }
  1976. Result:=-(Dividend Div Divisor);
  1977. Remainder:=-(Dividend+(Result*Divisor));
  1978. end
  1979. else
  1980. begin
  1981. Result:=Dividend Div Divisor;
  1982. Remainder:=Dividend-(Result*Divisor);
  1983. end;
  1984. end;
  1985. {$endif FPC_MATH_HAS_DIVMOD}
  1986. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  1987. begin
  1988. if val then result:=iftrue else result:=iffalse;
  1989. end;
  1990. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  1991. begin
  1992. if val then result:=iftrue else result:=iffalse;
  1993. end;
  1994. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  1995. begin
  1996. if val then result:=iftrue else result:=iffalse;
  1997. end;
  1998. // dilemma here. asm can do the two comparisons in one go?
  1999. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  2000. function CompareValue(const A, B : Integer): TValueRelationship;
  2001. begin
  2002. result:=GreaterThanValue;
  2003. if a=b then
  2004. result:=EqualsValue
  2005. else
  2006. if a<b then
  2007. result:=LessThanValue;
  2008. end;
  2009. function CompareValue(const A, B: Int64): TValueRelationship;
  2010. begin
  2011. result:=GreaterThanValue;
  2012. if a=b then
  2013. result:=EqualsValue
  2014. else
  2015. if a<b then
  2016. result:=LessThanValue;
  2017. end;
  2018. function CompareValue(const A, B: QWord): TValueRelationship;
  2019. begin
  2020. result:=GreaterThanValue;
  2021. if a=b then
  2022. result:=EqualsValue
  2023. else
  2024. if a<b then
  2025. result:=LessThanValue;
  2026. end;
  2027. {$ifdef FPC_HAS_TYPE_SINGLE}
  2028. function CompareValue(const A, B: Single; delta: Single = 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_DOUBLE}
  2039. function CompareValue(const A, B: Double; delta: Double = 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_EXTENDED}
  2050. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  2051. begin
  2052. result:=GreaterThanValue;
  2053. if abs(a-b)<=delta then
  2054. result:=EqualsValue
  2055. else
  2056. if a<b then
  2057. result:=LessThanValue;
  2058. end;
  2059. {$endif}
  2060. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2061. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  2062. var
  2063. RV : Double;
  2064. begin
  2065. RV:=IntPower(10,Digits);
  2066. Result:=Round(AValue/RV)*RV;
  2067. end;
  2068. {$endif}
  2069. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2070. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  2071. var
  2072. RV : Extended;
  2073. begin
  2074. RV:=IntPower(10,Digits);
  2075. Result:=Round(AValue/RV)*RV;
  2076. end;
  2077. {$endif}
  2078. {$ifdef FPC_HAS_TYPE_SINGLE}
  2079. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  2080. var
  2081. RV : Single;
  2082. begin
  2083. RV:=IntPower(10,Digits);
  2084. Result:=Round(AValue/RV)*RV;
  2085. end;
  2086. {$endif}
  2087. {$ifdef FPC_HAS_TYPE_SINGLE}
  2088. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  2089. var
  2090. RV : Single;
  2091. begin
  2092. RV := IntPower(10, -Digits);
  2093. if AValue < 0 then
  2094. Result := Trunc((AValue*RV) - 0.5)/RV
  2095. else
  2096. Result := Trunc((AValue*RV) + 0.5)/RV;
  2097. end;
  2098. {$endif}
  2099. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2100. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  2101. var
  2102. RV : Double;
  2103. begin
  2104. RV := IntPower(10, -Digits);
  2105. if AValue < 0 then
  2106. Result := Trunc((AValue*RV) - 0.5)/RV
  2107. else
  2108. Result := Trunc((AValue*RV) + 0.5)/RV;
  2109. end;
  2110. {$endif}
  2111. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2112. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  2113. var
  2114. RV : Extended;
  2115. begin
  2116. RV := IntPower(10, -Digits);
  2117. if AValue < 0 then
  2118. Result := Trunc((AValue*RV) - 0.5)/RV
  2119. else
  2120. Result := Trunc((AValue*RV) + 0.5)/RV;
  2121. end;
  2122. {$endif}
  2123. function RandomFrom(const AValues: array of Double): Double; overload;
  2124. begin
  2125. result:=AValues[random(High(AValues)+1)];
  2126. end;
  2127. function RandomFrom(const AValues: array of Integer): Integer; overload;
  2128. begin
  2129. result:=AValues[random(High(AValues)+1)];
  2130. end;
  2131. function RandomFrom(const AValues: array of Int64): Int64; overload;
  2132. begin
  2133. result:=AValues[random(High(AValues)+1)];
  2134. end;
  2135. function FutureValue(ARate: Float; NPeriods: Integer;
  2136. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  2137. var
  2138. q, qn, factor: Float;
  2139. begin
  2140. if ARate = 0 then
  2141. Result := -APresentValue - APayment * NPeriods
  2142. else begin
  2143. q := 1.0 + ARate;
  2144. qn := power(q, NPeriods);
  2145. factor := (qn - 1) / (q - 1);
  2146. if APaymentTime = ptStartOfPeriod then
  2147. factor := factor * q;
  2148. Result := -(APresentValue * qn + APayment*factor);
  2149. end;
  2150. end;
  2151. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  2152. APaymentTime: TPaymentTime): Float;
  2153. { The interest rate cannot be calculated analytically. We solve the equation
  2154. numerically by means of the Newton method:
  2155. - guess value for the interest reate
  2156. - calculate at which interest rate the tangent of the curve fv(rate)
  2157. (straight line!) has the requested future vale.
  2158. - use this rate for the next iteration. }
  2159. const
  2160. DELTA = 0.001;
  2161. EPS = 1E-9; // required precision of interest rate (after typ. 6 iterations)
  2162. MAXIT = 20; // max iteration count to protect agains non-convergence
  2163. var
  2164. r1, r2, dr: Float;
  2165. fv1, fv2: Float;
  2166. iteration: Integer;
  2167. begin
  2168. iteration := 0;
  2169. r1 := 0.05; // inital guess
  2170. repeat
  2171. r2 := r1 + DELTA;
  2172. fv1 := FutureValue(r1, NPeriods, APayment, APresentValue, APaymentTime);
  2173. fv2 := FutureValue(r2, NPeriods, APayment, APresentValue, APaymentTime);
  2174. dr := (AFutureValue - fv1) / (fv2 - fv1) * delta; // tangent at fv(r)
  2175. r1 := r1 + dr; // next guess
  2176. inc(iteration);
  2177. until (abs(dr) < EPS) or (iteration >= MAXIT);
  2178. Result := r1;
  2179. end;
  2180. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  2181. APaymentTime: TPaymentTime): Float;
  2182. { Solve the cash flow equation (1) for q^n and take the logarithm }
  2183. var
  2184. q, x1, x2: Float;
  2185. begin
  2186. if ARate = 0 then
  2187. Result := -(APresentValue + AFutureValue) / APayment
  2188. else begin
  2189. q := 1.0 + ARate;
  2190. if APaymentTime = ptStartOfPeriod then
  2191. APayment := APayment * q;
  2192. x1 := APayment - AFutureValue * ARate;
  2193. x2 := APayment + APresentValue * ARate;
  2194. if (x2 = 0) // we have to divide by x2
  2195. or (sign(x1) * sign(x2) < 0) // the argument of the log is negative
  2196. then
  2197. Result := Infinity
  2198. else begin
  2199. Result := ln(x1/x2) / ln(q);
  2200. end;
  2201. end;
  2202. end;
  2203. function Payment(ARate: Float; NPeriods: Integer;
  2204. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  2205. var
  2206. q, qn, factor: Float;
  2207. begin
  2208. if ARate = 0 then
  2209. Result := -(AFutureValue + APresentValue) / NPeriods
  2210. else begin
  2211. q := 1.0 + ARate;
  2212. qn := power(q, NPeriods);
  2213. factor := (qn - 1) / (q - 1);
  2214. if APaymentTime = ptStartOfPeriod then
  2215. factor := factor * q;
  2216. Result := -(AFutureValue + APresentValue * qn) / factor;
  2217. end;
  2218. end;
  2219. function PresentValue(ARate: Float; NPeriods: Integer;
  2220. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  2221. var
  2222. q, qn, factor: Float;
  2223. begin
  2224. if ARate = 0.0 then
  2225. Result := -AFutureValue - APayment * NPeriods
  2226. else begin
  2227. q := 1.0 + ARate;
  2228. qn := power(q, NPeriods);
  2229. factor := (qn - 1) / (q - 1);
  2230. if APaymentTime = ptStartOfPeriod then
  2231. factor := factor * q;
  2232. Result := -(AFutureValue + APayment*factor) / qn;
  2233. end;
  2234. end;
  2235. {$else}
  2236. implementation
  2237. {$endif FPUNONE}
  2238. end.