math.pp 68 KB

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