math.pp 49 KB

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