math.pp 53 KB

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