math.pp 68 KB

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