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