math.pp 53 KB

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