2
0

math.pp 54 KB

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