math.pp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305
  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;out 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. {$ifndef FPC_MATH_HAS_TAN}
  540. function tan(x : float) : float;
  541. var
  542. _sin,_cos : float;
  543. begin
  544. sincos(x,_Sin,_Cos);
  545. cotan:=_Sin/_Cos;
  546. end;
  547. {$endif FPC_MATH_HAS_TAN}
  548. {$ifndef FPC_MATH_HAS_COTAN}
  549. function cotan(x : float) : float;
  550. var
  551. _sin,_cos : float;
  552. begin
  553. sincos(x,_Sin,_Cos);
  554. cotan:=_Cos/_Sin;
  555. end;
  556. {$endif FPC_MATH_HAS_COTAN}
  557. {$ifndef FPC_MATH_HAS_SINCOS}
  558. procedure sincos(theta : float;out sinus,cosinus : float);
  559. begin
  560. sinus:=sin(theta);
  561. cosinus:=cos(theta);
  562. end;
  563. {$endif FPC_MATH_HAS_SINCOS}
  564. { ArcSin and ArcCos from Arjan van Dijk ([email protected]) }
  565. function arcsin(x : float) : float;
  566. begin
  567. if abs(x) > 1 then InvalidArgument
  568. else if abs(x) < 0.5 then
  569. arcsin := arctan(x/sqrt(1-sqr(x)))
  570. else
  571. arcsin := sign(x) * (pi*0.5 - arctan(sqrt(1 / sqr(x) - 1)));
  572. end;
  573. function Arccos(x : Float) : Float;
  574. begin
  575. arccos := pi*0.5 - arcsin(x);
  576. end;
  577. {$ifndef FPC_MATH_HAS_ARCTAN2}
  578. function arctan2(y,x : float) : float;
  579. begin
  580. if (x=0) then
  581. begin
  582. if y=0 then
  583. arctan2:=0.0
  584. else if y>0 then
  585. arctan2:=pi/2
  586. else if y<0 then
  587. arctan2:=-pi/2;
  588. end
  589. else
  590. ArcTan2:=ArcTan(y/x);
  591. if x<0.0 then
  592. ArcTan2:=ArcTan2+pi;
  593. if ArcTan2>pi then
  594. ArcTan2:=ArcTan2-2*pi;
  595. end;
  596. {$endif FPC_MATH_HAS_ARCTAN2}
  597. function cosh(x : float) : float;
  598. var
  599. temp : float;
  600. begin
  601. temp:=exp(x);
  602. cosh:=0.5*(temp+1.0/temp);
  603. end;
  604. function sinh(x : float) : float;
  605. var
  606. temp : float;
  607. begin
  608. temp:=exp(x);
  609. sinh:=0.5*(temp-1.0/temp);
  610. end;
  611. Const MaxTanh = 5678.22249441322; // Ln(MaxExtended)/2
  612. function tanh(x : float) : float;
  613. var Temp : float;
  614. begin
  615. if x>MaxTanh then exit(1.0)
  616. else if x<-MaxTanh then exit (-1.0);
  617. temp:=exp(-2*x);
  618. tanh:=(1-temp)/(1+temp)
  619. end;
  620. function arccosh(x : float) : float;
  621. begin
  622. arccosh:=arcosh(x);
  623. end;
  624. function arcsinh(x : float) : float;
  625. begin
  626. arcsinh:=arsinh(x);
  627. end;
  628. function arctanh(x : float) : float;
  629. begin
  630. if x>1 then InvalidArgument;
  631. arctanh:=artanh(x);
  632. end;
  633. function arcosh(x : float) : float;
  634. begin
  635. if x<1 then InvalidArgument;
  636. arcosh:=Ln(x+Sqrt(x*x-1));
  637. end;
  638. function arsinh(x : float) : float;
  639. begin
  640. arsinh:=Ln(x+Sqrt(1+x*x));
  641. end;
  642. function artanh(x : float) : float;
  643. begin
  644. If abs(x)>1 then InvalidArgument;
  645. artanh:=(Ln((1+x)/(1-x)))*0.5;
  646. end;
  647. function hypot(x,y : float) : float;
  648. begin
  649. hypot:=Sqrt(x*x+y*y)
  650. end;
  651. function log10(x : float) : float;
  652. begin
  653. log10:=ln(x)/ln(10);
  654. end;
  655. function log2(x : float) : float;
  656. begin
  657. log2:=ln(x)/ln(2)
  658. end;
  659. function logn(n,x : float) : float;
  660. begin
  661. if n<0 then InvalidArgument;
  662. logn:=ln(x)/ln(n);
  663. end;
  664. function lnxp1(x : float) : float;
  665. begin
  666. if x<-1 then
  667. InvalidArgument;
  668. lnxp1:=ln(1+x);
  669. end;
  670. function power(base,exponent : float) : float;
  671. begin
  672. if Exponent=0.0 then
  673. if base <> 0.0 then
  674. result:=1.0
  675. else
  676. InvalidArgument
  677. else if (base=0.0) and (exponent>0.0) then
  678. result:=0.0
  679. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  680. result:=intpower(base,trunc(exponent))
  681. else if base>0.0 then
  682. result:=exp(exponent * ln (base))
  683. else
  684. InvalidArgument;
  685. end;
  686. function intpower(base : float;const exponent : Integer) : float;
  687. var
  688. i : longint;
  689. begin
  690. if (base = 0.0) and (exponent = 0) then
  691. InvalidArgument;
  692. i:=abs(exponent);
  693. intpower:=1.0;
  694. while i>0 do
  695. begin
  696. while (i and 1)=0 do
  697. begin
  698. i:=i shr 1;
  699. base:=sqr(base);
  700. end;
  701. i:=i-1;
  702. intpower:=intpower*base;
  703. end;
  704. if exponent<0 then
  705. intpower:=1.0/intpower;
  706. end;
  707. operator ** (bas,expo : float) e: float;
  708. begin
  709. e:=power(bas,expo);
  710. end;
  711. operator ** (bas,expo : int64) i: int64;
  712. begin
  713. i:=round(intpower(bas,expo));
  714. end;
  715. function ceil(x : float) : integer;
  716. begin
  717. Ceil:=Trunc(x);
  718. If Frac(x)>0 then
  719. Ceil:=Ceil+1;
  720. end;
  721. function floor(x : float) : integer;
  722. begin
  723. Floor:=Trunc(x);
  724. If Frac(x)<0 then
  725. Floor := Floor-1;
  726. end;
  727. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  728. begin
  729. Exponent:=0;
  730. if (X<>0) then
  731. if (abs(X)<0.5) then
  732. repeat
  733. X:=X*2;
  734. Dec(Exponent);
  735. until (abs(X)>=0.5)
  736. else
  737. while (abs(X)>=1) do
  738. begin
  739. X:=X/2;
  740. Inc(Exponent);
  741. end;
  742. Mantissa:=X;
  743. end;
  744. function ldexp(x : float;const p : Integer) : float;
  745. begin
  746. ldexp:=x*intpower(2.0,p);
  747. end;
  748. {$ifdef FPC_HAS_TYPE_SINGLE}
  749. function mean(const data : array of Single) : float;
  750. begin
  751. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  752. end;
  753. function mean(const data : PSingle; Const N : longint) : float;
  754. begin
  755. mean:=sum(Data,N);
  756. mean:=mean/N;
  757. end;
  758. function sum(const data : array of Single) : float;
  759. begin
  760. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  761. end;
  762. function sum(const data : PSingle;Const N : longint) : float;
  763. var
  764. i : longint;
  765. begin
  766. sum:=0.0;
  767. for i:=0 to N-1 do
  768. sum:=sum+data[i];
  769. end;
  770. {$endif FPC_HAS_TYPE_SINGLE}
  771. {$ifdef FPC_HAS_TYPE_DOUBLE}
  772. function mean(const data : array of Double) : float;
  773. begin
  774. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  775. end;
  776. function mean(const data : PDouble; Const N : longint) : float;
  777. begin
  778. mean:=sum(Data,N);
  779. mean:=mean/N;
  780. end;
  781. function sum(const data : array of Double) : float;
  782. begin
  783. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  784. end;
  785. function sum(const data : PDouble;Const N : longint) : float;
  786. var
  787. i : longint;
  788. begin
  789. sum:=0.0;
  790. for i:=0 to N-1 do
  791. sum:=sum+data[i];
  792. end;
  793. {$endif FPC_HAS_TYPE_DOUBLE}
  794. {$ifdef FPC_HAS_TYPE_EXTENDED}
  795. function mean(const data : array of Extended) : float;
  796. begin
  797. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  798. end;
  799. function mean(const data : PExtended; Const N : longint) : float;
  800. begin
  801. mean:=sum(Data,N);
  802. mean:=mean/N;
  803. end;
  804. function sum(const data : array of Extended) : float;
  805. begin
  806. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  807. end;
  808. function sum(const data : PExtended;Const N : longint) : float;
  809. var
  810. i : longint;
  811. begin
  812. sum:=0.0;
  813. for i:=0 to N-1 do
  814. sum:=sum+data[i];
  815. end;
  816. {$endif FPC_HAS_TYPE_EXTENDED}
  817. function sumInt(const data : PInt64;Const N : longint) : Int64;
  818. var
  819. i : longint;
  820. begin
  821. sumInt:=0;
  822. for i:=0 to N-1 do
  823. sumInt:=sumInt+data[i];
  824. end;
  825. function sumInt(const data : array of Int64) : Int64;
  826. begin
  827. Result:=SumInt(@Data[0],High(Data)+1);
  828. end;
  829. {$ifdef FPC_HAS_TYPE_SINGLE}
  830. function sumofsquares(const data : array of Single) : float;
  831. begin
  832. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  833. end;
  834. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  835. var
  836. i : longint;
  837. begin
  838. sumofsquares:=0.0;
  839. for i:=0 to N-1 do
  840. sumofsquares:=sumofsquares+sqr(data[i]);
  841. end;
  842. procedure sumsandsquares(const data : array of Single;
  843. var sum,sumofsquares : float);
  844. begin
  845. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  846. end;
  847. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  848. var sum,sumofsquares : float);
  849. var
  850. i : Integer;
  851. temp : float;
  852. begin
  853. sumofsquares:=0.0;
  854. sum:=0.0;
  855. for i:=0 to N-1 do
  856. begin
  857. temp:=data[i];
  858. sumofsquares:=sumofsquares+sqr(temp);
  859. sum:=sum+temp;
  860. end;
  861. end;
  862. {$endif FPC_HAS_TYPE_SINGLE}
  863. {$ifdef FPC_HAS_TYPE_DOUBLE}
  864. function sumofsquares(const data : array of Double) : float;
  865. begin
  866. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  867. end;
  868. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  869. var
  870. i : longint;
  871. begin
  872. sumofsquares:=0.0;
  873. for i:=0 to N-1 do
  874. sumofsquares:=sumofsquares+sqr(data[i]);
  875. end;
  876. procedure sumsandsquares(const data : array of Double;
  877. var sum,sumofsquares : float);
  878. begin
  879. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  880. end;
  881. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  882. var sum,sumofsquares : float);
  883. var
  884. i : Integer;
  885. temp : float;
  886. begin
  887. sumofsquares:=0.0;
  888. sum:=0.0;
  889. for i:=0 to N-1 do
  890. begin
  891. temp:=data[i];
  892. sumofsquares:=sumofsquares+sqr(temp);
  893. sum:=sum+temp;
  894. end;
  895. end;
  896. {$endif FPC_HAS_TYPE_DOUBLE}
  897. {$ifdef FPC_HAS_TYPE_EXTENDED}
  898. function sumofsquares(const data : array of Extended) : float;
  899. begin
  900. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  901. end;
  902. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  903. var
  904. i : longint;
  905. begin
  906. sumofsquares:=0.0;
  907. for i:=0 to N-1 do
  908. sumofsquares:=sumofsquares+sqr(data[i]);
  909. end;
  910. procedure sumsandsquares(const data : array of Extended;
  911. var sum,sumofsquares : float);
  912. begin
  913. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  914. end;
  915. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  916. var sum,sumofsquares : float);
  917. var
  918. i : Integer;
  919. temp : float;
  920. begin
  921. sumofsquares:=0.0;
  922. sum:=0.0;
  923. for i:=0 to N-1 do
  924. begin
  925. temp:=data[i];
  926. sumofsquares:=sumofsquares+sqr(temp);
  927. sum:=sum+temp;
  928. end;
  929. end;
  930. {$endif FPC_HAS_TYPE_EXTENDED}
  931. function randg(mean,stddev : float) : float;
  932. Var U1,S2 : Float;
  933. begin
  934. repeat
  935. u1:= 2*random-1;
  936. S2:=Sqr(U1)+sqr(2*random-1);
  937. until s2<1;
  938. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  939. end;
  940. {$ifdef FPC_HAS_TYPE_SINGLE}
  941. function stddev(const data : array of Single) : float;
  942. begin
  943. Result:=Stddev(PSingle(@Data[0]),High(Data)+1)
  944. end;
  945. function stddev(const data : PSingle; Const N : Integer) : float;
  946. begin
  947. StdDev:=Sqrt(Variance(Data,N));
  948. end;
  949. procedure meanandstddev(const data : array of Single;
  950. var mean,stddev : float);
  951. begin
  952. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  953. end;
  954. procedure meanandstddev(const data : PSingle;
  955. Const N : Longint;var mean,stddev : float);
  956. Var I : longint;
  957. begin
  958. Mean:=0;
  959. StdDev:=0;
  960. For I:=0 to N-1 do
  961. begin
  962. Mean:=Mean+Data[i];
  963. StdDev:=StdDev+Sqr(Data[i]);
  964. end;
  965. Mean:=Mean/N;
  966. StdDev:=(StdDev-N*Sqr(Mean));
  967. If N>1 then
  968. StdDev:=Sqrt(Stddev/(N-1))
  969. else
  970. StdDev:=0;
  971. end;
  972. function variance(const data : array of Single) : float;
  973. begin
  974. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  975. end;
  976. function variance(const data : PSingle; Const N : Integer) : float;
  977. begin
  978. If N=1 then
  979. Result:=0
  980. else
  981. Result:=TotalVariance(Data,N)/(N-1);
  982. end;
  983. function totalvariance(const data : array of Single) : float;
  984. begin
  985. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  986. end;
  987. function totalvariance(const data : PSingle;Const N : Integer) : float;
  988. var S,SS : Float;
  989. begin
  990. If N=1 then
  991. Result:=0
  992. else
  993. begin
  994. SumsAndSquares(Data,N,S,SS);
  995. Result := SS-Sqr(S)/N;
  996. end;
  997. end;
  998. function popnstddev(const data : array of Single) : float;
  999. begin
  1000. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  1001. end;
  1002. function popnstddev(const data : PSingle; Const N : Integer) : float;
  1003. begin
  1004. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1005. end;
  1006. function popnvariance(const data : array of Single) : float;
  1007. begin
  1008. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  1009. end;
  1010. function popnvariance(const data : PSingle; Const N : Integer) : float;
  1011. begin
  1012. PopnVariance:=TotalVariance(Data,N)/N;
  1013. end;
  1014. procedure momentskewkurtosis(const data : array of Single;
  1015. var m1,m2,m3,m4,skew,kurtosis : float);
  1016. begin
  1017. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1018. end;
  1019. procedure momentskewkurtosis(const data : PSingle; Const N : Integer;
  1020. var m1,m2,m3,m4,skew,kurtosis : float);
  1021. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  1022. I : Longint;
  1023. begin
  1024. invN:=1.0/N;
  1025. s:=0;
  1026. ss:=0;
  1027. sq:=0;
  1028. sc:=0;
  1029. for i:=0 to N-1 do
  1030. begin
  1031. temp:=Data[i]; { faster }
  1032. S:=S+temp;
  1033. acc:=sqr(temp);
  1034. ss:=ss+acc;
  1035. Acc:=acc*temp;
  1036. Sc:=sc+acc;
  1037. acc:=acc*temp;
  1038. sq:=sq+acc;
  1039. end;
  1040. M1:=s*invN;
  1041. M1S:=sqr(M1);
  1042. S2N:=SS*invN;
  1043. S3N:=SC*invN;
  1044. M2:=S2N-M1S;
  1045. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  1046. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  1047. Skew:=M3*power(M2,-3/2);
  1048. Kurtosis:=M4 / Sqr(M2);
  1049. end;
  1050. function norm(const data : array of Single) : float;
  1051. begin
  1052. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  1053. end;
  1054. function norm(const data : PSingle; Const N : Integer) : float;
  1055. begin
  1056. norm:=sqrt(sumofsquares(data,N));
  1057. end;
  1058. {$endif FPC_HAS_TYPE_SINGLE}
  1059. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1060. function stddev(const data : array of Double) : float;
  1061. begin
  1062. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  1063. end;
  1064. function stddev(const data : PDouble; Const N : Integer) : float;
  1065. begin
  1066. StdDev:=Sqrt(Variance(Data,N));
  1067. end;
  1068. procedure meanandstddev(const data : array of Double;
  1069. var mean,stddev : float);
  1070. begin
  1071. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  1072. end;
  1073. procedure meanandstddev(const data : PDouble;
  1074. Const N : Longint;var mean,stddev : float);
  1075. Var I : longint;
  1076. begin
  1077. Mean:=0;
  1078. StdDev:=0;
  1079. For I:=0 to N-1 do
  1080. begin
  1081. Mean:=Mean+Data[i];
  1082. StdDev:=StdDev+Sqr(Data[i]);
  1083. end;
  1084. Mean:=Mean/N;
  1085. StdDev:=(StdDev-N*Sqr(Mean));
  1086. If N>1 then
  1087. StdDev:=Sqrt(Stddev/(N-1))
  1088. else
  1089. StdDev:=0;
  1090. end;
  1091. function variance(const data : array of Double) : float;
  1092. begin
  1093. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  1094. end;
  1095. function variance(const data : PDouble; Const N : Integer) : float;
  1096. begin
  1097. If N=1 then
  1098. Result:=0
  1099. else
  1100. Result:=TotalVariance(Data,N)/(N-1);
  1101. end;
  1102. function totalvariance(const data : array of Double) : float;
  1103. begin
  1104. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  1105. end;
  1106. function totalvariance(const data : PDouble;Const N : Integer) : float;
  1107. var S,SS : Float;
  1108. begin
  1109. If N=1 then
  1110. Result:=0
  1111. else
  1112. begin
  1113. SumsAndSquares(Data,N,S,SS);
  1114. Result := SS-Sqr(S)/N;
  1115. end;
  1116. end;
  1117. function popnstddev(const data : array of Double) : float;
  1118. begin
  1119. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  1120. end;
  1121. function popnstddev(const data : PDouble; Const N : Integer) : float;
  1122. begin
  1123. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1124. end;
  1125. function popnvariance(const data : array of Double) : float;
  1126. begin
  1127. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  1128. end;
  1129. function popnvariance(const data : PDouble; Const N : Integer) : float;
  1130. begin
  1131. PopnVariance:=TotalVariance(Data,N)/N;
  1132. end;
  1133. procedure momentskewkurtosis(const data : array of Double;
  1134. var m1,m2,m3,m4,skew,kurtosis : float);
  1135. begin
  1136. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1137. end;
  1138. procedure momentskewkurtosis(const data : PDouble; Const N : Integer;
  1139. var m1,m2,m3,m4,skew,kurtosis : float);
  1140. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  1141. I : Longint;
  1142. begin
  1143. invN:=1.0/N;
  1144. s:=0;
  1145. ss:=0;
  1146. sq:=0;
  1147. sc:=0;
  1148. for i:=0 to N-1 do
  1149. begin
  1150. temp:=Data[i]; { faster }
  1151. S:=S+temp;
  1152. acc:=sqr(temp);
  1153. ss:=ss+acc;
  1154. Acc:=acc*temp;
  1155. Sc:=sc+acc;
  1156. acc:=acc*temp;
  1157. sq:=sq+acc;
  1158. end;
  1159. M1:=s*invN;
  1160. M1S:=sqr(M1);
  1161. S2N:=SS*invN;
  1162. S3N:=SC*invN;
  1163. M2:=S2N-M1S;
  1164. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  1165. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  1166. Skew:=M3*power(M2,-3/2);
  1167. Kurtosis:=M4 / Sqr(M2);
  1168. end;
  1169. function norm(const data : array of Double) : float;
  1170. begin
  1171. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  1172. end;
  1173. function norm(const data : PDouble; Const N : Integer) : float;
  1174. begin
  1175. norm:=sqrt(sumofsquares(data,N));
  1176. end;
  1177. {$endif FPC_HAS_TYPE_DOUBLE}
  1178. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1179. function stddev(const data : array of Extended) : float;
  1180. begin
  1181. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  1182. end;
  1183. function stddev(const data : PExtended; Const N : Integer) : float;
  1184. begin
  1185. StdDev:=Sqrt(Variance(Data,N));
  1186. end;
  1187. procedure meanandstddev(const data : array of Extended;
  1188. var mean,stddev : float);
  1189. begin
  1190. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  1191. end;
  1192. procedure meanandstddev(const data : PExtended;
  1193. Const N : Longint;var mean,stddev : float);
  1194. Var I : longint;
  1195. begin
  1196. Mean:=0;
  1197. StdDev:=0;
  1198. For I:=0 to N-1 do
  1199. begin
  1200. Mean:=Mean+Data[i];
  1201. StdDev:=StdDev+Sqr(Data[i]);
  1202. end;
  1203. Mean:=Mean/N;
  1204. StdDev:=(StdDev-N*Sqr(Mean));
  1205. If N>1 then
  1206. StdDev:=Sqrt(Stddev/(N-1))
  1207. else
  1208. StdDev:=0;
  1209. end;
  1210. function variance(const data : array of Extended) : float;
  1211. begin
  1212. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  1213. end;
  1214. function variance(const data : PExtended; Const N : Integer) : float;
  1215. begin
  1216. If N=1 then
  1217. Result:=0
  1218. else
  1219. Result:=TotalVariance(Data,N)/(N-1);
  1220. end;
  1221. function totalvariance(const data : array of Extended) : float;
  1222. begin
  1223. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  1224. end;
  1225. function totalvariance(const data : PExtended;Const N : Integer) : float;
  1226. var S,SS : Float;
  1227. begin
  1228. If N=1 then
  1229. Result:=0
  1230. else
  1231. begin
  1232. SumsAndSquares(Data,N,S,SS);
  1233. Result := SS-Sqr(S)/N;
  1234. end;
  1235. end;
  1236. function popnstddev(const data : array of Extended) : float;
  1237. begin
  1238. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  1239. end;
  1240. function popnstddev(const data : PExtended; Const N : Integer) : float;
  1241. begin
  1242. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1243. end;
  1244. function popnvariance(const data : array of Extended) : float;
  1245. begin
  1246. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  1247. end;
  1248. function popnvariance(const data : PExtended; Const N : Integer) : float;
  1249. begin
  1250. PopnVariance:=TotalVariance(Data,N)/N;
  1251. end;
  1252. procedure momentskewkurtosis(const data : array of Extended;
  1253. var m1,m2,m3,m4,skew,kurtosis : float);
  1254. begin
  1255. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1256. end;
  1257. procedure momentskewkurtosis(const data : PExtended; Const N : Integer;
  1258. var m1,m2,m3,m4,skew,kurtosis : float);
  1259. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  1260. I : Longint;
  1261. begin
  1262. invN:=1.0/N;
  1263. s:=0;
  1264. ss:=0;
  1265. sq:=0;
  1266. sc:=0;
  1267. for i:=0 to N-1 do
  1268. begin
  1269. temp:=Data[i]; { faster }
  1270. S:=S+temp;
  1271. acc:=sqr(temp);
  1272. ss:=ss+acc;
  1273. Acc:=acc*temp;
  1274. Sc:=sc+acc;
  1275. acc:=acc*temp;
  1276. sq:=sq+acc;
  1277. end;
  1278. M1:=s*invN;
  1279. M1S:=sqr(M1);
  1280. S2N:=SS*invN;
  1281. S3N:=SC*invN;
  1282. M2:=S2N-M1S;
  1283. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  1284. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  1285. Skew:=M3*power(M2,-3/2);
  1286. Kurtosis:=M4 / Sqr(M2);
  1287. end;
  1288. function norm(const data : array of Extended) : float;
  1289. begin
  1290. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  1291. end;
  1292. function norm(const data : PExtended; Const N : Integer) : float;
  1293. begin
  1294. norm:=sqrt(sumofsquares(data,N));
  1295. end;
  1296. {$endif FPC_HAS_TYPE_EXTENDED}
  1297. function MinIntValue(const Data: array of Integer): Integer;
  1298. var
  1299. I: Integer;
  1300. begin
  1301. Result := Data[Low(Data)];
  1302. For I := Succ(Low(Data)) To High(Data) Do
  1303. If Data[I] < Result Then Result := Data[I];
  1304. end;
  1305. function MaxIntValue(const Data: array of Integer): Integer;
  1306. var
  1307. I: Integer;
  1308. begin
  1309. Result := Data[Low(Data)];
  1310. For I := Succ(Low(Data)) To High(Data) Do
  1311. If Data[I] > Result Then Result := Data[I];
  1312. end;
  1313. function MinValue(const Data: array of Integer): Integer;
  1314. begin
  1315. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  1316. end;
  1317. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  1318. var
  1319. I: Integer;
  1320. begin
  1321. Result := Data[0];
  1322. For I := 1 To N-1 do
  1323. If Data[I] < Result Then Result := Data[I];
  1324. end;
  1325. function MaxValue(const Data: array of Integer): Integer;
  1326. begin
  1327. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  1328. end;
  1329. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  1330. var
  1331. i : longint;
  1332. begin
  1333. { get an initial value }
  1334. maxvalue:=data[0];
  1335. for i:=1 to N-1 do
  1336. if data[i]>maxvalue then
  1337. maxvalue:=data[i];
  1338. end;
  1339. {$ifdef FPC_HAS_TYPE_SINGLE}
  1340. function minvalue(const data : array of Single) : Single;
  1341. begin
  1342. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  1343. end;
  1344. function minvalue(const data : PSingle; Const N : Integer) : Single;
  1345. var
  1346. i : longint;
  1347. begin
  1348. { get an initial value }
  1349. minvalue:=data[0];
  1350. for i:=1 to N-1 do
  1351. if data[i]<minvalue then
  1352. minvalue:=data[i];
  1353. end;
  1354. function maxvalue(const data : array of Single) : Single;
  1355. begin
  1356. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  1357. end;
  1358. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  1359. var
  1360. i : longint;
  1361. begin
  1362. { get an initial value }
  1363. maxvalue:=data[0];
  1364. for i:=1 to N-1 do
  1365. if data[i]>maxvalue then
  1366. maxvalue:=data[i];
  1367. end;
  1368. {$endif FPC_HAS_TYPE_SINGLE}
  1369. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1370. function minvalue(const data : array of Double) : Double;
  1371. begin
  1372. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  1373. end;
  1374. function minvalue(const data : PDouble; Const N : Integer) : Double;
  1375. var
  1376. i : longint;
  1377. begin
  1378. { get an initial value }
  1379. minvalue:=data[0];
  1380. for i:=1 to N-1 do
  1381. if data[i]<minvalue then
  1382. minvalue:=data[i];
  1383. end;
  1384. function maxvalue(const data : array of Double) : Double;
  1385. begin
  1386. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  1387. end;
  1388. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  1389. var
  1390. i : longint;
  1391. begin
  1392. { get an initial value }
  1393. maxvalue:=data[0];
  1394. for i:=1 to N-1 do
  1395. if data[i]>maxvalue then
  1396. maxvalue:=data[i];
  1397. end;
  1398. {$endif FPC_HAS_TYPE_DOUBLE}
  1399. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1400. function minvalue(const data : array of Extended) : Extended;
  1401. begin
  1402. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  1403. end;
  1404. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  1405. var
  1406. i : longint;
  1407. begin
  1408. { get an initial value }
  1409. minvalue:=data[0];
  1410. for i:=1 to N-1 do
  1411. if data[i]<minvalue then
  1412. minvalue:=data[i];
  1413. end;
  1414. function maxvalue(const data : array of Extended) : Extended;
  1415. begin
  1416. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  1417. end;
  1418. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  1419. var
  1420. i : longint;
  1421. begin
  1422. { get an initial value }
  1423. maxvalue:=data[0];
  1424. for i:=1 to N-1 do
  1425. if data[i]>maxvalue then
  1426. maxvalue:=data[i];
  1427. end;
  1428. {$endif FPC_HAS_TYPE_EXTENDED}
  1429. function Min(a, b: Integer): Integer;inline;
  1430. begin
  1431. if a < b then
  1432. Result := a
  1433. else
  1434. Result := b;
  1435. end;
  1436. function Max(a, b: Integer): Integer;inline;
  1437. begin
  1438. if a > b then
  1439. Result := a
  1440. else
  1441. Result := b;
  1442. end;
  1443. {
  1444. function Min(a, b: Cardinal): Cardinal;inline;
  1445. begin
  1446. if a < b then
  1447. Result := a
  1448. else
  1449. Result := b;
  1450. end;
  1451. function Max(a, b: Cardinal): Cardinal;inline;
  1452. begin
  1453. if a > b then
  1454. Result := a
  1455. else
  1456. Result := b;
  1457. end;
  1458. }
  1459. function Min(a, b: Int64): Int64;inline;
  1460. begin
  1461. if a < b then
  1462. Result := a
  1463. else
  1464. Result := b;
  1465. end;
  1466. function Max(a, b: Int64): Int64;inline;
  1467. begin
  1468. if a > b then
  1469. Result := a
  1470. else
  1471. Result := b;
  1472. end;
  1473. {$ifdef FPC_HAS_TYPE_SINGLE}
  1474. function Min(a, b: Single): Single;inline;
  1475. begin
  1476. if a < b then
  1477. Result := a
  1478. else
  1479. Result := b;
  1480. end;
  1481. function Max(a, b: Single): Single;inline;
  1482. begin
  1483. if a > b then
  1484. Result := a
  1485. else
  1486. Result := b;
  1487. end;
  1488. {$endif FPC_HAS_TYPE_SINGLE}
  1489. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1490. function Min(a, b: Double): Double;inline;
  1491. begin
  1492. if a < b then
  1493. Result := a
  1494. else
  1495. Result := b;
  1496. end;
  1497. function Max(a, b: Double): Double;inline;
  1498. begin
  1499. if a > b then
  1500. Result := a
  1501. else
  1502. Result := b;
  1503. end;
  1504. {$endif FPC_HAS_TYPE_DOUBLE}
  1505. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1506. function Min(a, b: Extended): Extended;inline;
  1507. begin
  1508. if a < b then
  1509. Result := a
  1510. else
  1511. Result := b;
  1512. end;
  1513. function Max(a, b: Extended): Extended;inline;
  1514. begin
  1515. if a > b then
  1516. Result := a
  1517. else
  1518. Result := b;
  1519. end;
  1520. {$endif FPC_HAS_TYPE_EXTENDED}
  1521. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  1522. begin
  1523. Result:=(AValue>=AMin) and (AValue<=AMax);
  1524. end;
  1525. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  1526. begin
  1527. Result:=(AValue>=AMin) and (AValue<=AMax);
  1528. end;
  1529. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1530. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  1531. begin
  1532. Result:=(AValue>=AMin) and (AValue<=AMax);
  1533. end;
  1534. {$endif FPC_HAS_TYPE_DOUBLE}
  1535. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  1536. begin
  1537. Result:=AValue;
  1538. If Result<AMin then
  1539. Result:=AMin
  1540. else if Result>AMax then
  1541. Result:=AMax;
  1542. end;
  1543. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  1544. begin
  1545. Result:=AValue;
  1546. If Result<AMin then
  1547. Result:=AMin
  1548. else if Result>AMax then
  1549. Result:=AMax;
  1550. end;
  1551. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1552. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  1553. begin
  1554. Result:=AValue;
  1555. If Result<AMin then
  1556. Result:=AMin
  1557. else if Result>AMax then
  1558. Result:=AMax;
  1559. end;
  1560. {$endif FPC_HAS_TYPE_DOUBLE}
  1561. Const
  1562. EZeroResolution = 1E-16;
  1563. DZeroResolution = 1E-12;
  1564. SZeroResolution = 1E-4;
  1565. function IsZero(const A: Single; Epsilon: Single): Boolean;
  1566. begin
  1567. if (Epsilon=0) then
  1568. Epsilon:=SZeroResolution;
  1569. Result:=Abs(A)<=Epsilon;
  1570. end;
  1571. function IsZero(const A: Single): Boolean;inline;
  1572. begin
  1573. Result:=IsZero(A,single(SZeroResolution));
  1574. end;
  1575. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1576. function IsZero(const A: Double; Epsilon: Double): Boolean;
  1577. begin
  1578. if (Epsilon=0) then
  1579. Epsilon:=DZeroResolution;
  1580. Result:=Abs(A)<=Epsilon;
  1581. end;
  1582. function IsZero(const A: Double): Boolean;inline;
  1583. begin
  1584. Result:=IsZero(A,DZeroResolution);
  1585. end;
  1586. {$endif FPC_HAS_TYPE_DOUBLE}
  1587. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1588. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  1589. begin
  1590. if (Epsilon=0) then
  1591. Epsilon:=EZeroResolution;
  1592. Result:=Abs(A)<=Epsilon;
  1593. end;
  1594. function IsZero(const A: Extended): Boolean;inline;
  1595. begin
  1596. Result:=IsZero(A,EZeroResolution);
  1597. end;
  1598. {$endif FPC_HAS_TYPE_EXTENDED}
  1599. type
  1600. TSplitDouble = packed record
  1601. cards: Array[0..1] of cardinal;
  1602. end;
  1603. function IsNan(const d : Double): Boolean;
  1604. var
  1605. fraczero, expMaximal: boolean;
  1606. begin
  1607. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1608. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1609. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1610. (TSplitDouble(d).cards[1] = 0);
  1611. {$else FPC_BIG_ENDIAN}
  1612. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1613. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1614. (TSplitDouble(d).cards[0] = 0);
  1615. {$endif FPC_BIG_ENDIAN}
  1616. Result:=expMaximal and not(fraczero);
  1617. end;
  1618. function IsInfinite(const d : Double): Boolean;
  1619. var
  1620. fraczero, expMaximal: boolean;
  1621. begin
  1622. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1623. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1624. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1625. (TSplitDouble(d).cards[1] = 0);
  1626. {$else FPC_BIG_ENDIAN}
  1627. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1628. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1629. (TSplitDouble(d).cards[0] = 0);
  1630. {$endif FPC_BIG_ENDIAN}
  1631. Result:=expMaximal and fraczero;
  1632. end;
  1633. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1634. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1635. begin
  1636. if (Epsilon=0) then
  1637. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1638. if (A>B) then
  1639. Result:=((A-B)<=Epsilon)
  1640. else
  1641. Result:=((B-A)<=Epsilon);
  1642. end;
  1643. function SameValue(const A, B: Extended): Boolean;inline;
  1644. begin
  1645. Result:=SameValue(A,B,0);
  1646. end;
  1647. {$endif FPC_HAS_TYPE_EXTENDED}
  1648. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1649. function SameValue(const A, B: Double): Boolean;inline;
  1650. begin
  1651. Result:=SameValue(A,B,0);
  1652. end;
  1653. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1654. begin
  1655. if (Epsilon=0) then
  1656. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1657. if (A>B) then
  1658. Result:=((A-B)<=Epsilon)
  1659. else
  1660. Result:=((B-A)<=Epsilon);
  1661. end;
  1662. {$endif FPC_HAS_TYPE_DOUBLE}
  1663. function SameValue(const A, B: Single): Boolean;inline;
  1664. begin
  1665. Result:=SameValue(A,B,0);
  1666. end;
  1667. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  1668. begin
  1669. if (Epsilon=0) then
  1670. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  1671. if (A>B) then
  1672. Result:=((A-B)<=Epsilon)
  1673. else
  1674. Result:=((B-A)<=Epsilon);
  1675. end;
  1676. // Some CPUs probably allow a faster way of doing this in a single operation...
  1677. // There weshould define CPUDIVMOD in the header mathuh.inc and implement it using asm.
  1678. {$ifndef CPUDIVMOD}
  1679. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  1680. begin
  1681. Result:=Dividend Div Divisor;
  1682. Remainder:=Dividend Mod Divisor;
  1683. end;
  1684. {$endif}
  1685. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: SmallInt);
  1686. var
  1687. UnsignedResult: Word absolute Result;
  1688. UnsignedRemainder: Word absolute Remainder;
  1689. begin
  1690. DivMod(Dividend, Divisor, UnsignedResult, UnsignedRemainder);
  1691. end;
  1692. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  1693. begin
  1694. if val then result:=iftrue else result:=iffalse;
  1695. end;
  1696. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  1697. begin
  1698. if val then result:=iftrue else result:=iffalse;
  1699. end;
  1700. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  1701. begin
  1702. if val then result:=iftrue else result:=iffalse;
  1703. end;
  1704. function ifthen(val:boolean;const iftrue:String ; const iffalse:String ='') :String; inline;
  1705. begin
  1706. if val then result:=iftrue else result:=iffalse;
  1707. end;
  1708. // dilemma here. asm can do the two comparisons in one go?
  1709. // but pascal is portable and can be i inline;ed. Ah well, we need purepascal's anyway:
  1710. function CompareValue ( const A, B : Integer) : TValueRelationship;
  1711. begin
  1712. result:=GreaterThanValue;
  1713. if a=b then
  1714. result:=EqualsValue
  1715. else
  1716. if a<b then
  1717. result:=LessThanValue;
  1718. end;
  1719. function CompareValue ( const A, B : Int64) : TValueRelationship;
  1720. begin
  1721. result:=GreaterThanValue;
  1722. if a=b then
  1723. result:=EqualsValue
  1724. else
  1725. if a<b then
  1726. result:=LessThanValue;
  1727. end;
  1728. {$ifdef FPC_HAS_TYPE_SINGLE}
  1729. function CompareValue ( const A, B : Single; delta : Single = 0.0) : TValueRelationship;
  1730. begin
  1731. result:=GreaterThanValue;
  1732. if abs(a-b)<=delta then
  1733. result:=EqualsValue
  1734. else
  1735. if a<b then
  1736. result:=LessThanValue;
  1737. end;
  1738. {$endif}
  1739. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1740. function CompareValue ( const A, B : Double; delta : Double = 0.0) : TValueRelationship;
  1741. begin
  1742. result:=GreaterThanValue;
  1743. if abs(a-b)<=delta then
  1744. result:=EqualsValue
  1745. else
  1746. if a<b then
  1747. result:=LessThanValue;
  1748. end;
  1749. {$endif}
  1750. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1751. function CompareValue ( const A, B : Extended; delta : Extended = 0.0) : TValueRelationship;
  1752. begin
  1753. result:=GreaterThanValue;
  1754. if abs(a-b)<=delta then
  1755. result:=EqualsValue
  1756. else
  1757. if a<b then
  1758. result:=LessThanValue;
  1759. end;
  1760. {$endif}
  1761. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1762. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  1763. var
  1764. RV : Double;
  1765. begin
  1766. RV:=IntPower(10,Digits);
  1767. Result:=Round(AValue/RV)*RV;
  1768. end;
  1769. {$endif}
  1770. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1771. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  1772. var
  1773. RV : Extended;
  1774. begin
  1775. RV:=IntPower(10,Digits);
  1776. Result:=Round(AValue/RV)*RV;
  1777. end;
  1778. {$endif}
  1779. {$ifdef FPC_HAS_TYPE_SINGLE}
  1780. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  1781. var
  1782. RV : Single;
  1783. begin
  1784. RV:=IntPower(10,Digits);
  1785. Result:=Round(AValue/RV)*RV;
  1786. end;
  1787. {$endif}
  1788. {$ifdef FPC_HAS_TYPE_SINGLE}
  1789. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  1790. var
  1791. RV : Single;
  1792. begin
  1793. RV:=IntPower(10,Digits);
  1794. Result:=Trunc((AValue/RV)+0.5)*RV;
  1795. end;
  1796. {$endif}
  1797. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1798. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  1799. var
  1800. RV : Double;
  1801. begin
  1802. RV:=IntPower(10,Digits);
  1803. Result:=Trunc((AValue/RV)+0.5)*RV;
  1804. end;
  1805. {$endif}
  1806. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1807. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  1808. var
  1809. RV : Extended;
  1810. begin
  1811. RV:=IntPower(10,Digits);
  1812. Result:=Trunc((AValue/RV)+0.5)*RV;
  1813. end;
  1814. {$endif}
  1815. end.