math.pp 68 KB

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