math.pp 53 KB

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