math.pp 59 KB

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