math.pp 71 KB

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