math.pp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435
  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. x:=abs(x);
  697. y:=abs(y);
  698. if (x>y) then
  699. hypot:=x*sqrt(1.0+sqr(y/x))
  700. else if (x>0.0) then
  701. hypot:=y*sqrt(1.0+sqr(x/y))
  702. else
  703. hypot:=y;
  704. end;
  705. function log10(x : float) : float;
  706. begin
  707. log10:=ln(x)/ln(10);
  708. end;
  709. function log2(x : float) : float;
  710. begin
  711. log2:=ln(x)/ln(2)
  712. end;
  713. function logn(n,x : float) : float;
  714. begin
  715. if n<0 then InvalidArgument;
  716. logn:=ln(x)/ln(n);
  717. end;
  718. function lnxp1(x : float) : float;
  719. begin
  720. if x<-1 then
  721. InvalidArgument;
  722. lnxp1:=ln(1+x);
  723. end;
  724. function power(base,exponent : float) : float;
  725. begin
  726. if Exponent=0.0 then
  727. result:=1.0
  728. else if (base=0.0) and (exponent>0.0) then
  729. result:=0.0
  730. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  731. result:=intpower(base,trunc(exponent))
  732. else if base>0.0 then
  733. result:=exp(exponent * ln (base))
  734. else
  735. InvalidArgument;
  736. end;
  737. function intpower(base : float;const exponent : Integer) : float;
  738. var
  739. i : longint;
  740. begin
  741. if (base = 0.0) and (exponent = 0) then
  742. result:=1
  743. else
  744. begin
  745. i:=abs(exponent);
  746. intpower:=1.0;
  747. while i>0 do
  748. begin
  749. while (i and 1)=0 do
  750. begin
  751. i:=i shr 1;
  752. base:=sqr(base);
  753. end;
  754. i:=i-1;
  755. intpower:=intpower*base;
  756. end;
  757. if exponent<0 then
  758. intpower:=1.0/intpower;
  759. end;
  760. end;
  761. operator ** (bas,expo : float) e: float; inline;
  762. begin
  763. e:=power(bas,expo);
  764. end;
  765. operator ** (bas,expo : int64) i: int64; inline;
  766. begin
  767. i:=round(intpower(bas,expo));
  768. end;
  769. function ceil(x : float) : integer;
  770. begin
  771. Ceil:=Trunc(x);
  772. If Frac(x)>0 then
  773. Ceil:=Ceil+1;
  774. end;
  775. function floor(x : float) : integer;
  776. begin
  777. Floor:=Trunc(x);
  778. If Frac(x)<0 then
  779. Floor := Floor-1;
  780. end;
  781. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  782. begin
  783. Exponent:=0;
  784. if (X<>0) then
  785. if (abs(X)<0.5) then
  786. repeat
  787. X:=X*2;
  788. Dec(Exponent);
  789. until (abs(X)>=0.5)
  790. else
  791. while (abs(X)>=1) do
  792. begin
  793. X:=X/2;
  794. Inc(Exponent);
  795. end;
  796. Mantissa:=X;
  797. end;
  798. function ldexp(x : float;const p : Integer) : float;
  799. begin
  800. ldexp:=x*intpower(2.0,p);
  801. end;
  802. {$ifdef FPC_HAS_TYPE_SINGLE}
  803. function mean(const data : array of Single) : float;
  804. begin
  805. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  806. end;
  807. function mean(const data : PSingle; Const N : longint) : float;
  808. begin
  809. mean:=sum(Data,N);
  810. mean:=mean/N;
  811. end;
  812. function sum(const data : array of Single) : float;inline;
  813. begin
  814. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  815. end;
  816. function sum(const data : PSingle;Const N : longint) : float;
  817. var
  818. i : longint;
  819. begin
  820. sum:=0.0;
  821. for i:=0 to N-1 do
  822. sum:=sum+data[i];
  823. end;
  824. {$endif FPC_HAS_TYPE_SINGLE}
  825. {$ifdef FPC_HAS_TYPE_DOUBLE}
  826. function mean(const data : array of Double) : float; inline;
  827. begin
  828. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  829. end;
  830. function mean(const data : PDouble; Const N : longint) : float;
  831. begin
  832. mean:=sum(Data,N);
  833. mean:=mean/N;
  834. end;
  835. function sum(const data : array of Double) : float; inline;
  836. begin
  837. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  838. end;
  839. function sum(const data : PDouble;Const N : longint) : float;
  840. var
  841. i : longint;
  842. begin
  843. sum:=0.0;
  844. for i:=0 to N-1 do
  845. sum:=sum+data[i];
  846. end;
  847. {$endif FPC_HAS_TYPE_DOUBLE}
  848. {$ifdef FPC_HAS_TYPE_EXTENDED}
  849. function mean(const data : array of Extended) : float;
  850. begin
  851. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  852. end;
  853. function mean(const data : PExtended; Const N : longint) : float;
  854. begin
  855. mean:=sum(Data,N);
  856. mean:=mean/N;
  857. end;
  858. function sum(const data : array of Extended) : float; inline;
  859. begin
  860. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  861. end;
  862. function sum(const data : PExtended;Const N : longint) : float;
  863. var
  864. i : longint;
  865. begin
  866. sum:=0.0;
  867. for i:=0 to N-1 do
  868. sum:=sum+data[i];
  869. end;
  870. {$endif FPC_HAS_TYPE_EXTENDED}
  871. function sumInt(const data : PInt64;Const N : longint) : Int64;
  872. var
  873. i : longint;
  874. begin
  875. sumInt:=0;
  876. for i:=0 to N-1 do
  877. sumInt:=sumInt+data[i];
  878. end;
  879. function sumInt(const data : array of Int64) : Int64; inline;
  880. begin
  881. Result:=SumInt(@Data[0],High(Data)+1);
  882. end;
  883. {$ifdef FPC_HAS_TYPE_SINGLE}
  884. function sumofsquares(const data : array of Single) : float; inline;
  885. begin
  886. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  887. end;
  888. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  889. var
  890. i : longint;
  891. begin
  892. sumofsquares:=0.0;
  893. for i:=0 to N-1 do
  894. sumofsquares:=sumofsquares+sqr(data[i]);
  895. end;
  896. procedure sumsandsquares(const data : array of Single;
  897. var sum,sumofsquares : float); inline;
  898. begin
  899. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  900. end;
  901. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  902. var sum,sumofsquares : float);
  903. var
  904. i : Integer;
  905. temp : float;
  906. begin
  907. sumofsquares:=0.0;
  908. sum:=0.0;
  909. for i:=0 to N-1 do
  910. begin
  911. temp:=data[i];
  912. sumofsquares:=sumofsquares+sqr(temp);
  913. sum:=sum+temp;
  914. end;
  915. end;
  916. {$endif FPC_HAS_TYPE_SINGLE}
  917. {$ifdef FPC_HAS_TYPE_DOUBLE}
  918. function sumofsquares(const data : array of Double) : float; inline;
  919. begin
  920. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  921. end;
  922. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  923. var
  924. i : longint;
  925. begin
  926. sumofsquares:=0.0;
  927. for i:=0 to N-1 do
  928. sumofsquares:=sumofsquares+sqr(data[i]);
  929. end;
  930. procedure sumsandsquares(const data : array of Double;
  931. var sum,sumofsquares : float);
  932. begin
  933. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  934. end;
  935. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  936. var sum,sumofsquares : float);
  937. var
  938. i : Integer;
  939. temp : float;
  940. begin
  941. sumofsquares:=0.0;
  942. sum:=0.0;
  943. for i:=0 to N-1 do
  944. begin
  945. temp:=data[i];
  946. sumofsquares:=sumofsquares+sqr(temp);
  947. sum:=sum+temp;
  948. end;
  949. end;
  950. {$endif FPC_HAS_TYPE_DOUBLE}
  951. {$ifdef FPC_HAS_TYPE_EXTENDED}
  952. function sumofsquares(const data : array of Extended) : float; inline;
  953. begin
  954. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  955. end;
  956. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  957. var
  958. i : longint;
  959. begin
  960. sumofsquares:=0.0;
  961. for i:=0 to N-1 do
  962. sumofsquares:=sumofsquares+sqr(data[i]);
  963. end;
  964. procedure sumsandsquares(const data : array of Extended;
  965. var sum,sumofsquares : float); inline;
  966. begin
  967. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  968. end;
  969. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  970. var sum,sumofsquares : float);
  971. var
  972. i : Integer;
  973. temp : float;
  974. begin
  975. sumofsquares:=0.0;
  976. sum:=0.0;
  977. for i:=0 to N-1 do
  978. begin
  979. temp:=data[i];
  980. sumofsquares:=sumofsquares+sqr(temp);
  981. sum:=sum+temp;
  982. end;
  983. end;
  984. {$endif FPC_HAS_TYPE_EXTENDED}
  985. function randg(mean,stddev : float) : float;
  986. Var U1,S2 : Float;
  987. begin
  988. repeat
  989. u1:= 2*random-1;
  990. S2:=Sqr(U1)+sqr(2*random-1);
  991. until s2<1;
  992. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  993. end;
  994. function RandomRange(const aFrom, aTo: Integer): Integer;
  995. begin
  996. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  997. end;
  998. function RandomRange(const aFrom, aTo: Int64): Int64;
  999. begin
  1000. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1001. end;
  1002. {$ifdef FPC_HAS_TYPE_SINGLE}
  1003. function stddev(const data : array of Single) : float; inline;
  1004. begin
  1005. Result:=Stddev(PSingle(@Data[0]),High(Data)+1);
  1006. end;
  1007. function stddev(const data : PSingle; Const N : Integer) : float;
  1008. begin
  1009. StdDev:=Sqrt(Variance(Data,N));
  1010. end;
  1011. procedure meanandstddev(const data : array of Single;
  1012. var mean,stddev : float); inline;
  1013. begin
  1014. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  1015. end;
  1016. procedure meanandstddev(const data : PSingle;
  1017. Const N : Longint;var mean,stddev : float);
  1018. Var I : longint;
  1019. begin
  1020. Mean:=0;
  1021. StdDev:=0;
  1022. For I:=0 to N-1 do
  1023. begin
  1024. Mean:=Mean+Data[i];
  1025. StdDev:=StdDev+Sqr(Data[i]);
  1026. end;
  1027. Mean:=Mean/N;
  1028. StdDev:=(StdDev-N*Sqr(Mean));
  1029. If N>1 then
  1030. StdDev:=Sqrt(Stddev/(N-1))
  1031. else
  1032. StdDev:=0;
  1033. end;
  1034. function variance(const data : array of Single) : float; inline;
  1035. begin
  1036. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  1037. end;
  1038. function variance(const data : PSingle; Const N : Integer) : float;
  1039. begin
  1040. If N=1 then
  1041. Result:=0
  1042. else
  1043. Result:=TotalVariance(Data,N)/(N-1);
  1044. end;
  1045. function totalvariance(const data : array of Single) : float; inline;
  1046. begin
  1047. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  1048. end;
  1049. function totalvariance(const data : PSingle;Const N : Integer) : float;
  1050. var S,SS : Float;
  1051. begin
  1052. If N=1 then
  1053. Result:=0
  1054. else
  1055. begin
  1056. SumsAndSquares(Data,N,S,SS);
  1057. Result := SS-Sqr(S)/N;
  1058. end;
  1059. end;
  1060. function popnstddev(const data : array of Single) : float;
  1061. begin
  1062. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  1063. end;
  1064. function popnstddev(const data : PSingle; Const N : Integer) : float;
  1065. begin
  1066. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1067. end;
  1068. function popnvariance(const data : array of Single) : float; inline;
  1069. begin
  1070. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  1071. end;
  1072. function popnvariance(const data : PSingle; Const N : Integer) : float;
  1073. begin
  1074. PopnVariance:=TotalVariance(Data,N)/N;
  1075. end;
  1076. procedure momentskewkurtosis(const data : array of single;
  1077. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1078. begin
  1079. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1080. end;
  1081. procedure momentskewkurtosis(
  1082. const data: pSingle;
  1083. Const N: integer;
  1084. out m1: float;
  1085. out m2: float;
  1086. out m3: float;
  1087. out m4: float;
  1088. out skew: float;
  1089. out kurtosis: float
  1090. );
  1091. var
  1092. i: integer;
  1093. value : psingle;
  1094. deviation, deviation2: single;
  1095. reciprocalN: float;
  1096. begin
  1097. m1 := 0;
  1098. reciprocalN := 1/N;
  1099. value := data;
  1100. for i := 0 to N-1 do
  1101. begin
  1102. m1 := m1 + value^;
  1103. inc(value);
  1104. end;
  1105. m1 := reciprocalN * m1;
  1106. m2 := 0;
  1107. m3 := 0;
  1108. m4 := 0;
  1109. value := data;
  1110. for i := 0 to N-1 do
  1111. begin
  1112. deviation := (value^-m1);
  1113. deviation2 := deviation * deviation;
  1114. m2 := m2 + deviation2;
  1115. m3 := m3 + deviation2 * deviation;
  1116. m4 := m4 + deviation2 * deviation2;
  1117. inc(value);
  1118. end;
  1119. m2 := reciprocalN * m2;
  1120. m3 := reciprocalN * m3;
  1121. m4 := reciprocalN * m4;
  1122. skew := m3 / (sqrt(m2)*m2);
  1123. kurtosis := m4 / (m2 * m2);
  1124. end;
  1125. function norm(const data : array of Single) : float; inline;
  1126. begin
  1127. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  1128. end;
  1129. function norm(const data : PSingle; Const N : Integer) : float;
  1130. begin
  1131. norm:=sqrt(sumofsquares(data,N));
  1132. end;
  1133. {$endif FPC_HAS_TYPE_SINGLE}
  1134. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1135. function stddev(const data : array of Double) : float; inline;
  1136. begin
  1137. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  1138. end;
  1139. function stddev(const data : PDouble; Const N : Integer) : float;
  1140. begin
  1141. StdDev:=Sqrt(Variance(Data,N));
  1142. end;
  1143. procedure meanandstddev(const data : array of Double;
  1144. var mean,stddev : float);
  1145. begin
  1146. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  1147. end;
  1148. procedure meanandstddev(const data : PDouble;
  1149. Const N : Longint;var mean,stddev : float);
  1150. Var I : longint;
  1151. begin
  1152. Mean:=0;
  1153. StdDev:=0;
  1154. For I:=0 to N-1 do
  1155. begin
  1156. Mean:=Mean+Data[i];
  1157. StdDev:=StdDev+Sqr(Data[i]);
  1158. end;
  1159. Mean:=Mean/N;
  1160. StdDev:=(StdDev-N*Sqr(Mean));
  1161. If N>1 then
  1162. StdDev:=Sqrt(Stddev/(N-1))
  1163. else
  1164. StdDev:=0;
  1165. end;
  1166. function variance(const data : array of Double) : float; inline;
  1167. begin
  1168. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  1169. end;
  1170. function variance(const data : PDouble; Const N : Integer) : float;
  1171. begin
  1172. If N=1 then
  1173. Result:=0
  1174. else
  1175. Result:=TotalVariance(Data,N)/(N-1);
  1176. end;
  1177. function totalvariance(const data : array of Double) : float; inline;
  1178. begin
  1179. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  1180. end;
  1181. function totalvariance(const data : PDouble;Const N : Integer) : float;
  1182. var S,SS : Float;
  1183. begin
  1184. If N=1 then
  1185. Result:=0
  1186. else
  1187. begin
  1188. SumsAndSquares(Data,N,S,SS);
  1189. Result := SS-Sqr(S)/N;
  1190. end;
  1191. end;
  1192. function popnstddev(const data : array of Double) : float;
  1193. begin
  1194. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  1195. end;
  1196. function popnstddev(const data : PDouble; Const N : Integer) : float;
  1197. begin
  1198. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1199. end;
  1200. function popnvariance(const data : array of Double) : float; inline;
  1201. begin
  1202. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  1203. end;
  1204. function popnvariance(const data : PDouble; Const N : Integer) : float;
  1205. begin
  1206. PopnVariance:=TotalVariance(Data,N)/N;
  1207. end;
  1208. procedure momentskewkurtosis(const data : array of Double;
  1209. out m1,m2,m3,m4,skew,kurtosis : float);
  1210. begin
  1211. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1212. end;
  1213. procedure momentskewkurtosis(
  1214. const data: pdouble;
  1215. Const N: integer;
  1216. out m1: float;
  1217. out m2: float;
  1218. out m3: float;
  1219. out m4: float;
  1220. out skew: float;
  1221. out kurtosis: float
  1222. );
  1223. var
  1224. i: integer;
  1225. value : pdouble;
  1226. deviation, deviation2: double;
  1227. reciprocalN: float;
  1228. begin
  1229. m1 := 0;
  1230. reciprocalN := 1/N;
  1231. value := data;
  1232. for i := 0 to N-1 do
  1233. begin
  1234. m1 := m1 + value^;
  1235. inc(value);
  1236. end;
  1237. m1 := reciprocalN * m1;
  1238. m2 := 0;
  1239. m3 := 0;
  1240. m4 := 0;
  1241. value := data;
  1242. for i := 0 to N-1 do
  1243. begin
  1244. deviation := (value^-m1);
  1245. deviation2 := deviation * deviation;
  1246. m2 := m2 + deviation2;
  1247. m3 := m3 + deviation2 * deviation;
  1248. m4 := m4 + deviation2 * deviation2;
  1249. inc(value);
  1250. end;
  1251. m2 := reciprocalN * m2;
  1252. m3 := reciprocalN * m3;
  1253. m4 := reciprocalN * m4;
  1254. skew := m3 / (sqrt(m2)*m2);
  1255. kurtosis := m4 / (m2 * m2);
  1256. end;
  1257. function norm(const data : array of Double) : float; inline;
  1258. begin
  1259. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  1260. end;
  1261. function norm(const data : PDouble; Const N : Integer) : float;
  1262. begin
  1263. norm:=sqrt(sumofsquares(data,N));
  1264. end;
  1265. {$endif FPC_HAS_TYPE_DOUBLE}
  1266. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1267. function stddev(const data : array of Extended) : float; inline;
  1268. begin
  1269. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  1270. end;
  1271. function stddev(const data : PExtended; Const N : Integer) : float;
  1272. begin
  1273. StdDev:=Sqrt(Variance(Data,N));
  1274. end;
  1275. procedure meanandstddev(const data : array of Extended;
  1276. var mean,stddev : float); inline;
  1277. begin
  1278. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  1279. end;
  1280. procedure meanandstddev(const data : PExtended;
  1281. Const N : Longint;var mean,stddev : float);
  1282. Var I : longint;
  1283. begin
  1284. Mean:=0;
  1285. StdDev:=0;
  1286. For I:=0 to N-1 do
  1287. begin
  1288. Mean:=Mean+Data[i];
  1289. StdDev:=StdDev+Sqr(Data[i]);
  1290. end;
  1291. Mean:=Mean/N;
  1292. StdDev:=(StdDev-N*Sqr(Mean));
  1293. If N>1 then
  1294. StdDev:=Sqrt(Stddev/(N-1))
  1295. else
  1296. StdDev:=0;
  1297. end;
  1298. function variance(const data : array of Extended) : float; inline;
  1299. begin
  1300. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  1301. end;
  1302. function variance(const data : PExtended; Const N : Integer) : float;
  1303. begin
  1304. If N=1 then
  1305. Result:=0
  1306. else
  1307. Result:=TotalVariance(Data,N)/(N-1);
  1308. end;
  1309. function totalvariance(const data : array of Extended) : float; inline;
  1310. begin
  1311. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  1312. end;
  1313. function totalvariance(const data : PExtended;Const N : Integer) : float;
  1314. var S,SS : Float;
  1315. begin
  1316. If N=1 then
  1317. Result:=0
  1318. else
  1319. begin
  1320. SumsAndSquares(Data,N,S,SS);
  1321. Result := SS-Sqr(S)/N;
  1322. end;
  1323. end;
  1324. function popnstddev(const data : array of Extended) : float;
  1325. begin
  1326. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  1327. end;
  1328. function popnstddev(const data : PExtended; Const N : Integer) : float;
  1329. begin
  1330. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1331. end;
  1332. function popnvariance(const data : array of Extended) : float; inline;
  1333. begin
  1334. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  1335. end;
  1336. function popnvariance(const data : PExtended; Const N : Integer) : float;
  1337. begin
  1338. PopnVariance:=TotalVariance(Data,N)/N;
  1339. end;
  1340. procedure momentskewkurtosis(const data : array of Extended;
  1341. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1342. begin
  1343. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1344. end;
  1345. procedure momentskewkurtosis(
  1346. const data: pExtended;
  1347. Const N: integer;
  1348. out m1: float;
  1349. out m2: float;
  1350. out m3: float;
  1351. out m4: float;
  1352. out skew: float;
  1353. out kurtosis: float
  1354. );
  1355. var
  1356. i: integer;
  1357. value : pextended;
  1358. deviation, deviation2: extended;
  1359. reciprocalN: float;
  1360. begin
  1361. m1 := 0;
  1362. reciprocalN := 1/N;
  1363. value := data;
  1364. for i := 0 to N-1 do
  1365. begin
  1366. m1 := m1 + value^;
  1367. inc(value);
  1368. end;
  1369. m1 := reciprocalN * m1;
  1370. m2 := 0;
  1371. m3 := 0;
  1372. m4 := 0;
  1373. value := data;
  1374. for i := 0 to N-1 do
  1375. begin
  1376. deviation := (value^-m1);
  1377. deviation2 := deviation * deviation;
  1378. m2 := m2 + deviation2;
  1379. m3 := m3 + deviation2 * deviation;
  1380. m4 := m4 + deviation2 * deviation2;
  1381. inc(value);
  1382. end;
  1383. m2 := reciprocalN * m2;
  1384. m3 := reciprocalN * m3;
  1385. m4 := reciprocalN * m4;
  1386. skew := m3 / (sqrt(m2)*m2);
  1387. kurtosis := m4 / (m2 * m2);
  1388. end;
  1389. function norm(const data : array of Extended) : float; inline;
  1390. begin
  1391. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  1392. end;
  1393. function norm(const data : PExtended; Const N : Integer) : float;
  1394. begin
  1395. norm:=sqrt(sumofsquares(data,N));
  1396. end;
  1397. {$endif FPC_HAS_TYPE_EXTENDED}
  1398. function MinIntValue(const Data: array of Integer): Integer;
  1399. var
  1400. I: Integer;
  1401. begin
  1402. Result := Data[Low(Data)];
  1403. For I := Succ(Low(Data)) To High(Data) Do
  1404. If Data[I] < Result Then Result := Data[I];
  1405. end;
  1406. function MaxIntValue(const Data: array of Integer): Integer;
  1407. var
  1408. I: Integer;
  1409. begin
  1410. Result := Data[Low(Data)];
  1411. For I := Succ(Low(Data)) To High(Data) Do
  1412. If Data[I] > Result Then Result := Data[I];
  1413. end;
  1414. function MinValue(const Data: array of Integer): Integer; inline;
  1415. begin
  1416. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  1417. end;
  1418. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  1419. var
  1420. I: Integer;
  1421. begin
  1422. Result := Data[0];
  1423. For I := 1 To N-1 do
  1424. If Data[I] < Result Then Result := Data[I];
  1425. end;
  1426. function MaxValue(const Data: array of Integer): Integer; inline;
  1427. begin
  1428. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  1429. end;
  1430. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  1431. var
  1432. i : longint;
  1433. begin
  1434. { get an initial value }
  1435. maxvalue:=data[0];
  1436. for i:=1 to N-1 do
  1437. if data[i]>maxvalue then
  1438. maxvalue:=data[i];
  1439. end;
  1440. {$ifdef FPC_HAS_TYPE_SINGLE}
  1441. function minvalue(const data : array of Single) : Single; inline;
  1442. begin
  1443. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  1444. end;
  1445. function minvalue(const data : PSingle; Const N : Integer) : Single;
  1446. var
  1447. i : longint;
  1448. begin
  1449. { get an initial value }
  1450. minvalue:=data[0];
  1451. for i:=1 to N-1 do
  1452. if data[i]<minvalue then
  1453. minvalue:=data[i];
  1454. end;
  1455. function maxvalue(const data : array of Single) : Single; inline;
  1456. begin
  1457. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  1458. end;
  1459. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  1460. var
  1461. i : longint;
  1462. begin
  1463. { get an initial value }
  1464. maxvalue:=data[0];
  1465. for i:=1 to N-1 do
  1466. if data[i]>maxvalue then
  1467. maxvalue:=data[i];
  1468. end;
  1469. {$endif FPC_HAS_TYPE_SINGLE}
  1470. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1471. function minvalue(const data : array of Double) : Double; inline;
  1472. begin
  1473. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  1474. end;
  1475. function minvalue(const data : PDouble; Const N : Integer) : Double;
  1476. var
  1477. i : longint;
  1478. begin
  1479. { get an initial value }
  1480. minvalue:=data[0];
  1481. for i:=1 to N-1 do
  1482. if data[i]<minvalue then
  1483. minvalue:=data[i];
  1484. end;
  1485. function maxvalue(const data : array of Double) : Double; inline;
  1486. begin
  1487. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  1488. end;
  1489. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  1490. var
  1491. i : longint;
  1492. begin
  1493. { get an initial value }
  1494. maxvalue:=data[0];
  1495. for i:=1 to N-1 do
  1496. if data[i]>maxvalue then
  1497. maxvalue:=data[i];
  1498. end;
  1499. {$endif FPC_HAS_TYPE_DOUBLE}
  1500. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1501. function minvalue(const data : array of Extended) : Extended; inline;
  1502. begin
  1503. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  1504. end;
  1505. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  1506. var
  1507. i : longint;
  1508. begin
  1509. { get an initial value }
  1510. minvalue:=data[0];
  1511. for i:=1 to N-1 do
  1512. if data[i]<minvalue then
  1513. minvalue:=data[i];
  1514. end;
  1515. function maxvalue(const data : array of Extended) : Extended; inline;
  1516. begin
  1517. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  1518. end;
  1519. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  1520. var
  1521. i : longint;
  1522. begin
  1523. { get an initial value }
  1524. maxvalue:=data[0];
  1525. for i:=1 to N-1 do
  1526. if data[i]>maxvalue then
  1527. maxvalue:=data[i];
  1528. end;
  1529. {$endif FPC_HAS_TYPE_EXTENDED}
  1530. function Min(a, b: Integer): Integer;inline;
  1531. begin
  1532. if a < b then
  1533. Result := a
  1534. else
  1535. Result := b;
  1536. end;
  1537. function Max(a, b: Integer): Integer;inline;
  1538. begin
  1539. if a > b then
  1540. Result := a
  1541. else
  1542. Result := b;
  1543. end;
  1544. {
  1545. function Min(a, b: Cardinal): Cardinal;inline;
  1546. begin
  1547. if a < b then
  1548. Result := a
  1549. else
  1550. Result := b;
  1551. end;
  1552. function Max(a, b: Cardinal): Cardinal;inline;
  1553. begin
  1554. if a > b then
  1555. Result := a
  1556. else
  1557. Result := b;
  1558. end;
  1559. }
  1560. function Min(a, b: Int64): Int64;inline;
  1561. begin
  1562. if a < b then
  1563. Result := a
  1564. else
  1565. Result := b;
  1566. end;
  1567. function Max(a, b: Int64): Int64;inline;
  1568. begin
  1569. if a > b then
  1570. Result := a
  1571. else
  1572. Result := b;
  1573. end;
  1574. {$ifdef FPC_HAS_TYPE_SINGLE}
  1575. function Min(a, b: Single): Single;inline;
  1576. begin
  1577. if a < b then
  1578. Result := a
  1579. else
  1580. Result := b;
  1581. end;
  1582. function Max(a, b: Single): Single;inline;
  1583. begin
  1584. if a > b then
  1585. Result := a
  1586. else
  1587. Result := b;
  1588. end;
  1589. {$endif FPC_HAS_TYPE_SINGLE}
  1590. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1591. function Min(a, b: Double): Double;inline;
  1592. begin
  1593. if a < b then
  1594. Result := a
  1595. else
  1596. Result := b;
  1597. end;
  1598. function Max(a, b: Double): Double;inline;
  1599. begin
  1600. if a > b then
  1601. Result := a
  1602. else
  1603. Result := b;
  1604. end;
  1605. {$endif FPC_HAS_TYPE_DOUBLE}
  1606. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1607. function Min(a, b: Extended): Extended;inline;
  1608. begin
  1609. if a < b then
  1610. Result := a
  1611. else
  1612. Result := b;
  1613. end;
  1614. function Max(a, b: Extended): Extended;inline;
  1615. begin
  1616. if a > b then
  1617. Result := a
  1618. else
  1619. Result := b;
  1620. end;
  1621. {$endif FPC_HAS_TYPE_EXTENDED}
  1622. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  1623. begin
  1624. Result:=(AValue>=AMin) and (AValue<=AMax);
  1625. end;
  1626. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  1627. begin
  1628. Result:=(AValue>=AMin) and (AValue<=AMax);
  1629. end;
  1630. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1631. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  1632. begin
  1633. Result:=(AValue>=AMin) and (AValue<=AMax);
  1634. end;
  1635. {$endif FPC_HAS_TYPE_DOUBLE}
  1636. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  1637. begin
  1638. Result:=AValue;
  1639. If Result<AMin then
  1640. Result:=AMin
  1641. else if Result>AMax then
  1642. Result:=AMax;
  1643. end;
  1644. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  1645. begin
  1646. Result:=AValue;
  1647. If Result<AMin then
  1648. Result:=AMin
  1649. else if Result>AMax then
  1650. Result:=AMax;
  1651. end;
  1652. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1653. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  1654. begin
  1655. Result:=AValue;
  1656. If Result<AMin then
  1657. Result:=AMin
  1658. else if Result>AMax then
  1659. Result:=AMax;
  1660. end;
  1661. {$endif FPC_HAS_TYPE_DOUBLE}
  1662. Const
  1663. EZeroResolution = 1E-16;
  1664. DZeroResolution = 1E-12;
  1665. SZeroResolution = 1E-4;
  1666. function IsZero(const A: Single; Epsilon: Single): Boolean;
  1667. begin
  1668. if (Epsilon=0) then
  1669. Epsilon:=SZeroResolution;
  1670. Result:=Abs(A)<=Epsilon;
  1671. end;
  1672. function IsZero(const A: Single): Boolean;inline;
  1673. begin
  1674. Result:=IsZero(A,single(SZeroResolution));
  1675. end;
  1676. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1677. function IsZero(const A: Double; Epsilon: Double): Boolean;
  1678. begin
  1679. if (Epsilon=0) then
  1680. Epsilon:=DZeroResolution;
  1681. Result:=Abs(A)<=Epsilon;
  1682. end;
  1683. function IsZero(const A: Double): Boolean;inline;
  1684. begin
  1685. Result:=IsZero(A,DZeroResolution);
  1686. end;
  1687. {$endif FPC_HAS_TYPE_DOUBLE}
  1688. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1689. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  1690. begin
  1691. if (Epsilon=0) then
  1692. Epsilon:=EZeroResolution;
  1693. Result:=Abs(A)<=Epsilon;
  1694. end;
  1695. function IsZero(const A: Extended): Boolean;inline;
  1696. begin
  1697. Result:=IsZero(A,EZeroResolution);
  1698. end;
  1699. {$endif FPC_HAS_TYPE_EXTENDED}
  1700. type
  1701. TSplitDouble = packed record
  1702. cards: Array[0..1] of cardinal;
  1703. end;
  1704. function IsNan(const d : Single): Boolean; overload;
  1705. begin
  1706. result:=(longword(d) and $7fffffff)>$7f800000;
  1707. end;
  1708. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1709. function IsNan(const d : Double): Boolean;
  1710. var
  1711. fraczero, expMaximal: boolean;
  1712. begin
  1713. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1714. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1715. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1716. (TSplitDouble(d).cards[1] = 0);
  1717. {$else FPC_BIG_ENDIAN}
  1718. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1719. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1720. (TSplitDouble(d).cards[0] = 0);
  1721. {$endif FPC_BIG_ENDIAN}
  1722. Result:=expMaximal and not(fraczero);
  1723. end;
  1724. {$endif FPC_HAS_TYPE_DOUBLE}
  1725. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1726. function IsNan(const d : Extended): Boolean; overload;
  1727. type
  1728. TSplitExtended = packed record
  1729. case byte of
  1730. 0: (bytes: Array[0..9] of byte);
  1731. 1: (words: Array[0..4] of word);
  1732. 2: (cards: Array[0..1] of cardinal; w: word);
  1733. end;
  1734. var
  1735. fraczero, expMaximal: boolean;
  1736. begin
  1737. {$ifdef FPC_BIG_ENDIAN}
  1738. {$error no support for big endian extended type yet}
  1739. {$else FPC_BIG_ENDIAN}
  1740. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  1741. fraczero := (TSplitExtended(d).cards[0] = 0) and
  1742. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  1743. {$endif FPC_BIG_ENDIAN}
  1744. Result:=expMaximal and not(fraczero);
  1745. end;
  1746. {$endif FPC_HAS_TYPE_EXTENDED}
  1747. function IsInfinite(const d : Double): Boolean;
  1748. var
  1749. fraczero, expMaximal: boolean;
  1750. begin
  1751. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1752. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1753. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1754. (TSplitDouble(d).cards[1] = 0);
  1755. {$else FPC_BIG_ENDIAN}
  1756. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1757. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1758. (TSplitDouble(d).cards[0] = 0);
  1759. {$endif FPC_BIG_ENDIAN}
  1760. Result:=expMaximal and fraczero;
  1761. end;
  1762. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1763. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1764. begin
  1765. if (Epsilon=0) then
  1766. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1767. if (A>B) then
  1768. Result:=((A-B)<=Epsilon)
  1769. else
  1770. Result:=((B-A)<=Epsilon);
  1771. end;
  1772. function SameValue(const A, B: Extended): Boolean;inline;
  1773. begin
  1774. Result:=SameValue(A,B,0.0);
  1775. end;
  1776. {$endif FPC_HAS_TYPE_EXTENDED}
  1777. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1778. function SameValue(const A, B: Double): Boolean;inline;
  1779. begin
  1780. Result:=SameValue(A,B,0.0);
  1781. end;
  1782. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1783. begin
  1784. if (Epsilon=0) then
  1785. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1786. if (A>B) then
  1787. Result:=((A-B)<=Epsilon)
  1788. else
  1789. Result:=((B-A)<=Epsilon);
  1790. end;
  1791. {$endif FPC_HAS_TYPE_DOUBLE}
  1792. function SameValue(const A, B: Single): Boolean;inline;
  1793. begin
  1794. Result:=SameValue(A,B,0);
  1795. end;
  1796. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  1797. begin
  1798. if (Epsilon=0) then
  1799. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  1800. if (A>B) then
  1801. Result:=((A-B)<=Epsilon)
  1802. else
  1803. Result:=((B-A)<=Epsilon);
  1804. end;
  1805. // Some CPUs probably allow a faster way of doing this in a single operation...
  1806. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  1807. {$ifndef FPC_MATH_HAS_DIVMOD}
  1808. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  1809. begin
  1810. if Dividend < 0 then
  1811. begin
  1812. { Use DivMod with >=0 dividend }
  1813. Dividend:=-Dividend;
  1814. { The documented behavior of Pascal's div/mod operators and DivMod
  1815. on negative dividends is to return Result closer to zero and
  1816. a negative Remainder. Which means that we can just negate both
  1817. Result and Remainder, and all it's Ok. }
  1818. Result:=-(Dividend Div Divisor);
  1819. Remainder:=-(Dividend+(Result*Divisor));
  1820. end
  1821. else
  1822. begin
  1823. Result:=Dividend Div Divisor;
  1824. Remainder:=Dividend-(Result*Divisor);
  1825. end;
  1826. end;
  1827. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: SmallInt);
  1828. begin
  1829. if Dividend < 0 then
  1830. begin
  1831. { Use DivMod with >=0 dividend }
  1832. Dividend:=-Dividend;
  1833. { The documented behavior of Pascal's div/mod operators and DivMod
  1834. on negative dividends is to return Result closer to zero and
  1835. a negative Remainder. Which means that we can just negate both
  1836. Result and Remainder, and all it's Ok. }
  1837. Result:=-(Dividend Div Divisor);
  1838. Remainder:=-(Dividend+(Result*Divisor));
  1839. end
  1840. else
  1841. begin
  1842. Result:=Dividend Div Divisor;
  1843. Remainder:=Dividend-(Result*Divisor);
  1844. end;
  1845. end;
  1846. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  1847. begin
  1848. Result:=Dividend Div Divisor;
  1849. Remainder:=Dividend-(Result*Divisor);
  1850. end;
  1851. procedure DivMod(Dividend: Integer; Divisor: Integer; var Result, Remainder: Integer);
  1852. begin
  1853. if Dividend < 0 then
  1854. begin
  1855. { Use DivMod with >=0 dividend }
  1856. Dividend:=-Dividend;
  1857. { The documented behavior of Pascal's div/mod operators and DivMod
  1858. on negative dividends is to return Result closer to zero and
  1859. a negative Remainder. Which means that we can just negate both
  1860. Result and Remainder, and all it's Ok. }
  1861. Result:=-(Dividend Div Divisor);
  1862. Remainder:=-(Dividend+(Result*Divisor));
  1863. end
  1864. else
  1865. begin
  1866. Result:=Dividend Div Divisor;
  1867. Remainder:=Dividend-(Result*Divisor);
  1868. end;
  1869. end;
  1870. {$endif FPC_MATH_HAS_DIVMOD}
  1871. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  1872. begin
  1873. if val then result:=iftrue else result:=iffalse;
  1874. end;
  1875. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  1876. begin
  1877. if val then result:=iftrue else result:=iffalse;
  1878. end;
  1879. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  1880. begin
  1881. if val then result:=iftrue else result:=iffalse;
  1882. end;
  1883. // dilemma here. asm can do the two comparisons in one go?
  1884. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  1885. function CompareValue(const A, B : Integer): TValueRelationship;
  1886. begin
  1887. result:=GreaterThanValue;
  1888. if a=b then
  1889. result:=EqualsValue
  1890. else
  1891. if a<b then
  1892. result:=LessThanValue;
  1893. end;
  1894. function CompareValue(const A, B: Int64): 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: QWord): 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. {$ifdef FPC_HAS_TYPE_SINGLE}
  1913. function CompareValue(const A, B: Single; delta: Single = 0.0): TValueRelationship;
  1914. begin
  1915. result:=GreaterThanValue;
  1916. if abs(a-b)<=delta then
  1917. result:=EqualsValue
  1918. else
  1919. if a<b then
  1920. result:=LessThanValue;
  1921. end;
  1922. {$endif}
  1923. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1924. function CompareValue(const A, B: Double; delta: Double = 0.0): TValueRelationship;
  1925. begin
  1926. result:=GreaterThanValue;
  1927. if abs(a-b)<=delta then
  1928. result:=EqualsValue
  1929. else
  1930. if a<b then
  1931. result:=LessThanValue;
  1932. end;
  1933. {$endif}
  1934. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1935. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  1936. begin
  1937. result:=GreaterThanValue;
  1938. if abs(a-b)<=delta then
  1939. result:=EqualsValue
  1940. else
  1941. if a<b then
  1942. result:=LessThanValue;
  1943. end;
  1944. {$endif}
  1945. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1946. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  1947. var
  1948. RV : Double;
  1949. begin
  1950. RV:=IntPower(10,Digits);
  1951. Result:=Round(AValue/RV)*RV;
  1952. end;
  1953. {$endif}
  1954. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1955. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  1956. var
  1957. RV : Extended;
  1958. begin
  1959. RV:=IntPower(10,Digits);
  1960. Result:=Round(AValue/RV)*RV;
  1961. end;
  1962. {$endif}
  1963. {$ifdef FPC_HAS_TYPE_SINGLE}
  1964. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  1965. var
  1966. RV : Single;
  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 SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  1974. var
  1975. RV : Single;
  1976. begin
  1977. RV := IntPower(10, -Digits);
  1978. if AValue < 0 then
  1979. Result := Trunc((AValue*RV) - 0.5)/RV
  1980. else
  1981. Result := Trunc((AValue*RV) + 0.5)/RV;
  1982. end;
  1983. {$endif}
  1984. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1985. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  1986. var
  1987. RV : Double;
  1988. begin
  1989. RV := IntPower(10, -Digits);
  1990. if AValue < 0 then
  1991. Result := Trunc((AValue*RV) - 0.5)/RV
  1992. else
  1993. Result := Trunc((AValue*RV) + 0.5)/RV;
  1994. end;
  1995. {$endif}
  1996. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1997. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  1998. var
  1999. RV : Extended;
  2000. begin
  2001. RV := IntPower(10, -Digits);
  2002. if AValue < 0 then
  2003. Result := Trunc((AValue*RV) - 0.5)/RV
  2004. else
  2005. Result := Trunc((AValue*RV) + 0.5)/RV;
  2006. end;
  2007. {$endif}
  2008. function RandomFrom(const AValues: array of Double): Double; overload;
  2009. begin
  2010. result:=AValues[random(High(AValues)+1)];
  2011. end;
  2012. function RandomFrom(const AValues: array of Integer): Integer; overload;
  2013. begin
  2014. result:=AValues[random(High(AValues)+1)];
  2015. end;
  2016. function RandomFrom(const AValues: array of Int64): Int64; overload;
  2017. begin
  2018. result:=AValues[random(High(AValues)+1)];
  2019. end;
  2020. {$else}
  2021. implementation
  2022. {$endif FPUNONE}
  2023. end.