math.pp 55 KB

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