math.pp 52 KB

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