math.pp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  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. This unit is an equivalent to the Delphi math unit
  13. (with some improvements)
  14. What's to do:
  15. o some statistical functions
  16. o all financial functions
  17. o optimizations
  18. }
  19. {$MODE objfpc}
  20. {$inline on }
  21. unit math;
  22. interface
  23. uses
  24. sysutils;
  25. { Ranges of the IEEE floating point types, including denormals }
  26. {$ifdef FPC_HAS_TYPE_SINGLE}
  27. const
  28. MinSingle = 1.5e-45;
  29. MaxSingle = 3.4e+38;
  30. {$endif FPC_HAS_TYPE_SINGLE}
  31. {$ifdef FPC_HAS_TYPE_DOUBLE}
  32. const
  33. MinDouble = 5.0e-324;
  34. MaxDouble = 1.7e+308;
  35. {$endif FPC_HAS_TYPE_DOUBLE}
  36. {$ifdef FPC_HAS_TYPE_EXTENDED}
  37. const
  38. MinExtended = 3.4e-4932;
  39. MaxExtended = 1.1e+4932;
  40. {$endif FPC_HAS_TYPE_EXTENDED}
  41. {$ifdef FPC_HAS_TYPE_COMP}
  42. const
  43. MinComp = -9.223372036854775807e+18;
  44. MaxComp = 9.223372036854775807e+18;
  45. {$endif FPC_HAS_TYPE_COMP}
  46. { the original delphi functions use extended as argument, }
  47. { but I would prefer double, because 8 bytes is a very }
  48. { natural size for the processor }
  49. { WARNING : changing float type will }
  50. { break all assembler code PM }
  51. {$ifdef FPC_HAS_TYPE_FLOAT128}
  52. type
  53. float = float128;
  54. const
  55. MinFloat = MinFloat128;
  56. MaxFloat = MaxFloat128;
  57. {$else FPC_HAS_TYPE_FLOAT128}
  58. {$ifdef FPC_HAS_TYPE_EXTENDED}
  59. type
  60. float = extended;
  61. const
  62. MinFloat = MinExtended;
  63. MaxFloat = MaxExtended;
  64. {$else FPC_HAS_TYPE_EXTENDED}
  65. {$ifdef FPC_HAS_TYPE_DOUBLE}
  66. type
  67. float = double;
  68. const
  69. MinFloat = MinDouble;
  70. MaxFloat = MaxDouble;
  71. {$else FPC_HAS_TYPE_DOUBLE}
  72. {$ifdef FPC_HAS_TYPE_SINGLE}
  73. type
  74. float = single;
  75. const
  76. MinFloat = MinSingle;
  77. MaxFloat = MaxSingle;
  78. {$else FPC_HAS_TYPE_SINGLE}
  79. {$fatal At least one floating point type must be supported}
  80. {$endif FPC_HAS_TYPE_SINGLE}
  81. {$endif FPC_HAS_TYPE_DOUBLE}
  82. {$endif FPC_HAS_TYPE_EXTENDED}
  83. {$endif FPC_HAS_TYPE_FLOAT128}
  84. type
  85. PFloat = ^Float;
  86. PInteger = ^Integer;
  87. tpaymenttime = (ptendofperiod,ptstartofperiod);
  88. einvalidargument = class(ematherror);
  89. TValueRelationship = -1..1;
  90. const
  91. EqualsValue = 0;
  92. LessThanValue = Low(TValueRelationship);
  93. GreaterThanValue = High(TValueRelationship);
  94. {$ifopt R+}
  95. {$define RangeCheckWasOn}
  96. {$R-}
  97. {$endif opt R+}
  98. {$ifopt Q+}
  99. {$define OverflowCheckWasOn}
  100. {$Q-}
  101. {$endif opt Q+}
  102. {$if defined(CPUARM) and defined(unix)}
  103. { the ARM linux emulator doesn't like 0.0/0.0 }
  104. NaN = ln(-1.0);
  105. {$else CPUARM}
  106. NaN = 0.0/0.0;
  107. {$endif CPUARM}
  108. Infinity = 1.0/0.0;
  109. {$ifdef RangeCheckWasOn}
  110. {$R+}
  111. {$undef RangeCheckWasOn}
  112. {$endif}
  113. {$ifdef OverflowCheckWasOn}
  114. {$Q+}
  115. {$undef OverflowCheckWasOn}
  116. {$endif}
  117. { Min/max determination }
  118. function MinIntValue(const Data: array of Integer): Integer;
  119. function MaxIntValue(const Data: array of Integer): Integer;
  120. { Extra, not present in Delphi, but used frequently }
  121. function Min(a, b: Integer): Integer;inline;
  122. function Max(a, b: Integer): Integer;inline;
  123. { this causes more trouble than it solves
  124. function Min(a, b: Cardinal): Cardinal;
  125. function Max(a, b: Cardinal): Cardinal;
  126. }
  127. function Min(a, b: Int64): Int64;inline;
  128. function Max(a, b: Int64): Int64;inline;
  129. {$ifdef FPC_HAS_TYPE_SINGLE}
  130. function Min(a, b: Single): Single;inline;
  131. function Max(a, b: Single): Single;inline;
  132. {$endif FPC_HAS_TYPE_SINGLE}
  133. {$ifdef FPC_HAS_TYPE_DOUBLE}
  134. function Min(a, b: Double): Double;inline;
  135. function Max(a, b: Double): Double;inline;
  136. {$endif FPC_HAS_TYPE_DOUBLE}
  137. {$ifdef FPC_HAS_TYPE_EXTENDED}
  138. function Min(a, b: Extended): Extended;inline;
  139. function Max(a, b: Extended): Extended;inline;
  140. {$endif FPC_HAS_TYPE_EXTENDED}
  141. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  142. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  143. {$ifdef FPC_HAS_TYPE_DOUBLE}
  144. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  145. {$endif FPC_HAS_TYPE_DOUBLE}
  146. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  147. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  148. {$ifdef FPC_HAS_TYPE_DOUBLE}
  149. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  150. {$endif FPC_HAS_TYPE_DOUBLE}
  151. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  152. // Sign functions
  153. Type
  154. TValueSign = -1..1;
  155. const
  156. NegativeValue = Low(TValueSign);
  157. ZeroValue = 0;
  158. PositiveValue = High(TValueSign);
  159. function Sign(const AValue: Integer): TValueSign;inline;
  160. function Sign(const AValue: Int64): TValueSign;inline;
  161. function Sign(const AValue: Double): TValueSign;inline;
  162. function IsZero(const A: Single; Epsilon: Single): Boolean;
  163. function IsZero(const A: Single): Boolean;inline;
  164. {$ifdef FPC_HAS_TYPE_DOUBLE}
  165. function IsZero(const A: Double; Epsilon: Double): Boolean;
  166. function IsZero(const A: Double): Boolean;inline;
  167. {$endif FPC_HAS_TYPE_DOUBLE}
  168. {$ifdef FPC_HAS_TYPE_EXTENDED}
  169. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  170. function IsZero(const A: Extended): Boolean;inline;
  171. {$endif FPC_HAS_TYPE_EXTENDED}
  172. function IsNan(const d : Double): Boolean;
  173. function IsInfinite(const d : Double): Boolean;
  174. {$ifdef FPC_HAS_TYPE_EXTENDED}
  175. function SameValue(const A, B: Extended): Boolean;inline;
  176. {$endif}
  177. {$ifdef FPC_HAS_TYPE_DOUBLE}
  178. function SameValue(const A, B: Double): Boolean;inline;
  179. {$endif}
  180. function SameValue(const A, B: Single): Boolean;inline;
  181. {$ifdef FPC_HAS_TYPE_EXTENDED}
  182. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  183. {$endif}
  184. {$ifdef FPC_HAS_TYPE_DOUBLE}
  185. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  186. {$endif}
  187. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  188. { angle conversion }
  189. function degtorad(deg : float) : float;
  190. function radtodeg(rad : float) : float;
  191. function gradtorad(grad : float) : float;
  192. function radtograd(rad : float) : float;
  193. function degtograd(deg : float) : float;
  194. function gradtodeg(grad : float) : float;
  195. { one cycle are 2*Pi rad }
  196. function cycletorad(cycle : float) : float;
  197. function radtocycle(rad : float) : float;
  198. { trigoniometric functions }
  199. function tan(x : float) : float;
  200. function cotan(x : float) : float;
  201. procedure sincos(theta : float;var sinus,cosinus : float);
  202. { inverse functions }
  203. function arccos(x : float) : float;
  204. function arcsin(x : float) : float;
  205. { calculates arctan(y/x) and returns an angle in the correct quadrant }
  206. function arctan2(y,x : float) : float;
  207. { hyperbolic functions }
  208. function cosh(x : float) : float;
  209. function sinh(x : float) : float;
  210. function tanh(x : float) : float;
  211. { area functions }
  212. { delphi names: }
  213. function arccosh(x : float) : float;
  214. function arcsinh(x : float) : float;
  215. function arctanh(x : float) : float;
  216. { IMHO the function should be called as follows (FK) }
  217. function arcosh(x : float) : float;
  218. function arsinh(x : float) : float;
  219. function artanh(x : float) : float;
  220. { triangle functions }
  221. { returns the length of the hypotenuse of a right triangle }
  222. { if x and y are the other sides }
  223. function hypot(x,y : float) : float;
  224. { logarithm functions }
  225. function log10(x : float) : float;
  226. function log2(x : float) : float;
  227. function logn(n,x : float) : float;
  228. { returns natural logarithm of x+1 }
  229. function lnxp1(x : float) : float;
  230. { exponential functions }
  231. function power(base,exponent : float) : float;
  232. { base^exponent }
  233. function intpower(base : float;const exponent : Integer) : float;
  234. operator ** (bas,expo : float) e: float;
  235. operator ** (bas,expo : int64) i: int64;
  236. { number converting }
  237. { rounds x towards positive infinity }
  238. function ceil(x : float) : Integer;
  239. { rounds x towards negative infinity }
  240. function floor(x : float) : Integer;
  241. { misc. functions }
  242. { splits x into mantissa and exponent (to base 2) }
  243. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  244. { returns x*(2^p) }
  245. function ldexp(x : float; const p : Integer) : float;
  246. { statistical functions }
  247. {$ifdef FPC_HAS_TYPE_SINGLE}
  248. function mean(const data : array of Single) : float;
  249. function sum(const data : array of Single) : float;
  250. function mean(const data : PSingle; Const N : longint) : float;
  251. function sum(const data : PSingle; Const N : Longint) : float;
  252. {$endif FPC_HAS_TYPE_SINGLE}
  253. {$ifdef FPC_HAS_TYPE_DOUBLE}
  254. function mean(const data : array of double) : float;
  255. function sum(const data : array of double) : float;
  256. function mean(const data : PDouble; Const N : longint) : float;
  257. function sum(const data : PDouble; Const N : Longint) : float;
  258. {$endif FPC_HAS_TYPE_DOUBLE}
  259. {$ifdef FPC_HAS_TYPE_EXTENDED}
  260. function mean(const data : array of Extended) : float;
  261. function sum(const data : array of Extended) : float;
  262. function mean(const data : PExtended; Const N : longint) : float;
  263. function sum(const data : PExtended; Const N : Longint) : float;
  264. {$endif FPC_HAS_TYPE_EXTENDED}
  265. function sumInt(const data : PInt64;Const N : longint) : Int64;
  266. function sumInt(const data : array of Int64) : Int64;
  267. {$ifdef FPC_HAS_TYPE_SINGLE}
  268. function sumofsquares(const data : array of Single) : float;
  269. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  270. { calculates the sum and the sum of squares of data }
  271. procedure sumsandsquares(const data : array of Single;
  272. var sum,sumofsquares : float);
  273. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  274. var sum,sumofsquares : float);
  275. {$endif FPC_HAS_TYPE_SINGLE}
  276. {$ifdef FPC_HAS_TYPE_DOUBLE}
  277. function sumofsquares(const data : array of double) : float;
  278. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  279. { calculates the sum and the sum of squares of data }
  280. procedure sumsandsquares(const data : array of Double;
  281. var sum,sumofsquares : float);
  282. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  283. var sum,sumofsquares : float);
  284. {$endif FPC_HAS_TYPE_DOUBLE}
  285. {$ifdef FPC_HAS_TYPE_EXTENDED}
  286. function sumofsquares(const data : array of Extended) : float;
  287. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  288. { calculates the sum and the sum of squares of data }
  289. procedure sumsandsquares(const data : array of Extended;
  290. var sum,sumofsquares : float);
  291. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  292. var sum,sumofsquares : float);
  293. {$endif FPC_HAS_TYPE_EXTENDED}
  294. {$ifdef FPC_HAS_TYPE_SINGLE}
  295. function minvalue(const data : array of Single) : Single;
  296. function minvalue(const data : PSingle; Const N : Integer) : Single;
  297. function maxvalue(const data : array of Single) : Single;
  298. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  299. {$endif FPC_HAS_TYPE_SINGLE}
  300. {$ifdef FPC_HAS_TYPE_DOUBLE}
  301. function minvalue(const data : array of Double) : Double;
  302. function minvalue(const data : PDouble; Const N : Integer) : Double;
  303. function maxvalue(const data : array of Double) : Double;
  304. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  305. {$endif FPC_HAS_TYPE_DOUBLE}
  306. {$ifdef FPC_HAS_TYPE_EXTENDED}
  307. function minvalue(const data : array of Extended) : Extended;
  308. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  309. function maxvalue(const data : array of Extended) : Extended;
  310. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  311. {$endif FPC_HAS_TYPE_EXTENDED}
  312. function minvalue(const data : array of integer) : Integer;
  313. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  314. function maxvalue(const data : array of integer) : Integer;
  315. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  316. { returns random values with gaussian distribution }
  317. function randg(mean,stddev : float) : float;
  318. {$ifdef FPC_HAS_TYPE_SINGLE}
  319. { calculates the standard deviation }
  320. function stddev(const data : array of Single) : float;
  321. function stddev(const data : PSingle; Const N : Integer) : float;
  322. { calculates the mean and stddev }
  323. procedure meanandstddev(const data : array of Single;
  324. var mean,stddev : float);
  325. procedure meanandstddev(const data : PSingle;
  326. Const N : Longint;var mean,stddev : float);
  327. function variance(const data : array of Single) : float;
  328. function totalvariance(const data : array of Single) : float;
  329. function variance(const data : PSingle; Const N : Integer) : float;
  330. function totalvariance(const data : PSingle; Const N : Integer) : float;
  331. { I don't know what the following functions do: }
  332. function popnstddev(const data : array of Single) : float;
  333. function popnstddev(const data : PSingle; Const N : Integer) : float;
  334. function popnvariance(const data : PSingle; Const N : Integer) : float;
  335. function popnvariance(const data : array of Single) : float;
  336. procedure momentskewkurtosis(const data : array of Single;
  337. var m1,m2,m3,m4,skew,kurtosis : float);
  338. procedure momentskewkurtosis(const data : PSingle; Const N : Integer;
  339. var m1,m2,m3,m4,skew,kurtosis : float);
  340. { geometrical function }
  341. { returns the euclidean L2 norm }
  342. function norm(const data : array of Single) : float;
  343. function norm(const data : PSingle; Const N : Integer) : float;
  344. {$endif FPC_HAS_TYPE_SINGLE}
  345. {$ifdef FPC_HAS_TYPE_DOUBLE}
  346. { calculates the standard deviation }
  347. function stddev(const data : array of Double) : float;
  348. function stddev(const data : PDouble; Const N : Integer) : float;
  349. { calculates the mean and stddev }
  350. procedure meanandstddev(const data : array of Double;
  351. var mean,stddev : float);
  352. procedure meanandstddev(const data : PDouble;
  353. Const N : Longint;var mean,stddev : float);
  354. function variance(const data : array of Double) : float;
  355. function totalvariance(const data : array of Double) : float;
  356. function variance(const data : PDouble; Const N : Integer) : float;
  357. function totalvariance(const data : PDouble; Const N : Integer) : float;
  358. { I don't know what the following functions do: }
  359. function popnstddev(const data : array of Double) : float;
  360. function popnstddev(const data : PDouble; Const N : Integer) : float;
  361. function popnvariance(const data : PDouble; Const N : Integer) : float;
  362. function popnvariance(const data : array of Double) : float;
  363. procedure momentskewkurtosis(const data : array of Double;
  364. var m1,m2,m3,m4,skew,kurtosis : float);
  365. procedure momentskewkurtosis(const data : PDouble; Const N : Integer;
  366. var m1,m2,m3,m4,skew,kurtosis : float);
  367. { geometrical function }
  368. { returns the euclidean L2 norm }
  369. function norm(const data : array of double) : float;
  370. function norm(const data : PDouble; Const N : Integer) : float;
  371. {$endif FPC_HAS_TYPE_DOUBLE}
  372. {$ifdef FPC_HAS_TYPE_EXTENDED}
  373. { calculates the standard deviation }
  374. function stddev(const data : array of Extended) : float;
  375. function stddev(const data : PExtended; Const N : Integer) : float;
  376. { calculates the mean and stddev }
  377. procedure meanandstddev(const data : array of Extended;
  378. var mean,stddev : float);
  379. procedure meanandstddev(const data : PExtended;
  380. Const N : Longint;var mean,stddev : float);
  381. function variance(const data : array of Extended) : float;
  382. function totalvariance(const data : array of Extended) : float;
  383. function variance(const data : PExtended; Const N : Integer) : float;
  384. function totalvariance(const data : PExtended; Const N : Integer) : float;
  385. { I don't know what the following functions do: }
  386. function popnstddev(const data : array of Extended) : float;
  387. function popnstddev(const data : PExtended; Const N : Integer) : float;
  388. function popnvariance(const data : PExtended; Const N : Integer) : float;
  389. function popnvariance(const data : array of Extended) : float;
  390. procedure momentskewkurtosis(const data : array of Extended;
  391. var m1,m2,m3,m4,skew,kurtosis : float);
  392. procedure momentskewkurtosis(const data : PExtended; Const N : Integer;
  393. var m1,m2,m3,m4,skew,kurtosis : float);
  394. { geometrical function }
  395. { returns the euclidean L2 norm }
  396. function norm(const data : array of Extended) : float;
  397. function norm(const data : PExtended; Const N : Integer) : float;
  398. {$endif FPC_HAS_TYPE_EXTENDED}
  399. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer; {$ifdef MATHINLINE}inline; {$endif}
  400. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64; {$ifdef MATHINLINE}inline; {$endif}
  401. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double; {$ifdef MATHINLINE}inline; {$endif}
  402. function CompareValue ( const A, B : Integer) : TValueRelationship; inline;
  403. function CompareValue ( const A, B : Int64) : TValueRelationship; inline;
  404. {$ifdef FPC_HAS_TYPE_SINGLE}
  405. function CompareValue ( const A, B : Single; delta : Single = 0.0 ) : TValueRelationship; inline;
  406. {$endif}
  407. {$ifdef FPC_HAS_TYPE_DOUBLE}
  408. function CompareValue ( const A, B : Double; delta : Double = 0.0) : TValueRelationship; inline;
  409. {$endif}
  410. {$ifdef FPC_HAS_TYPE_EXTENDED}
  411. function CompareValue ( const A, B : Extended; delta : Extended = 0.0 ) : TValueRelationship; inline;
  412. {$endif}
  413. { include cpu specific stuff }
  414. {$i mathuh.inc}
  415. implementation
  416. { include cpu specific stuff }
  417. {$i mathu.inc}
  418. ResourceString
  419. SMathError = 'Math Error : %s';
  420. SInvalidArgument = 'Invalid argument';
  421. Procedure DoMathError(Const S : String);
  422. begin
  423. Raise EMathError.CreateFmt(SMathError,[S]);
  424. end;
  425. Procedure InvalidArgument;
  426. begin
  427. Raise EInvalidArgument.Create(SInvalidArgument);
  428. end;
  429. function Sign(const AValue: Integer): TValueSign;inline;
  430. begin
  431. If Avalue<0 then
  432. Result:=NegativeValue
  433. else If Avalue>0 then
  434. Result:=PositiveValue
  435. else
  436. Result:=ZeroValue;
  437. end;
  438. function Sign(const AValue: Int64): TValueSign;inline;
  439. begin
  440. If Avalue<0 then
  441. Result:=NegativeValue
  442. else If Avalue>0 then
  443. Result:=PositiveValue
  444. else
  445. Result:=ZeroValue;
  446. end;
  447. function Sign(const AValue: Double): TValueSign;inline;
  448. begin
  449. If Avalue<0.0 then
  450. Result:=NegativeValue
  451. else If Avalue>0.0 then
  452. Result:=PositiveValue
  453. else
  454. Result:=ZeroValue;
  455. end;
  456. function degtorad(deg : float) : float;
  457. begin
  458. degtorad:=deg*(pi/180.0);
  459. end;
  460. function radtodeg(rad : float) : float;
  461. begin
  462. radtodeg:=rad*(180.0/pi);
  463. end;
  464. function gradtorad(grad : float) : float;
  465. begin
  466. gradtorad:=grad*(pi/200.0);
  467. end;
  468. function radtograd(rad : float) : float;
  469. begin
  470. radtograd:=rad*(200.0/pi);
  471. end;
  472. function degtograd(deg : float) : float;
  473. begin
  474. degtograd:=deg*(200.0/180.0);
  475. end;
  476. function gradtodeg(grad : float) : float;
  477. begin
  478. gradtodeg:=grad*(180.0/200.0);
  479. end;
  480. function cycletorad(cycle : float) : float;
  481. begin
  482. cycletorad:=(2*pi)*cycle;
  483. end;
  484. function radtocycle(rad : float) : float;
  485. begin
  486. { avoid division }
  487. radtocycle:=rad*(1/(2*pi));
  488. end;
  489. function tan(x : float) : float;
  490. begin
  491. Tan:=Sin(x)/Cos(x)
  492. end;
  493. function cotan(x : float) : float;
  494. begin
  495. cotan:=Cos(X)/Sin(X);
  496. end;
  497. procedure sincos(theta : float;var sinus,cosinus : float);
  498. begin
  499. sinus:=sin(theta);
  500. cosinus:=cos(theta);
  501. end;
  502. { ArcSin and ArcCos from Arjan van Dijk ([email protected]) }
  503. function arcsin(x : float) : float;
  504. begin
  505. if abs(x) > 1 then InvalidArgument
  506. else if abs(x) < 0.5 then
  507. arcsin := arctan(x/sqrt(1-sqr(x)))
  508. else
  509. arcsin := sign(x) * (pi*0.5 - arctan(sqrt(1 / sqr(x) - 1)));
  510. end;
  511. function Arccos(x : Float) : Float;
  512. begin
  513. arccos := pi*0.5 - arcsin(x);
  514. end;
  515. {$ifndef FPC_MATH_HAS_ARCTAN2}
  516. function arctan2(y,x : float) : float;
  517. begin
  518. if (x=0) then
  519. begin
  520. if y=0 then
  521. arctan2:=0.0
  522. else if y>0 then
  523. arctan2:=pi/2
  524. else if y<0 then
  525. arctan2:=-pi/2;
  526. end
  527. else
  528. ArcTan2:=ArcTan(y/x);
  529. if x<0.0 then
  530. ArcTan2:=ArcTan2+pi;
  531. if ArcTan2>pi then
  532. ArcTan2:=ArcTan2-2*pi;
  533. end;
  534. {$endif FPC_MATH_HAS_ARCTAN2}
  535. function cosh(x : float) : float;
  536. var
  537. temp : float;
  538. begin
  539. temp:=exp(x);
  540. cosh:=0.5*(temp+1.0/temp);
  541. end;
  542. function sinh(x : float) : float;
  543. var
  544. temp : float;
  545. begin
  546. temp:=exp(x);
  547. sinh:=0.5*(temp-1.0/temp);
  548. end;
  549. Const MaxTanh = 5678.22249441322; // Ln(MaxExtended)/2
  550. function tanh(x : float) : float;
  551. var Temp : float;
  552. begin
  553. if x>MaxTanh then exit(1.0)
  554. else if x<-MaxTanh then exit (-1.0);
  555. temp:=exp(-2*x);
  556. tanh:=(1-temp)/(1+temp)
  557. end;
  558. function arccosh(x : float) : float;
  559. begin
  560. arccosh:=arcosh(x);
  561. end;
  562. function arcsinh(x : float) : float;
  563. begin
  564. arcsinh:=arsinh(x);
  565. end;
  566. function arctanh(x : float) : float;
  567. begin
  568. if x>1 then InvalidArgument;
  569. arctanh:=artanh(x);
  570. end;
  571. function arcosh(x : float) : float;
  572. begin
  573. if x<1 then InvalidArgument;
  574. arcosh:=Ln(x+Sqrt(x*x-1));
  575. end;
  576. function arsinh(x : float) : float;
  577. begin
  578. arsinh:=Ln(x+Sqrt(1+x*x));
  579. end;
  580. function artanh(x : float) : float;
  581. begin
  582. If abs(x)>1 then InvalidArgument;
  583. artanh:=(Ln((1+x)/(1-x)))*0.5;
  584. end;
  585. function hypot(x,y : float) : float;
  586. begin
  587. hypot:=Sqrt(x*x+y*y)
  588. end;
  589. function log10(x : float) : float;
  590. begin
  591. log10:=ln(x)/ln(10);
  592. end;
  593. function log2(x : float) : float;
  594. begin
  595. log2:=ln(x)/ln(2)
  596. end;
  597. function logn(n,x : float) : float;
  598. begin
  599. if n<0 then InvalidArgument;
  600. logn:=ln(x)/ln(n);
  601. end;
  602. function lnxp1(x : float) : float;
  603. begin
  604. if x<-1 then
  605. InvalidArgument;
  606. lnxp1:=ln(1+x);
  607. end;
  608. function power(base,exponent : float) : float;
  609. begin
  610. if Exponent=0.0 then
  611. if base <> 0.0 then
  612. result:=1.0
  613. else
  614. InvalidArgument
  615. else if (base=0.0) and (exponent>0.0) then
  616. result:=0.0
  617. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  618. result:=intpower(base,trunc(exponent))
  619. else if base>0.0 then
  620. result:=exp(exponent * ln (base))
  621. else
  622. InvalidArgument;
  623. end;
  624. function intpower(base : float;const exponent : Integer) : float;
  625. var
  626. i : longint;
  627. begin
  628. if (base = 0.0) and (exponent = 0) then
  629. InvalidArgument;
  630. i:=abs(exponent);
  631. intpower:=1.0;
  632. while i>0 do
  633. begin
  634. while (i and 1)=0 do
  635. begin
  636. i:=i shr 1;
  637. base:=sqr(base);
  638. end;
  639. i:=i-1;
  640. intpower:=intpower*base;
  641. end;
  642. if exponent<0 then
  643. intpower:=1.0/intpower;
  644. end;
  645. operator ** (bas,expo : float) e: float;
  646. begin
  647. e:=power(bas,expo);
  648. end;
  649. operator ** (bas,expo : int64) i: int64;
  650. begin
  651. i:=round(intpower(bas,expo));
  652. end;
  653. function ceil(x : float) : integer;
  654. begin
  655. Ceil:=Trunc(x);
  656. If Frac(x)>0 then
  657. Ceil:=Ceil+1;
  658. end;
  659. function floor(x : float) : integer;
  660. begin
  661. Floor:=Trunc(x);
  662. If Frac(x)<0 then
  663. Floor := Floor-1;
  664. end;
  665. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  666. begin
  667. Exponent:=0;
  668. if (X<>0) then
  669. if (abs(X)<0.5) then
  670. repeat
  671. X:=X*2;
  672. Dec(Exponent);
  673. until (abs(X)>=0.5)
  674. else
  675. while (abs(X)>=1) do
  676. begin
  677. X:=X/2;
  678. Inc(Exponent);
  679. end;
  680. Mantissa:=X;
  681. end;
  682. function ldexp(x : float;const p : Integer) : float;
  683. begin
  684. ldexp:=x*intpower(2.0,p);
  685. end;
  686. {$ifdef FPC_HAS_TYPE_SINGLE}
  687. function mean(const data : array of Single) : float;
  688. begin
  689. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  690. end;
  691. function mean(const data : PSingle; Const N : longint) : float;
  692. begin
  693. mean:=sum(Data,N);
  694. mean:=mean/N;
  695. end;
  696. function sum(const data : array of Single) : float;
  697. begin
  698. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  699. end;
  700. function sum(const data : PSingle;Const N : longint) : float;
  701. var
  702. i : longint;
  703. begin
  704. sum:=0.0;
  705. for i:=0 to N-1 do
  706. sum:=sum+data[i];
  707. end;
  708. {$endif FPC_HAS_TYPE_SINGLE}
  709. {$ifdef FPC_HAS_TYPE_DOUBLE}
  710. function mean(const data : array of Double) : float;
  711. begin
  712. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  713. end;
  714. function mean(const data : PDouble; Const N : longint) : float;
  715. begin
  716. mean:=sum(Data,N);
  717. mean:=mean/N;
  718. end;
  719. function sum(const data : array of Double) : float;
  720. begin
  721. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  722. end;
  723. function sum(const data : PDouble;Const N : longint) : float;
  724. var
  725. i : longint;
  726. begin
  727. sum:=0.0;
  728. for i:=0 to N-1 do
  729. sum:=sum+data[i];
  730. end;
  731. {$endif FPC_HAS_TYPE_DOUBLE}
  732. {$ifdef FPC_HAS_TYPE_EXTENDED}
  733. function mean(const data : array of Extended) : float;
  734. begin
  735. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  736. end;
  737. function mean(const data : PExtended; Const N : longint) : float;
  738. begin
  739. mean:=sum(Data,N);
  740. mean:=mean/N;
  741. end;
  742. function sum(const data : array of Extended) : float;
  743. begin
  744. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  745. end;
  746. function sum(const data : PExtended;Const N : longint) : float;
  747. var
  748. i : longint;
  749. begin
  750. sum:=0.0;
  751. for i:=0 to N-1 do
  752. sum:=sum+data[i];
  753. end;
  754. {$endif FPC_HAS_TYPE_EXTENDED}
  755. function sumInt(const data : PInt64;Const N : longint) : Int64;
  756. var
  757. i : longint;
  758. begin
  759. sumInt:=0;
  760. for i:=0 to N-1 do
  761. sumInt:=sumInt+data[i];
  762. end;
  763. function sumInt(const data : array of Int64) : Int64;
  764. begin
  765. Result:=SumInt(@Data[0],High(Data)+1);
  766. end;
  767. {$ifdef FPC_HAS_TYPE_SINGLE}
  768. function sumofsquares(const data : array of Single) : float;
  769. begin
  770. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  771. end;
  772. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  773. var
  774. i : longint;
  775. begin
  776. sumofsquares:=0.0;
  777. for i:=0 to N-1 do
  778. sumofsquares:=sumofsquares+sqr(data[i]);
  779. end;
  780. procedure sumsandsquares(const data : array of Single;
  781. var sum,sumofsquares : float);
  782. begin
  783. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  784. end;
  785. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  786. var sum,sumofsquares : float);
  787. var
  788. i : Integer;
  789. temp : float;
  790. begin
  791. sumofsquares:=0.0;
  792. sum:=0.0;
  793. for i:=0 to N-1 do
  794. begin
  795. temp:=data[i];
  796. sumofsquares:=sumofsquares+sqr(temp);
  797. sum:=sum+temp;
  798. end;
  799. end;
  800. {$endif FPC_HAS_TYPE_SINGLE}
  801. {$ifdef FPC_HAS_TYPE_DOUBLE}
  802. function sumofsquares(const data : array of Double) : float;
  803. begin
  804. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  805. end;
  806. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  807. var
  808. i : longint;
  809. begin
  810. sumofsquares:=0.0;
  811. for i:=0 to N-1 do
  812. sumofsquares:=sumofsquares+sqr(data[i]);
  813. end;
  814. procedure sumsandsquares(const data : array of Double;
  815. var sum,sumofsquares : float);
  816. begin
  817. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  818. end;
  819. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  820. var sum,sumofsquares : float);
  821. var
  822. i : Integer;
  823. temp : float;
  824. begin
  825. sumofsquares:=0.0;
  826. sum:=0.0;
  827. for i:=0 to N-1 do
  828. begin
  829. temp:=data[i];
  830. sumofsquares:=sumofsquares+sqr(temp);
  831. sum:=sum+temp;
  832. end;
  833. end;
  834. {$endif FPC_HAS_TYPE_DOUBLE}
  835. {$ifdef FPC_HAS_TYPE_EXTENDED}
  836. function sumofsquares(const data : array of Extended) : float;
  837. begin
  838. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  839. end;
  840. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  841. var
  842. i : longint;
  843. begin
  844. sumofsquares:=0.0;
  845. for i:=0 to N-1 do
  846. sumofsquares:=sumofsquares+sqr(data[i]);
  847. end;
  848. procedure sumsandsquares(const data : array of Extended;
  849. var sum,sumofsquares : float);
  850. begin
  851. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  852. end;
  853. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  854. var sum,sumofsquares : float);
  855. var
  856. i : Integer;
  857. temp : float;
  858. begin
  859. sumofsquares:=0.0;
  860. sum:=0.0;
  861. for i:=0 to N-1 do
  862. begin
  863. temp:=data[i];
  864. sumofsquares:=sumofsquares+sqr(temp);
  865. sum:=sum+temp;
  866. end;
  867. end;
  868. {$endif FPC_HAS_TYPE_EXTENDED}
  869. function randg(mean,stddev : float) : float;
  870. Var U1,S2 : Float;
  871. begin
  872. repeat
  873. u1:= 2*random-1;
  874. S2:=Sqr(U1)+sqr(2*random-1);
  875. until s2<1;
  876. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  877. end;
  878. {$ifdef FPC_HAS_TYPE_SINGLE}
  879. function stddev(const data : array of Single) : float;
  880. begin
  881. Result:=Stddev(PSingle(@Data[0]),High(Data)+1)
  882. end;
  883. function stddev(const data : PSingle; Const N : Integer) : float;
  884. begin
  885. StdDev:=Sqrt(Variance(Data,N));
  886. end;
  887. procedure meanandstddev(const data : array of Single;
  888. var mean,stddev : float);
  889. begin
  890. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  891. end;
  892. procedure meanandstddev(const data : PSingle;
  893. Const N : Longint;var mean,stddev : float);
  894. Var I : longint;
  895. begin
  896. Mean:=0;
  897. StdDev:=0;
  898. For I:=0 to N-1 do
  899. begin
  900. Mean:=Mean+Data[i];
  901. StdDev:=StdDev+Sqr(Data[i]);
  902. end;
  903. Mean:=Mean/N;
  904. StdDev:=(StdDev-N*Sqr(Mean));
  905. If N>1 then
  906. StdDev:=Sqrt(Stddev/(N-1))
  907. else
  908. StdDev:=0;
  909. end;
  910. function variance(const data : array of Single) : float;
  911. begin
  912. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  913. end;
  914. function variance(const data : PSingle; Const N : Integer) : float;
  915. begin
  916. If N=1 then
  917. Result:=0
  918. else
  919. Result:=TotalVariance(Data,N)/(N-1);
  920. end;
  921. function totalvariance(const data : array of Single) : float;
  922. begin
  923. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  924. end;
  925. function totalvariance(const data : PSingle;Const N : Integer) : float;
  926. var S,SS : Float;
  927. begin
  928. If N=1 then
  929. Result:=0
  930. else
  931. begin
  932. SumsAndSquares(Data,N,S,SS);
  933. Result := SS-Sqr(S)/N;
  934. end;
  935. end;
  936. function popnstddev(const data : array of Single) : float;
  937. begin
  938. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  939. end;
  940. function popnstddev(const data : PSingle; Const N : Integer) : float;
  941. begin
  942. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  943. end;
  944. function popnvariance(const data : array of Single) : float;
  945. begin
  946. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  947. end;
  948. function popnvariance(const data : PSingle; Const N : Integer) : float;
  949. begin
  950. PopnVariance:=TotalVariance(Data,N)/N;
  951. end;
  952. procedure momentskewkurtosis(const data : array of Single;
  953. var m1,m2,m3,m4,skew,kurtosis : float);
  954. begin
  955. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  956. end;
  957. procedure momentskewkurtosis(const data : PSingle; Const N : Integer;
  958. var m1,m2,m3,m4,skew,kurtosis : float);
  959. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  960. I : Longint;
  961. begin
  962. invN:=1.0/N;
  963. s:=0;
  964. ss:=0;
  965. sq:=0;
  966. sc:=0;
  967. for i:=0 to N-1 do
  968. begin
  969. temp:=Data[i]; { faster }
  970. S:=S+temp;
  971. acc:=sqr(temp);
  972. ss:=ss+acc;
  973. Acc:=acc*temp;
  974. Sc:=sc+acc;
  975. acc:=acc*temp;
  976. sq:=sq+acc;
  977. end;
  978. M1:=s*invN;
  979. M1S:=sqr(M1);
  980. S2N:=SS*invN;
  981. S3N:=SC*invN;
  982. M2:=S2N-M1S;
  983. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  984. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  985. Skew:=M3*power(M2,-3/2);
  986. Kurtosis:=M4 / Sqr(M2);
  987. end;
  988. function norm(const data : array of Single) : float;
  989. begin
  990. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  991. end;
  992. function norm(const data : PSingle; Const N : Integer) : float;
  993. begin
  994. norm:=sqrt(sumofsquares(data,N));
  995. end;
  996. {$endif FPC_HAS_TYPE_SINGLE}
  997. {$ifdef FPC_HAS_TYPE_DOUBLE}
  998. function stddev(const data : array of Double) : float;
  999. begin
  1000. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  1001. end;
  1002. function stddev(const data : PDouble; Const N : Integer) : float;
  1003. begin
  1004. StdDev:=Sqrt(Variance(Data,N));
  1005. end;
  1006. procedure meanandstddev(const data : array of Double;
  1007. var mean,stddev : float);
  1008. begin
  1009. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  1010. end;
  1011. procedure meanandstddev(const data : PDouble;
  1012. Const N : Longint;var mean,stddev : float);
  1013. Var I : longint;
  1014. begin
  1015. Mean:=0;
  1016. StdDev:=0;
  1017. For I:=0 to N-1 do
  1018. begin
  1019. Mean:=Mean+Data[i];
  1020. StdDev:=StdDev+Sqr(Data[i]);
  1021. end;
  1022. Mean:=Mean/N;
  1023. StdDev:=(StdDev-N*Sqr(Mean));
  1024. If N>1 then
  1025. StdDev:=Sqrt(Stddev/(N-1))
  1026. else
  1027. StdDev:=0;
  1028. end;
  1029. function variance(const data : array of Double) : float;
  1030. begin
  1031. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  1032. end;
  1033. function variance(const data : PDouble; Const N : Integer) : float;
  1034. begin
  1035. If N=1 then
  1036. Result:=0
  1037. else
  1038. Result:=TotalVariance(Data,N)/(N-1);
  1039. end;
  1040. function totalvariance(const data : array of Double) : float;
  1041. begin
  1042. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  1043. end;
  1044. function totalvariance(const data : PDouble;Const N : Integer) : float;
  1045. var S,SS : Float;
  1046. begin
  1047. If N=1 then
  1048. Result:=0
  1049. else
  1050. begin
  1051. SumsAndSquares(Data,N,S,SS);
  1052. Result := SS-Sqr(S)/N;
  1053. end;
  1054. end;
  1055. function popnstddev(const data : array of Double) : float;
  1056. begin
  1057. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  1058. end;
  1059. function popnstddev(const data : PDouble; Const N : Integer) : float;
  1060. begin
  1061. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1062. end;
  1063. function popnvariance(const data : array of Double) : float;
  1064. begin
  1065. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  1066. end;
  1067. function popnvariance(const data : PDouble; Const N : Integer) : float;
  1068. begin
  1069. PopnVariance:=TotalVariance(Data,N)/N;
  1070. end;
  1071. procedure momentskewkurtosis(const data : array of Double;
  1072. var m1,m2,m3,m4,skew,kurtosis : float);
  1073. begin
  1074. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1075. end;
  1076. procedure momentskewkurtosis(const data : PDouble; Const N : Integer;
  1077. var m1,m2,m3,m4,skew,kurtosis : float);
  1078. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  1079. I : Longint;
  1080. begin
  1081. invN:=1.0/N;
  1082. s:=0;
  1083. ss:=0;
  1084. sq:=0;
  1085. sc:=0;
  1086. for i:=0 to N-1 do
  1087. begin
  1088. temp:=Data[i]; { faster }
  1089. S:=S+temp;
  1090. acc:=sqr(temp);
  1091. ss:=ss+acc;
  1092. Acc:=acc*temp;
  1093. Sc:=sc+acc;
  1094. acc:=acc*temp;
  1095. sq:=sq+acc;
  1096. end;
  1097. M1:=s*invN;
  1098. M1S:=sqr(M1);
  1099. S2N:=SS*invN;
  1100. S3N:=SC*invN;
  1101. M2:=S2N-M1S;
  1102. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  1103. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  1104. Skew:=M3*power(M2,-3/2);
  1105. Kurtosis:=M4 / Sqr(M2);
  1106. end;
  1107. function norm(const data : array of Double) : float;
  1108. begin
  1109. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  1110. end;
  1111. function norm(const data : PDouble; Const N : Integer) : float;
  1112. begin
  1113. norm:=sqrt(sumofsquares(data,N));
  1114. end;
  1115. {$endif FPC_HAS_TYPE_DOUBLE}
  1116. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1117. function stddev(const data : array of Extended) : float;
  1118. begin
  1119. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  1120. end;
  1121. function stddev(const data : PExtended; Const N : Integer) : float;
  1122. begin
  1123. StdDev:=Sqrt(Variance(Data,N));
  1124. end;
  1125. procedure meanandstddev(const data : array of Extended;
  1126. var mean,stddev : float);
  1127. begin
  1128. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  1129. end;
  1130. procedure meanandstddev(const data : PExtended;
  1131. Const N : Longint;var mean,stddev : float);
  1132. Var I : longint;
  1133. begin
  1134. Mean:=0;
  1135. StdDev:=0;
  1136. For I:=0 to N-1 do
  1137. begin
  1138. Mean:=Mean+Data[i];
  1139. StdDev:=StdDev+Sqr(Data[i]);
  1140. end;
  1141. Mean:=Mean/N;
  1142. StdDev:=(StdDev-N*Sqr(Mean));
  1143. If N>1 then
  1144. StdDev:=Sqrt(Stddev/(N-1))
  1145. else
  1146. StdDev:=0;
  1147. end;
  1148. function variance(const data : array of Extended) : float;
  1149. begin
  1150. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  1151. end;
  1152. function variance(const data : PExtended; Const N : Integer) : float;
  1153. begin
  1154. If N=1 then
  1155. Result:=0
  1156. else
  1157. Result:=TotalVariance(Data,N)/(N-1);
  1158. end;
  1159. function totalvariance(const data : array of Extended) : float;
  1160. begin
  1161. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  1162. end;
  1163. function totalvariance(const data : PExtended;Const N : Integer) : float;
  1164. var S,SS : Float;
  1165. begin
  1166. If N=1 then
  1167. Result:=0
  1168. else
  1169. begin
  1170. SumsAndSquares(Data,N,S,SS);
  1171. Result := SS-Sqr(S)/N;
  1172. end;
  1173. end;
  1174. function popnstddev(const data : array of Extended) : float;
  1175. begin
  1176. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  1177. end;
  1178. function popnstddev(const data : PExtended; Const N : Integer) : float;
  1179. begin
  1180. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1181. end;
  1182. function popnvariance(const data : array of Extended) : float;
  1183. begin
  1184. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  1185. end;
  1186. function popnvariance(const data : PExtended; Const N : Integer) : float;
  1187. begin
  1188. PopnVariance:=TotalVariance(Data,N)/N;
  1189. end;
  1190. procedure momentskewkurtosis(const data : array of Extended;
  1191. var m1,m2,m3,m4,skew,kurtosis : float);
  1192. begin
  1193. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1194. end;
  1195. procedure momentskewkurtosis(const data : PExtended; Const N : Integer;
  1196. var m1,m2,m3,m4,skew,kurtosis : float);
  1197. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  1198. I : Longint;
  1199. begin
  1200. invN:=1.0/N;
  1201. s:=0;
  1202. ss:=0;
  1203. sq:=0;
  1204. sc:=0;
  1205. for i:=0 to N-1 do
  1206. begin
  1207. temp:=Data[i]; { faster }
  1208. S:=S+temp;
  1209. acc:=sqr(temp);
  1210. ss:=ss+acc;
  1211. Acc:=acc*temp;
  1212. Sc:=sc+acc;
  1213. acc:=acc*temp;
  1214. sq:=sq+acc;
  1215. end;
  1216. M1:=s*invN;
  1217. M1S:=sqr(M1);
  1218. S2N:=SS*invN;
  1219. S3N:=SC*invN;
  1220. M2:=S2N-M1S;
  1221. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  1222. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  1223. Skew:=M3*power(M2,-3/2);
  1224. Kurtosis:=M4 / Sqr(M2);
  1225. end;
  1226. function norm(const data : array of Extended) : float;
  1227. begin
  1228. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  1229. end;
  1230. function norm(const data : PExtended; Const N : Integer) : float;
  1231. begin
  1232. norm:=sqrt(sumofsquares(data,N));
  1233. end;
  1234. {$endif FPC_HAS_TYPE_EXTENDED}
  1235. function MinIntValue(const Data: array of Integer): Integer;
  1236. var
  1237. I: Integer;
  1238. begin
  1239. Result := Data[Low(Data)];
  1240. For I := Succ(Low(Data)) To High(Data) Do
  1241. If Data[I] < Result Then Result := Data[I];
  1242. end;
  1243. function MaxIntValue(const Data: array of Integer): Integer;
  1244. var
  1245. I: Integer;
  1246. begin
  1247. Result := Data[Low(Data)];
  1248. For I := Succ(Low(Data)) To High(Data) Do
  1249. If Data[I] > Result Then Result := Data[I];
  1250. end;
  1251. function MinValue(const Data: array of Integer): Integer;
  1252. begin
  1253. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  1254. end;
  1255. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  1256. var
  1257. I: Integer;
  1258. begin
  1259. Result := Data[0];
  1260. For I := 1 To N-1 do
  1261. If Data[I] < Result Then Result := Data[I];
  1262. end;
  1263. function MaxValue(const Data: array of Integer): Integer;
  1264. begin
  1265. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  1266. end;
  1267. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  1268. var
  1269. i : longint;
  1270. begin
  1271. { get an initial value }
  1272. maxvalue:=data[0];
  1273. for i:=1 to N-1 do
  1274. if data[i]>maxvalue then
  1275. maxvalue:=data[i];
  1276. end;
  1277. {$ifdef FPC_HAS_TYPE_SINGLE}
  1278. function minvalue(const data : array of Single) : Single;
  1279. begin
  1280. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  1281. end;
  1282. function minvalue(const data : PSingle; Const N : Integer) : Single;
  1283. var
  1284. i : longint;
  1285. begin
  1286. { get an initial value }
  1287. minvalue:=data[0];
  1288. for i:=1 to N-1 do
  1289. if data[i]<minvalue then
  1290. minvalue:=data[i];
  1291. end;
  1292. function maxvalue(const data : array of Single) : Single;
  1293. begin
  1294. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  1295. end;
  1296. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  1297. var
  1298. i : longint;
  1299. begin
  1300. { get an initial value }
  1301. maxvalue:=data[0];
  1302. for i:=1 to N-1 do
  1303. if data[i]>maxvalue then
  1304. maxvalue:=data[i];
  1305. end;
  1306. {$endif FPC_HAS_TYPE_SINGLE}
  1307. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1308. function minvalue(const data : array of Double) : Double;
  1309. begin
  1310. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  1311. end;
  1312. function minvalue(const data : PDouble; Const N : Integer) : Double;
  1313. var
  1314. i : longint;
  1315. begin
  1316. { get an initial value }
  1317. minvalue:=data[0];
  1318. for i:=1 to N-1 do
  1319. if data[i]<minvalue then
  1320. minvalue:=data[i];
  1321. end;
  1322. function maxvalue(const data : array of Double) : Double;
  1323. begin
  1324. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  1325. end;
  1326. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  1327. var
  1328. i : longint;
  1329. begin
  1330. { get an initial value }
  1331. maxvalue:=data[0];
  1332. for i:=1 to N-1 do
  1333. if data[i]>maxvalue then
  1334. maxvalue:=data[i];
  1335. end;
  1336. {$endif FPC_HAS_TYPE_DOUBLE}
  1337. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1338. function minvalue(const data : array of Extended) : Extended;
  1339. begin
  1340. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  1341. end;
  1342. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  1343. var
  1344. i : longint;
  1345. begin
  1346. { get an initial value }
  1347. minvalue:=data[0];
  1348. for i:=1 to N-1 do
  1349. if data[i]<minvalue then
  1350. minvalue:=data[i];
  1351. end;
  1352. function maxvalue(const data : array of Extended) : Extended;
  1353. begin
  1354. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  1355. end;
  1356. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  1357. var
  1358. i : longint;
  1359. begin
  1360. { get an initial value }
  1361. maxvalue:=data[0];
  1362. for i:=1 to N-1 do
  1363. if data[i]>maxvalue then
  1364. maxvalue:=data[i];
  1365. end;
  1366. {$endif FPC_HAS_TYPE_EXTENDED}
  1367. function Min(a, b: Integer): Integer;inline;
  1368. begin
  1369. if a < b then
  1370. Result := a
  1371. else
  1372. Result := b;
  1373. end;
  1374. function Max(a, b: Integer): Integer;inline;
  1375. begin
  1376. if a > b then
  1377. Result := a
  1378. else
  1379. Result := b;
  1380. end;
  1381. {
  1382. function Min(a, b: Cardinal): Cardinal;inline;
  1383. begin
  1384. if a < b then
  1385. Result := a
  1386. else
  1387. Result := b;
  1388. end;
  1389. function Max(a, b: Cardinal): Cardinal;inline;
  1390. begin
  1391. if a > b then
  1392. Result := a
  1393. else
  1394. Result := b;
  1395. end;
  1396. }
  1397. function Min(a, b: Int64): Int64;inline;
  1398. begin
  1399. if a < b then
  1400. Result := a
  1401. else
  1402. Result := b;
  1403. end;
  1404. function Max(a, b: Int64): Int64;inline;
  1405. begin
  1406. if a > b then
  1407. Result := a
  1408. else
  1409. Result := b;
  1410. end;
  1411. {$ifdef FPC_HAS_TYPE_SINGLE}
  1412. function Min(a, b: Single): Single;inline;
  1413. begin
  1414. if a < b then
  1415. Result := a
  1416. else
  1417. Result := b;
  1418. end;
  1419. function Max(a, b: Single): Single;inline;
  1420. begin
  1421. if a > b then
  1422. Result := a
  1423. else
  1424. Result := b;
  1425. end;
  1426. {$endif FPC_HAS_TYPE_SINGLE}
  1427. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1428. function Min(a, b: Double): Double;inline;
  1429. begin
  1430. if a < b then
  1431. Result := a
  1432. else
  1433. Result := b;
  1434. end;
  1435. function Max(a, b: Double): Double;inline;
  1436. begin
  1437. if a > b then
  1438. Result := a
  1439. else
  1440. Result := b;
  1441. end;
  1442. {$endif FPC_HAS_TYPE_DOUBLE}
  1443. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1444. function Min(a, b: Extended): Extended;inline;
  1445. begin
  1446. if a < b then
  1447. Result := a
  1448. else
  1449. Result := b;
  1450. end;
  1451. function Max(a, b: Extended): Extended;inline;
  1452. begin
  1453. if a > b then
  1454. Result := a
  1455. else
  1456. Result := b;
  1457. end;
  1458. {$endif FPC_HAS_TYPE_EXTENDED}
  1459. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  1460. begin
  1461. Result:=(AValue>=AMin) and (AValue<=AMax);
  1462. end;
  1463. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  1464. begin
  1465. Result:=(AValue>=AMin) and (AValue<=AMax);
  1466. end;
  1467. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1468. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  1469. begin
  1470. Result:=(AValue>=AMin) and (AValue<=AMax);
  1471. end;
  1472. {$endif FPC_HAS_TYPE_DOUBLE}
  1473. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  1474. begin
  1475. Result:=AValue;
  1476. If Result<AMin then
  1477. Result:=AMin
  1478. else if Result>AMax then
  1479. Result:=AMax;
  1480. end;
  1481. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  1482. begin
  1483. Result:=AValue;
  1484. If Result<AMin then
  1485. Result:=AMin
  1486. else if Result>AMax then
  1487. Result:=AMax;
  1488. end;
  1489. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1490. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  1491. begin
  1492. Result:=AValue;
  1493. If Result<AMin then
  1494. Result:=AMin
  1495. else if Result>AMax then
  1496. Result:=AMax;
  1497. end;
  1498. {$endif FPC_HAS_TYPE_DOUBLE}
  1499. Const
  1500. EZeroResolution = 1E-16;
  1501. DZeroResolution = 1E-12;
  1502. SZeroResolution = 1E-4;
  1503. function IsZero(const A: Single; Epsilon: Single): Boolean;
  1504. begin
  1505. if (Epsilon=0) then
  1506. Epsilon:=SZeroResolution;
  1507. Result:=Abs(A)<=Epsilon;
  1508. end;
  1509. function IsZero(const A: Single): Boolean;inline;
  1510. begin
  1511. Result:=IsZero(A,single(SZeroResolution));
  1512. end;
  1513. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1514. function IsZero(const A: Double; Epsilon: Double): Boolean;
  1515. begin
  1516. if (Epsilon=0) then
  1517. Epsilon:=DZeroResolution;
  1518. Result:=Abs(A)<=Epsilon;
  1519. end;
  1520. function IsZero(const A: Double): Boolean;inline;
  1521. begin
  1522. Result:=IsZero(A,DZeroResolution);
  1523. end;
  1524. {$endif FPC_HAS_TYPE_DOUBLE}
  1525. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1526. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  1527. begin
  1528. if (Epsilon=0) then
  1529. Epsilon:=EZeroResolution;
  1530. Result:=Abs(A)<=Epsilon;
  1531. end;
  1532. function IsZero(const A: Extended): Boolean;inline;
  1533. begin
  1534. Result:=IsZero(A,EZeroResolution);
  1535. end;
  1536. {$endif FPC_HAS_TYPE_EXTENDED}
  1537. type
  1538. TSplitDouble = packed record
  1539. cards: Array[0..1] of cardinal;
  1540. end;
  1541. function IsNan(const d : Double): Boolean;
  1542. var
  1543. fraczero, expMaximal: boolean;
  1544. begin
  1545. {$if defined(FPC_BIG_ENDIAN) or (defined(CPUARM) and defined(FPUFPA))}
  1546. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1547. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1548. (TSplitDouble(d).cards[1] = 0);
  1549. {$else FPC_BIG_ENDIAN}
  1550. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1551. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1552. (TSplitDouble(d).cards[0] = 0);
  1553. {$endif FPC_BIG_ENDIAN}
  1554. Result:=expMaximal and not(fraczero);
  1555. end;
  1556. function IsInfinite(const d : Double): Boolean;
  1557. var
  1558. fraczero, expMaximal: boolean;
  1559. begin
  1560. {$if defined(FPC_BIG_ENDIAN) or (defined(CPUARM) and defined(FPUFPA))}
  1561. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1562. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1563. (TSplitDouble(d).cards[1] = 0);
  1564. {$else FPC_BIG_ENDIAN}
  1565. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1566. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1567. (TSplitDouble(d).cards[0] = 0);
  1568. {$endif FPC_BIG_ENDIAN}
  1569. Result:=expMaximal and fraczero;
  1570. end;
  1571. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1572. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1573. begin
  1574. if (Epsilon=0) then
  1575. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1576. if (A>B) then
  1577. Result:=((A-B)<=Epsilon)
  1578. else
  1579. Result:=((B-A)<=Epsilon);
  1580. end;
  1581. function SameValue(const A, B: Extended): Boolean;inline;
  1582. begin
  1583. Result:=SameValue(A,B,0);
  1584. end;
  1585. {$endif FPC_HAS_TYPE_EXTENDED}
  1586. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1587. function SameValue(const A, B: Double): Boolean;inline;
  1588. begin
  1589. Result:=SameValue(A,B,0);
  1590. end;
  1591. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1592. begin
  1593. if (Epsilon=0) then
  1594. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1595. if (A>B) then
  1596. Result:=((A-B)<=Epsilon)
  1597. else
  1598. Result:=((B-A)<=Epsilon);
  1599. end;
  1600. {$endif FPC_HAS_TYPE_DOUBLE}
  1601. function SameValue(const A, B: Single): Boolean;inline;
  1602. begin
  1603. Result:=SameValue(A,B,0);
  1604. end;
  1605. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  1606. begin
  1607. if (Epsilon=0) then
  1608. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  1609. if (A>B) then
  1610. Result:=((A-B)<=Epsilon)
  1611. else
  1612. Result:=((B-A)<=Epsilon);
  1613. end;
  1614. // Some CPUs probably allow a faster way of doing this in a single operation...
  1615. // There weshould define CPUDIVMOD in the header mathuh.inc and implement it using asm.
  1616. {$ifndef CPUDIVMOD}
  1617. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  1618. begin
  1619. Result:=Dividend Div Divisor;
  1620. Remainder:=Dividend Mod Divisor;
  1621. end;
  1622. {$endif}
  1623. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  1624. begin
  1625. if val then result:=iftrue else result:=iffalse;
  1626. end;
  1627. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  1628. begin
  1629. if val then result:=iftrue else result:=iffalse;
  1630. end;
  1631. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  1632. begin
  1633. if val then result:=iftrue else result:=iffalse;
  1634. end;
  1635. // dilemma here. asm can do the two comparisons in one go?
  1636. // but pascal is portable and can be i inline;ed. Ah well, we need purepascal's anyway:
  1637. function CompareValue ( const A, B : Integer) : TValueRelationship;
  1638. begin
  1639. result:=GreaterThanValue;
  1640. if a=b then
  1641. result:=EqualsValue
  1642. else
  1643. if a<b then
  1644. result:=LessThanValue;
  1645. end;
  1646. function CompareValue ( const A, B : Int64) : TValueRelationship;
  1647. begin
  1648. result:=GreaterThanValue;
  1649. if a=b then
  1650. result:=EqualsValue
  1651. else
  1652. if a<b then
  1653. result:=LessThanValue;
  1654. end;
  1655. {$ifdef FPC_HAS_TYPE_SINGLE}
  1656. function CompareValue ( const A, B : Single; delta : Single = 0.0) : TValueRelationship;
  1657. begin
  1658. result:=GreaterThanValue;
  1659. if abs(a-b)<=delta then
  1660. result:=EqualsValue
  1661. else
  1662. if a<b then
  1663. result:=LessThanValue;
  1664. end;
  1665. {$endif}
  1666. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1667. function CompareValue ( const A, B : Double; delta : Double = 0.0) : TValueRelationship;
  1668. begin
  1669. result:=GreaterThanValue;
  1670. if abs(a-b)<=delta then
  1671. result:=EqualsValue
  1672. else
  1673. if a<b then
  1674. result:=LessThanValue;
  1675. end;
  1676. {$endif}
  1677. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1678. function CompareValue ( const A, B : Extended; delta : Extended = 0.0) : TValueRelationship;
  1679. begin
  1680. result:=GreaterThanValue;
  1681. if abs(a-b)<=delta then
  1682. result:=EqualsValue
  1683. else
  1684. if a<b then
  1685. result:=LessThanValue;
  1686. end;
  1687. {$endif}
  1688. end.