math.pp 49 KB

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