math.pp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527
  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. {$push}
  97. {$R-}
  98. {$Q-}
  99. NaN = 0.0/0.0;
  100. Infinity = 1.0/0.0;
  101. NegInfinity = -1.0/0.0;
  102. {$pop}
  103. { Min/max determination }
  104. function MinIntValue(const Data: array of Integer): Integer;
  105. function MaxIntValue(const Data: array of Integer): Integer;
  106. { Extra, not present in Delphi, but used frequently }
  107. function Min(a, b: Integer): Integer;inline; overload;
  108. function Max(a, b: Integer): Integer;inline; overload;
  109. { this causes more trouble than it solves
  110. function Min(a, b: Cardinal): Cardinal; overload;
  111. function Max(a, b: Cardinal): Cardinal; overload;
  112. }
  113. function Min(a, b: Int64): Int64;inline; overload;
  114. function Max(a, b: Int64): Int64;inline; overload;
  115. {$ifdef FPC_HAS_TYPE_SINGLE}
  116. function Min(a, b: Single): Single;inline; overload;
  117. function Max(a, b: Single): Single;inline; overload;
  118. {$endif FPC_HAS_TYPE_SINGLE}
  119. {$ifdef FPC_HAS_TYPE_DOUBLE}
  120. function Min(a, b: Double): Double;inline; overload;
  121. function Max(a, b: Double): Double;inline; overload;
  122. {$endif FPC_HAS_TYPE_DOUBLE}
  123. {$ifdef FPC_HAS_TYPE_EXTENDED}
  124. function Min(a, b: Extended): Extended;inline; overload;
  125. function Max(a, b: Extended): Extended;inline; overload;
  126. {$endif FPC_HAS_TYPE_EXTENDED}
  127. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline; overload;
  128. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline; overload;
  129. {$ifdef FPC_HAS_TYPE_DOUBLE}
  130. function InRange(const AValue, AMin, AMax: Double): Boolean;inline; overload;
  131. {$endif FPC_HAS_TYPE_DOUBLE}
  132. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline; overload;
  133. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline; overload;
  134. {$ifdef FPC_HAS_TYPE_DOUBLE}
  135. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline; overload;
  136. {$endif FPC_HAS_TYPE_DOUBLE}
  137. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  138. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: SmallInt);
  139. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  140. procedure DivMod(Dividend: Integer; Divisor: Integer; var Result, Remainder: Integer);
  141. // Sign functions
  142. Type
  143. TValueSign = -1..1;
  144. const
  145. NegativeValue = Low(TValueSign);
  146. ZeroValue = 0;
  147. PositiveValue = High(TValueSign);
  148. function Sign(const AValue: Integer): TValueSign;inline; overload;
  149. function Sign(const AValue: Int64): TValueSign;inline; overload;
  150. {$ifdef FPC_HAS_TYPE_SINGLE}
  151. function Sign(const AValue: Single): TValueSign;inline; overload;
  152. {$endif}
  153. function Sign(const AValue: Double): TValueSign;inline; overload;
  154. {$ifdef FPC_HAS_TYPE_EXTENDED}
  155. function Sign(const AValue: Extended): TValueSign;inline; overload;
  156. {$endif}
  157. function IsZero(const A: Single; Epsilon: Single): Boolean; overload;
  158. function IsZero(const A: Single): Boolean;inline; overload;
  159. {$ifdef FPC_HAS_TYPE_DOUBLE}
  160. function IsZero(const A: Double; Epsilon: Double): Boolean; overload;
  161. function IsZero(const A: Double): Boolean;inline; overload;
  162. {$endif FPC_HAS_TYPE_DOUBLE}
  163. {$ifdef FPC_HAS_TYPE_EXTENDED}
  164. function IsZero(const A: Extended; Epsilon: Extended): Boolean; overload;
  165. function IsZero(const A: Extended): Boolean;inline; overload;
  166. {$endif FPC_HAS_TYPE_EXTENDED}
  167. function IsNan(const d : Single): Boolean; overload;
  168. {$ifdef FPC_HAS_TYPE_DOUBLE}
  169. function IsNan(const d : Double): Boolean; overload;
  170. {$endif FPC_HAS_TYPE_DOUBLE}
  171. {$ifdef FPC_HAS_TYPE_EXTENDED}
  172. function IsNan(const d : Extended): Boolean; overload;
  173. {$endif FPC_HAS_TYPE_EXTENDED}
  174. function IsInfinite(const d : Double): Boolean;
  175. {$ifdef FPC_HAS_TYPE_EXTENDED}
  176. function SameValue(const A, B: Extended): Boolean;inline; overload;
  177. {$endif}
  178. {$ifdef FPC_HAS_TYPE_DOUBLE}
  179. function SameValue(const A, B: Double): Boolean;inline; overload;
  180. {$endif}
  181. function SameValue(const A, B: Single): Boolean;inline; overload;
  182. {$ifdef FPC_HAS_TYPE_EXTENDED}
  183. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean; overload;
  184. {$endif}
  185. {$ifdef FPC_HAS_TYPE_DOUBLE}
  186. function SameValue(const A, B: Double; Epsilon: Double): Boolean; overload;
  187. {$endif}
  188. function SameValue(const A, B: Single; Epsilon: Single): Boolean; overload;
  189. type
  190. TRoundToRange = -37..37;
  191. {$ifdef FPC_HAS_TYPE_DOUBLE}
  192. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  193. {$endif}
  194. {$ifdef FPC_HAS_TYPE_EXTENDED}
  195. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  196. {$endif}
  197. {$ifdef FPC_HAS_TYPE_SINGLE}
  198. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  199. {$endif}
  200. {$ifdef FPC_HAS_TYPE_SINGLE}
  201. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  202. {$endif}
  203. {$ifdef FPC_HAS_TYPE_DOUBLE}
  204. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  205. {$endif}
  206. {$ifdef FPC_HAS_TYPE_EXTENDED}
  207. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  208. {$endif}
  209. { angle conversion }
  210. function degtorad(deg : float) : float;
  211. function radtodeg(rad : float) : float;
  212. function gradtorad(grad : float) : float;
  213. function radtograd(rad : float) : float;
  214. function degtograd(deg : float) : float;
  215. function gradtodeg(grad : float) : float;
  216. { one cycle are 2*Pi rad }
  217. function cycletorad(cycle : float) : float;
  218. function radtocycle(rad : float) : float;
  219. { trigoniometric functions }
  220. function tan(x : float) : float;
  221. function cotan(x : float) : float;
  222. function cot(x : float) : float; inline;
  223. procedure sincos(theta : float;out sinus,cosinus : float);
  224. function secant(x : float) : float; inline;
  225. function cosecant(x : float) : float; inline;
  226. function sec(x : float) : float; inline;
  227. function csc(x : float) : float; inline;
  228. { inverse functions }
  229. function arccos(x : float) : float;
  230. function arcsin(x : float) : float;
  231. { calculates arctan(y/x) and returns an angle in the correct quadrant }
  232. function arctan2(y,x : float) : float;
  233. { hyperbolic functions }
  234. function cosh(x : float) : float;
  235. function sinh(x : float) : float;
  236. function tanh(x : float) : float;
  237. { area functions }
  238. { delphi names: }
  239. function arccosh(x : float) : float;
  240. function arcsinh(x : float) : float;
  241. function arctanh(x : float) : float;
  242. { IMHO the function should be called as follows (FK) }
  243. function arcosh(x : float) : float;
  244. function arsinh(x : float) : float;
  245. function artanh(x : float) : float;
  246. { triangle functions }
  247. { returns the length of the hypotenuse of a right triangle }
  248. { if x and y are the other sides }
  249. function hypot(x,y : float) : float;
  250. { logarithm functions }
  251. function log10(x : float) : float;
  252. function log2(x : float) : float;
  253. function logn(n,x : float) : float;
  254. { returns natural logarithm of x+1 }
  255. function lnxp1(x : float) : float;
  256. { exponential functions }
  257. function power(base,exponent : float) : float;
  258. { base^exponent }
  259. function intpower(base : float;const exponent : Integer) : float;
  260. operator ** (bas,expo : float) e: float; inline;
  261. operator ** (bas,expo : int64) i: int64; inline;
  262. { number converting }
  263. { rounds x towards positive infinity }
  264. function ceil(x : float) : Integer;
  265. { rounds x towards negative infinity }
  266. function floor(x : float) : Integer;
  267. { misc. functions }
  268. { splits x into mantissa and exponent (to base 2) }
  269. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  270. { returns x*(2^p) }
  271. function ldexp(x : float; const p : Integer) : float;
  272. { statistical functions }
  273. {$ifdef FPC_HAS_TYPE_SINGLE}
  274. function mean(const data : array of Single) : float;
  275. function sum(const data : array of Single) : float;
  276. function mean(const data : PSingle; Const N : longint) : float;
  277. function sum(const data : PSingle; Const N : Longint) : float;
  278. {$endif FPC_HAS_TYPE_SINGLE}
  279. {$ifdef FPC_HAS_TYPE_DOUBLE}
  280. function mean(const data : array of double) : float;
  281. function sum(const data : array of double) : float;
  282. function mean(const data : PDouble; Const N : longint) : float;
  283. function sum(const data : PDouble; Const N : Longint) : float;
  284. {$endif FPC_HAS_TYPE_DOUBLE}
  285. {$ifdef FPC_HAS_TYPE_EXTENDED}
  286. function mean(const data : array of Extended) : float;
  287. function sum(const data : array of Extended) : float;
  288. function mean(const data : PExtended; Const N : longint) : float;
  289. function sum(const data : PExtended; Const N : Longint) : float;
  290. {$endif FPC_HAS_TYPE_EXTENDED}
  291. function sumInt(const data : PInt64;Const N : longint) : Int64;
  292. function sumInt(const data : array of Int64) : Int64;
  293. {$ifdef FPC_HAS_TYPE_SINGLE}
  294. function sumofsquares(const data : array of Single) : float;
  295. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  296. { calculates the sum and the sum of squares of data }
  297. procedure sumsandsquares(const data : array of Single;
  298. var sum,sumofsquares : float);
  299. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  300. var sum,sumofsquares : float);
  301. {$endif FPC_HAS_TYPE_SINGLE}
  302. {$ifdef FPC_HAS_TYPE_DOUBLE}
  303. function sumofsquares(const data : array of double) : float;
  304. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  305. { calculates the sum and the sum of squares of data }
  306. procedure sumsandsquares(const data : array of Double;
  307. var sum,sumofsquares : float);
  308. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  309. var sum,sumofsquares : float);
  310. {$endif FPC_HAS_TYPE_DOUBLE}
  311. {$ifdef FPC_HAS_TYPE_EXTENDED}
  312. function sumofsquares(const data : array of Extended) : float;
  313. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  314. { calculates the sum and the sum of squares of data }
  315. procedure sumsandsquares(const data : array of Extended;
  316. var sum,sumofsquares : float);
  317. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  318. var sum,sumofsquares : float);
  319. {$endif FPC_HAS_TYPE_EXTENDED}
  320. {$ifdef FPC_HAS_TYPE_SINGLE}
  321. function minvalue(const data : array of Single) : Single;
  322. function minvalue(const data : PSingle; Const N : Integer) : Single;
  323. function maxvalue(const data : array of Single) : Single;
  324. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  325. {$endif FPC_HAS_TYPE_SINGLE}
  326. {$ifdef FPC_HAS_TYPE_DOUBLE}
  327. function minvalue(const data : array of Double) : Double;
  328. function minvalue(const data : PDouble; Const N : Integer) : Double;
  329. function maxvalue(const data : array of Double) : Double;
  330. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  331. {$endif FPC_HAS_TYPE_DOUBLE}
  332. {$ifdef FPC_HAS_TYPE_EXTENDED}
  333. function minvalue(const data : array of Extended) : Extended;
  334. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  335. function maxvalue(const data : array of Extended) : Extended;
  336. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  337. {$endif FPC_HAS_TYPE_EXTENDED}
  338. function minvalue(const data : array of integer) : Integer;
  339. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  340. function maxvalue(const data : array of integer) : Integer;
  341. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  342. { returns random values with gaussian distribution }
  343. function randg(mean,stddev : float) : float;
  344. function RandomRange(const aFrom, aTo: Integer): Integer;
  345. function RandomRange(const aFrom, aTo: Int64): Int64;
  346. {$ifdef FPC_HAS_TYPE_SINGLE}
  347. { calculates the standard deviation }
  348. function stddev(const data : array of Single) : float;
  349. function stddev(const data : PSingle; Const N : Integer) : float;
  350. { calculates the mean and stddev }
  351. procedure meanandstddev(const data : array of Single;
  352. var mean,stddev : float);
  353. procedure meanandstddev(const data : PSingle;
  354. Const N : Longint;var mean,stddev : float);
  355. function variance(const data : array of Single) : float;
  356. function totalvariance(const data : array of Single) : float;
  357. function variance(const data : PSingle; Const N : Integer) : float;
  358. function totalvariance(const data : PSingle; Const N : Integer) : float;
  359. { I don't know what the following functions do: }
  360. function popnstddev(const data : array of Single) : float;
  361. function popnstddev(const data : PSingle; Const N : Integer) : float;
  362. function popnvariance(const data : PSingle; Const N : Integer) : float;
  363. function popnvariance(const data : array of Single) : float;
  364. procedure momentskewkurtosis(const data : array of Single;
  365. out m1,m2,m3,m4,skew,kurtosis : float);
  366. procedure momentskewkurtosis(const data : PSingle; Const N : Integer;
  367. out m1,m2,m3,m4,skew,kurtosis : float);
  368. { geometrical function }
  369. { returns the euclidean L2 norm }
  370. function norm(const data : array of Single) : float;
  371. function norm(const data : PSingle; Const N : Integer) : float;
  372. {$endif FPC_HAS_TYPE_SINGLE}
  373. {$ifdef FPC_HAS_TYPE_DOUBLE}
  374. { calculates the standard deviation }
  375. function stddev(const data : array of Double) : float;
  376. function stddev(const data : PDouble; Const N : Integer) : float;
  377. { calculates the mean and stddev }
  378. procedure meanandstddev(const data : array of Double;
  379. var mean,stddev : float);
  380. procedure meanandstddev(const data : PDouble;
  381. Const N : Longint;var mean,stddev : float);
  382. function variance(const data : array of Double) : float;
  383. function totalvariance(const data : array of Double) : float;
  384. function variance(const data : PDouble; Const N : Integer) : float;
  385. function totalvariance(const data : PDouble; Const N : Integer) : float;
  386. { I don't know what the following functions do: }
  387. function popnstddev(const data : array of Double) : float;
  388. function popnstddev(const data : PDouble; Const N : Integer) : float;
  389. function popnvariance(const data : PDouble; Const N : Integer) : float;
  390. function popnvariance(const data : array of Double) : float;
  391. procedure momentskewkurtosis(const data : array of Double;
  392. out m1,m2,m3,m4,skew,kurtosis : float);
  393. procedure momentskewkurtosis(const data : PDouble; Const N : Integer;
  394. out m1,m2,m3,m4,skew,kurtosis : float);
  395. { geometrical function }
  396. { returns the euclidean L2 norm }
  397. function norm(const data : array of double) : float;
  398. function norm(const data : PDouble; Const N : Integer) : float;
  399. {$endif FPC_HAS_TYPE_DOUBLE}
  400. {$ifdef FPC_HAS_TYPE_EXTENDED}
  401. { calculates the standard deviation }
  402. function stddev(const data : array of Extended) : float;
  403. function stddev(const data : PExtended; Const N : Integer) : float;
  404. { calculates the mean and stddev }
  405. procedure meanandstddev(const data : array of Extended;
  406. var mean,stddev : float);
  407. procedure meanandstddev(const data : PExtended;
  408. Const N : Longint;var mean,stddev : float);
  409. function variance(const data : array of Extended) : float;
  410. function totalvariance(const data : array of Extended) : float;
  411. function variance(const data : PExtended; Const N : Integer) : float;
  412. function totalvariance(const data : PExtended; Const N : Integer) : float;
  413. { I don't know what the following functions do: }
  414. function popnstddev(const data : array of Extended) : float;
  415. function popnstddev(const data : PExtended; Const N : Integer) : float;
  416. function popnvariance(const data : PExtended; Const N : Integer) : float;
  417. function popnvariance(const data : array of Extended) : float;
  418. procedure momentskewkurtosis(const data : array of Extended;
  419. out m1,m2,m3,m4,skew,kurtosis : float);
  420. procedure momentskewkurtosis(const data : PExtended; Const N : Integer;
  421. out m1,m2,m3,m4,skew,kurtosis : float);
  422. { geometrical function }
  423. { returns the euclidean L2 norm }
  424. function norm(const data : array of Extended) : float;
  425. function norm(const data : PExtended; Const N : Integer) : float;
  426. {$endif FPC_HAS_TYPE_EXTENDED}
  427. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer; inline; overload;
  428. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64; inline; overload;
  429. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double; inline; overload;
  430. function CompareValue ( const A, B : Integer) : TValueRelationship; inline;
  431. function CompareValue ( const A, B : Int64) : TValueRelationship; inline;
  432. function CompareValue ( const A, B : QWord) : TValueRelationship; inline;
  433. {$ifdef FPC_HAS_TYPE_SINGLE}
  434. function CompareValue ( const A, B : Single; delta : Single = 0.0 ) : TValueRelationship; inline;
  435. {$endif}
  436. {$ifdef FPC_HAS_TYPE_DOUBLE}
  437. function CompareValue ( const A, B : Double; delta : Double = 0.0) : TValueRelationship; inline;
  438. {$endif}
  439. {$ifdef FPC_HAS_TYPE_EXTENDED}
  440. function CompareValue ( const A, B : Extended; delta : Extended = 0.0 ) : TValueRelationship; inline;
  441. {$endif}
  442. function RandomFrom(const AValues: array of Double): Double; overload;
  443. function RandomFrom(const AValues: array of Integer): Integer; overload;
  444. function RandomFrom(const AValues: array of Int64): Int64; overload;
  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. result:=1.0
  698. else if (base=0.0) and (exponent>0.0) then
  699. result:=0.0
  700. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  701. result:=intpower(base,trunc(exponent))
  702. else if base>0.0 then
  703. result:=exp(exponent * ln (base))
  704. else
  705. InvalidArgument;
  706. end;
  707. function intpower(base : float;const exponent : Integer) : float;
  708. var
  709. i : longint;
  710. begin
  711. if (base = 0.0) and (exponent = 0) then
  712. result:=1
  713. else
  714. begin
  715. i:=abs(exponent);
  716. intpower:=1.0;
  717. while i>0 do
  718. begin
  719. while (i and 1)=0 do
  720. begin
  721. i:=i shr 1;
  722. base:=sqr(base);
  723. end;
  724. i:=i-1;
  725. intpower:=intpower*base;
  726. end;
  727. if exponent<0 then
  728. intpower:=1.0/intpower;
  729. end;
  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. function RandomRange(const aFrom, aTo: Integer): Integer;
  965. begin
  966. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  967. end;
  968. function RandomRange(const aFrom, aTo: Int64): Int64;
  969. begin
  970. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  971. end;
  972. {$ifdef FPC_HAS_TYPE_SINGLE}
  973. function stddev(const data : array of Single) : float;
  974. begin
  975. Result:=Stddev(PSingle(@Data[0]),High(Data)+1)
  976. end;
  977. function stddev(const data : PSingle; Const N : Integer) : float;
  978. begin
  979. StdDev:=Sqrt(Variance(Data,N));
  980. end;
  981. procedure meanandstddev(const data : array of Single;
  982. var mean,stddev : float);
  983. begin
  984. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  985. end;
  986. procedure meanandstddev(const data : PSingle;
  987. Const N : Longint;var mean,stddev : float);
  988. Var I : longint;
  989. begin
  990. Mean:=0;
  991. StdDev:=0;
  992. For I:=0 to N-1 do
  993. begin
  994. Mean:=Mean+Data[i];
  995. StdDev:=StdDev+Sqr(Data[i]);
  996. end;
  997. Mean:=Mean/N;
  998. StdDev:=(StdDev-N*Sqr(Mean));
  999. If N>1 then
  1000. StdDev:=Sqrt(Stddev/(N-1))
  1001. else
  1002. StdDev:=0;
  1003. end;
  1004. function variance(const data : array of Single) : float;
  1005. begin
  1006. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  1007. end;
  1008. function variance(const data : PSingle; Const N : Integer) : float;
  1009. begin
  1010. If N=1 then
  1011. Result:=0
  1012. else
  1013. Result:=TotalVariance(Data,N)/(N-1);
  1014. end;
  1015. function totalvariance(const data : array of Single) : float;
  1016. begin
  1017. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  1018. end;
  1019. function totalvariance(const data : PSingle;Const N : Integer) : float;
  1020. var S,SS : Float;
  1021. begin
  1022. If N=1 then
  1023. Result:=0
  1024. else
  1025. begin
  1026. SumsAndSquares(Data,N,S,SS);
  1027. Result := SS-Sqr(S)/N;
  1028. end;
  1029. end;
  1030. function popnstddev(const data : array of Single) : float;
  1031. begin
  1032. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  1033. end;
  1034. function popnstddev(const data : PSingle; Const N : Integer) : float;
  1035. begin
  1036. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1037. end;
  1038. function popnvariance(const data : array of Single) : float;
  1039. begin
  1040. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  1041. end;
  1042. function popnvariance(const data : PSingle; Const N : Integer) : float;
  1043. begin
  1044. PopnVariance:=TotalVariance(Data,N)/N;
  1045. end;
  1046. procedure momentskewkurtosis(const data : array of single;
  1047. out m1,m2,m3,m4,skew,kurtosis : float);
  1048. begin
  1049. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1050. end;
  1051. procedure momentskewkurtosis(
  1052. const data: pSingle;
  1053. Const N: integer;
  1054. out m1: float;
  1055. out m2: float;
  1056. out m3: float;
  1057. out m4: float;
  1058. out skew: float;
  1059. out kurtosis: float
  1060. );
  1061. var
  1062. i: integer;
  1063. value : psingle;
  1064. deviation, deviation2: single;
  1065. reciprocalN: float;
  1066. begin
  1067. m1 := 0;
  1068. reciprocalN := 1/N;
  1069. value := data;
  1070. for i := 0 to N-1 do
  1071. begin
  1072. m1 := m1 + value^;
  1073. inc(value);
  1074. end;
  1075. m1 := reciprocalN * m1;
  1076. m2 := 0;
  1077. m3 := 0;
  1078. m4 := 0;
  1079. value := data;
  1080. for i := 0 to N-1 do
  1081. begin
  1082. deviation := (value^-m1);
  1083. deviation2 := deviation * deviation;
  1084. m2 := m2 + deviation2;
  1085. m3 := m3 + deviation2 * deviation;
  1086. m4 := m4 + deviation2 * deviation2;
  1087. inc(value);
  1088. end;
  1089. m2 := reciprocalN * m2;
  1090. m3 := reciprocalN * m3;
  1091. m4 := reciprocalN * m4;
  1092. skew := m3 / (sqrt(m2)*m2);
  1093. kurtosis := m4 / (m2 * m2);
  1094. end;
  1095. function norm(const data : array of Single) : float;
  1096. begin
  1097. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  1098. end;
  1099. function norm(const data : PSingle; Const N : Integer) : float;
  1100. begin
  1101. norm:=sqrt(sumofsquares(data,N));
  1102. end;
  1103. {$endif FPC_HAS_TYPE_SINGLE}
  1104. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1105. function stddev(const data : array of Double) : float;
  1106. begin
  1107. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  1108. end;
  1109. function stddev(const data : PDouble; Const N : Integer) : float;
  1110. begin
  1111. StdDev:=Sqrt(Variance(Data,N));
  1112. end;
  1113. procedure meanandstddev(const data : array of Double;
  1114. var mean,stddev : float);
  1115. begin
  1116. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  1117. end;
  1118. procedure meanandstddev(const data : PDouble;
  1119. Const N : Longint;var mean,stddev : float);
  1120. Var I : longint;
  1121. begin
  1122. Mean:=0;
  1123. StdDev:=0;
  1124. For I:=0 to N-1 do
  1125. begin
  1126. Mean:=Mean+Data[i];
  1127. StdDev:=StdDev+Sqr(Data[i]);
  1128. end;
  1129. Mean:=Mean/N;
  1130. StdDev:=(StdDev-N*Sqr(Mean));
  1131. If N>1 then
  1132. StdDev:=Sqrt(Stddev/(N-1))
  1133. else
  1134. StdDev:=0;
  1135. end;
  1136. function variance(const data : array of Double) : float;
  1137. begin
  1138. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  1139. end;
  1140. function variance(const data : PDouble; Const N : Integer) : float;
  1141. begin
  1142. If N=1 then
  1143. Result:=0
  1144. else
  1145. Result:=TotalVariance(Data,N)/(N-1);
  1146. end;
  1147. function totalvariance(const data : array of Double) : float;
  1148. begin
  1149. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  1150. end;
  1151. function totalvariance(const data : PDouble;Const N : Integer) : float;
  1152. var S,SS : Float;
  1153. begin
  1154. If N=1 then
  1155. Result:=0
  1156. else
  1157. begin
  1158. SumsAndSquares(Data,N,S,SS);
  1159. Result := SS-Sqr(S)/N;
  1160. end;
  1161. end;
  1162. function popnstddev(const data : array of Double) : float;
  1163. begin
  1164. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  1165. end;
  1166. function popnstddev(const data : PDouble; Const N : Integer) : float;
  1167. begin
  1168. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1169. end;
  1170. function popnvariance(const data : array of Double) : float;
  1171. begin
  1172. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  1173. end;
  1174. function popnvariance(const data : PDouble; Const N : Integer) : float;
  1175. begin
  1176. PopnVariance:=TotalVariance(Data,N)/N;
  1177. end;
  1178. procedure momentskewkurtosis(const data : array of Double;
  1179. out m1,m2,m3,m4,skew,kurtosis : float);
  1180. begin
  1181. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1182. end;
  1183. procedure momentskewkurtosis(
  1184. const data: pdouble;
  1185. Const N: integer;
  1186. out m1: float;
  1187. out m2: float;
  1188. out m3: float;
  1189. out m4: float;
  1190. out skew: float;
  1191. out kurtosis: float
  1192. );
  1193. var
  1194. i: integer;
  1195. value : pdouble;
  1196. deviation, deviation2: double;
  1197. reciprocalN: float;
  1198. begin
  1199. m1 := 0;
  1200. reciprocalN := 1/N;
  1201. value := data;
  1202. for i := 0 to N-1 do
  1203. begin
  1204. m1 := m1 + value^;
  1205. inc(value);
  1206. end;
  1207. m1 := reciprocalN * m1;
  1208. m2 := 0;
  1209. m3 := 0;
  1210. m4 := 0;
  1211. value := data;
  1212. for i := 0 to N-1 do
  1213. begin
  1214. deviation := (value^-m1);
  1215. deviation2 := deviation * deviation;
  1216. m2 := m2 + deviation2;
  1217. m3 := m3 + deviation2 * deviation;
  1218. m4 := m4 + deviation2 * deviation2;
  1219. inc(value);
  1220. end;
  1221. m2 := reciprocalN * m2;
  1222. m3 := reciprocalN * m3;
  1223. m4 := reciprocalN * m4;
  1224. skew := m3 / (sqrt(m2)*m2);
  1225. kurtosis := m4 / (m2 * m2);
  1226. end;
  1227. function norm(const data : array of Double) : float;
  1228. begin
  1229. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  1230. end;
  1231. function norm(const data : PDouble; Const N : Integer) : float;
  1232. begin
  1233. norm:=sqrt(sumofsquares(data,N));
  1234. end;
  1235. {$endif FPC_HAS_TYPE_DOUBLE}
  1236. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1237. function stddev(const data : array of Extended) : float;
  1238. begin
  1239. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  1240. end;
  1241. function stddev(const data : PExtended; Const N : Integer) : float;
  1242. begin
  1243. StdDev:=Sqrt(Variance(Data,N));
  1244. end;
  1245. procedure meanandstddev(const data : array of Extended;
  1246. var mean,stddev : float);
  1247. begin
  1248. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  1249. end;
  1250. procedure meanandstddev(const data : PExtended;
  1251. Const N : Longint;var mean,stddev : float);
  1252. Var I : longint;
  1253. begin
  1254. Mean:=0;
  1255. StdDev:=0;
  1256. For I:=0 to N-1 do
  1257. begin
  1258. Mean:=Mean+Data[i];
  1259. StdDev:=StdDev+Sqr(Data[i]);
  1260. end;
  1261. Mean:=Mean/N;
  1262. StdDev:=(StdDev-N*Sqr(Mean));
  1263. If N>1 then
  1264. StdDev:=Sqrt(Stddev/(N-1))
  1265. else
  1266. StdDev:=0;
  1267. end;
  1268. function variance(const data : array of Extended) : float;
  1269. begin
  1270. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  1271. end;
  1272. function variance(const data : PExtended; Const N : Integer) : float;
  1273. begin
  1274. If N=1 then
  1275. Result:=0
  1276. else
  1277. Result:=TotalVariance(Data,N)/(N-1);
  1278. end;
  1279. function totalvariance(const data : array of Extended) : float;
  1280. begin
  1281. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  1282. end;
  1283. function totalvariance(const data : PExtended;Const N : Integer) : float;
  1284. var S,SS : Float;
  1285. begin
  1286. If N=1 then
  1287. Result:=0
  1288. else
  1289. begin
  1290. SumsAndSquares(Data,N,S,SS);
  1291. Result := SS-Sqr(S)/N;
  1292. end;
  1293. end;
  1294. function popnstddev(const data : array of Extended) : float;
  1295. begin
  1296. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  1297. end;
  1298. function popnstddev(const data : PExtended; Const N : Integer) : float;
  1299. begin
  1300. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1301. end;
  1302. function popnvariance(const data : array of Extended) : float;
  1303. begin
  1304. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  1305. end;
  1306. function popnvariance(const data : PExtended; Const N : Integer) : float;
  1307. begin
  1308. PopnVariance:=TotalVariance(Data,N)/N;
  1309. end;
  1310. procedure momentskewkurtosis(const data : array of Extended;
  1311. out m1,m2,m3,m4,skew,kurtosis : float);
  1312. begin
  1313. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1314. end;
  1315. procedure momentskewkurtosis(
  1316. const data: pExtended;
  1317. Const N: integer;
  1318. out m1: float;
  1319. out m2: float;
  1320. out m3: float;
  1321. out m4: float;
  1322. out skew: float;
  1323. out kurtosis: float
  1324. );
  1325. var
  1326. i: integer;
  1327. value : pextended;
  1328. deviation, deviation2: extended;
  1329. reciprocalN: float;
  1330. begin
  1331. m1 := 0;
  1332. reciprocalN := 1/N;
  1333. value := data;
  1334. for i := 0 to N-1 do
  1335. begin
  1336. m1 := m1 + value^;
  1337. inc(value);
  1338. end;
  1339. m1 := reciprocalN * m1;
  1340. m2 := 0;
  1341. m3 := 0;
  1342. m4 := 0;
  1343. value := data;
  1344. for i := 0 to N-1 do
  1345. begin
  1346. deviation := (value^-m1);
  1347. deviation2 := deviation * deviation;
  1348. m2 := m2 + deviation2;
  1349. m3 := m3 + deviation2 * deviation;
  1350. m4 := m4 + deviation2 * deviation2;
  1351. inc(value);
  1352. end;
  1353. m2 := reciprocalN * m2;
  1354. m3 := reciprocalN * m3;
  1355. m4 := reciprocalN * m4;
  1356. skew := m3 / (sqrt(m2)*m2);
  1357. kurtosis := m4 / (m2 * m2);
  1358. end;
  1359. function norm(const data : array of Extended) : float;
  1360. begin
  1361. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  1362. end;
  1363. function norm(const data : PExtended; Const N : Integer) : float;
  1364. begin
  1365. norm:=sqrt(sumofsquares(data,N));
  1366. end;
  1367. {$endif FPC_HAS_TYPE_EXTENDED}
  1368. function MinIntValue(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 MaxIntValue(const Data: array of Integer): Integer;
  1377. var
  1378. I: Integer;
  1379. begin
  1380. Result := Data[Low(Data)];
  1381. For I := Succ(Low(Data)) To High(Data) Do
  1382. If Data[I] > Result Then Result := Data[I];
  1383. end;
  1384. function MinValue(const Data: array of Integer): Integer;
  1385. begin
  1386. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  1387. end;
  1388. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  1389. var
  1390. I: Integer;
  1391. begin
  1392. Result := Data[0];
  1393. For I := 1 To N-1 do
  1394. If Data[I] < Result Then Result := Data[I];
  1395. end;
  1396. function MaxValue(const Data: array of Integer): Integer;
  1397. begin
  1398. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  1399. end;
  1400. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  1401. var
  1402. i : longint;
  1403. begin
  1404. { get an initial value }
  1405. maxvalue:=data[0];
  1406. for i:=1 to N-1 do
  1407. if data[i]>maxvalue then
  1408. maxvalue:=data[i];
  1409. end;
  1410. {$ifdef FPC_HAS_TYPE_SINGLE}
  1411. function minvalue(const data : array of Single) : Single;
  1412. begin
  1413. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  1414. end;
  1415. function minvalue(const data : PSingle; Const N : Integer) : Single;
  1416. var
  1417. i : longint;
  1418. begin
  1419. { get an initial value }
  1420. minvalue:=data[0];
  1421. for i:=1 to N-1 do
  1422. if data[i]<minvalue then
  1423. minvalue:=data[i];
  1424. end;
  1425. function maxvalue(const data : array of Single) : Single;
  1426. begin
  1427. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  1428. end;
  1429. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  1430. var
  1431. i : longint;
  1432. begin
  1433. { get an initial value }
  1434. maxvalue:=data[0];
  1435. for i:=1 to N-1 do
  1436. if data[i]>maxvalue then
  1437. maxvalue:=data[i];
  1438. end;
  1439. {$endif FPC_HAS_TYPE_SINGLE}
  1440. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1441. function minvalue(const data : array of Double) : Double;
  1442. begin
  1443. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  1444. end;
  1445. function minvalue(const data : PDouble; Const N : Integer) : Double;
  1446. var
  1447. i : longint;
  1448. begin
  1449. { get an initial value }
  1450. minvalue:=data[0];
  1451. for i:=1 to N-1 do
  1452. if data[i]<minvalue then
  1453. minvalue:=data[i];
  1454. end;
  1455. function maxvalue(const data : array of Double) : Double;
  1456. begin
  1457. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  1458. end;
  1459. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  1460. var
  1461. i : longint;
  1462. begin
  1463. { get an initial value }
  1464. maxvalue:=data[0];
  1465. for i:=1 to N-1 do
  1466. if data[i]>maxvalue then
  1467. maxvalue:=data[i];
  1468. end;
  1469. {$endif FPC_HAS_TYPE_DOUBLE}
  1470. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1471. function minvalue(const data : array of Extended) : Extended;
  1472. begin
  1473. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  1474. end;
  1475. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  1476. var
  1477. i : longint;
  1478. begin
  1479. { get an initial value }
  1480. minvalue:=data[0];
  1481. for i:=1 to N-1 do
  1482. if data[i]<minvalue then
  1483. minvalue:=data[i];
  1484. end;
  1485. function maxvalue(const data : array of Extended) : Extended;
  1486. begin
  1487. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  1488. end;
  1489. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  1490. var
  1491. i : longint;
  1492. begin
  1493. { get an initial value }
  1494. maxvalue:=data[0];
  1495. for i:=1 to N-1 do
  1496. if data[i]>maxvalue then
  1497. maxvalue:=data[i];
  1498. end;
  1499. {$endif FPC_HAS_TYPE_EXTENDED}
  1500. function Min(a, b: Integer): Integer;inline;
  1501. begin
  1502. if a < b then
  1503. Result := a
  1504. else
  1505. Result := b;
  1506. end;
  1507. function Max(a, b: Integer): Integer;inline;
  1508. begin
  1509. if a > b then
  1510. Result := a
  1511. else
  1512. Result := b;
  1513. end;
  1514. {
  1515. function Min(a, b: Cardinal): Cardinal;inline;
  1516. begin
  1517. if a < b then
  1518. Result := a
  1519. else
  1520. Result := b;
  1521. end;
  1522. function Max(a, b: Cardinal): Cardinal;inline;
  1523. begin
  1524. if a > b then
  1525. Result := a
  1526. else
  1527. Result := b;
  1528. end;
  1529. }
  1530. function Min(a, b: Int64): Int64;inline;
  1531. begin
  1532. if a < b then
  1533. Result := a
  1534. else
  1535. Result := b;
  1536. end;
  1537. function Max(a, b: Int64): Int64;inline;
  1538. begin
  1539. if a > b then
  1540. Result := a
  1541. else
  1542. Result := b;
  1543. end;
  1544. {$ifdef FPC_HAS_TYPE_SINGLE}
  1545. function Min(a, b: Single): Single;inline;
  1546. begin
  1547. if a < b then
  1548. Result := a
  1549. else
  1550. Result := b;
  1551. end;
  1552. function Max(a, b: Single): Single;inline;
  1553. begin
  1554. if a > b then
  1555. Result := a
  1556. else
  1557. Result := b;
  1558. end;
  1559. {$endif FPC_HAS_TYPE_SINGLE}
  1560. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1561. function Min(a, b: Double): Double;inline;
  1562. begin
  1563. if a < b then
  1564. Result := a
  1565. else
  1566. Result := b;
  1567. end;
  1568. function Max(a, b: Double): Double;inline;
  1569. begin
  1570. if a > b then
  1571. Result := a
  1572. else
  1573. Result := b;
  1574. end;
  1575. {$endif FPC_HAS_TYPE_DOUBLE}
  1576. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1577. function Min(a, b: Extended): Extended;inline;
  1578. begin
  1579. if a < b then
  1580. Result := a
  1581. else
  1582. Result := b;
  1583. end;
  1584. function Max(a, b: Extended): Extended;inline;
  1585. begin
  1586. if a > b then
  1587. Result := a
  1588. else
  1589. Result := b;
  1590. end;
  1591. {$endif FPC_HAS_TYPE_EXTENDED}
  1592. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  1593. begin
  1594. Result:=(AValue>=AMin) and (AValue<=AMax);
  1595. end;
  1596. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  1597. begin
  1598. Result:=(AValue>=AMin) and (AValue<=AMax);
  1599. end;
  1600. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1601. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  1602. begin
  1603. Result:=(AValue>=AMin) and (AValue<=AMax);
  1604. end;
  1605. {$endif FPC_HAS_TYPE_DOUBLE}
  1606. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;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. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  1615. begin
  1616. Result:=AValue;
  1617. If Result<AMin then
  1618. Result:=AMin
  1619. else if Result>AMax then
  1620. Result:=AMax;
  1621. end;
  1622. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1623. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  1624. begin
  1625. Result:=AValue;
  1626. If Result<AMin then
  1627. Result:=AMin
  1628. else if Result>AMax then
  1629. Result:=AMax;
  1630. end;
  1631. {$endif FPC_HAS_TYPE_DOUBLE}
  1632. Const
  1633. EZeroResolution = 1E-16;
  1634. DZeroResolution = 1E-12;
  1635. SZeroResolution = 1E-4;
  1636. function IsZero(const A: Single; Epsilon: Single): Boolean;
  1637. begin
  1638. if (Epsilon=0) then
  1639. Epsilon:=SZeroResolution;
  1640. Result:=Abs(A)<=Epsilon;
  1641. end;
  1642. function IsZero(const A: Single): Boolean;inline;
  1643. begin
  1644. Result:=IsZero(A,single(SZeroResolution));
  1645. end;
  1646. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1647. function IsZero(const A: Double; Epsilon: Double): Boolean;
  1648. begin
  1649. if (Epsilon=0) then
  1650. Epsilon:=DZeroResolution;
  1651. Result:=Abs(A)<=Epsilon;
  1652. end;
  1653. function IsZero(const A: Double): Boolean;inline;
  1654. begin
  1655. Result:=IsZero(A,DZeroResolution);
  1656. end;
  1657. {$endif FPC_HAS_TYPE_DOUBLE}
  1658. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1659. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  1660. begin
  1661. if (Epsilon=0) then
  1662. Epsilon:=EZeroResolution;
  1663. Result:=Abs(A)<=Epsilon;
  1664. end;
  1665. function IsZero(const A: Extended): Boolean;inline;
  1666. begin
  1667. Result:=IsZero(A,EZeroResolution);
  1668. end;
  1669. {$endif FPC_HAS_TYPE_EXTENDED}
  1670. type
  1671. TSplitDouble = packed record
  1672. cards: Array[0..1] of cardinal;
  1673. end;
  1674. function IsNan(const d : Single): Boolean; overload;
  1675. type
  1676. TSplitSingle = packed record
  1677. case byte of
  1678. 0: (bytes: Array[0..3] of byte);
  1679. 1: (words: Array[0..1] of word);
  1680. 2: (cards: Array[0..0] of cardinal);
  1681. end;
  1682. var
  1683. fraczero, expMaximal: boolean;
  1684. begin
  1685. {$ifdef FPC_BIG_ENDIAN}
  1686. expMaximal := ((TSplitSingle(d).words[0] shr 7) and $ff) = 255;
  1687. fraczero:= (TSplitSingle(d).cards[0] and $7fffff = 0);
  1688. {$else FPC_BIG_ENDIAN}
  1689. expMaximal := ((TSplitSingle(d).words[1] shr 7) and $ff) = 255;
  1690. fraczero := (TSplitSingle(d).cards[0] and $7fffff = 0);
  1691. {$endif FPC_BIG_ENDIAN}
  1692. Result:=expMaximal and not(fraczero);
  1693. end;
  1694. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1695. function IsNan(const d : Double): Boolean;
  1696. var
  1697. fraczero, expMaximal: boolean;
  1698. begin
  1699. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1700. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1701. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1702. (TSplitDouble(d).cards[1] = 0);
  1703. {$else FPC_BIG_ENDIAN}
  1704. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1705. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1706. (TSplitDouble(d).cards[0] = 0);
  1707. {$endif FPC_BIG_ENDIAN}
  1708. Result:=expMaximal and not(fraczero);
  1709. end;
  1710. {$endif FPC_HAS_TYPE_DOUBLE}
  1711. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1712. function IsNan(const d : Extended): Boolean; overload;
  1713. type
  1714. TSplitExtended = packed record
  1715. case byte of
  1716. 0: (bytes: Array[0..9] of byte);
  1717. 1: (words: Array[0..4] of word);
  1718. 2: (cards: Array[0..1] of cardinal; w: word);
  1719. end;
  1720. var
  1721. fraczero, expMaximal: boolean;
  1722. begin
  1723. {$ifdef FPC_BIG_ENDIAN}
  1724. {$error no support for big endian extended type yet}
  1725. {$else FPC_BIG_ENDIAN}
  1726. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  1727. fraczero := (TSplitExtended(d).cards[0] = 0) and
  1728. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  1729. {$endif FPC_BIG_ENDIAN}
  1730. Result:=expMaximal and not(fraczero);
  1731. end;
  1732. {$endif FPC_HAS_TYPE_EXTENDED}
  1733. function IsInfinite(const d : Double): Boolean;
  1734. var
  1735. fraczero, expMaximal: boolean;
  1736. begin
  1737. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1738. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1739. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1740. (TSplitDouble(d).cards[1] = 0);
  1741. {$else FPC_BIG_ENDIAN}
  1742. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1743. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1744. (TSplitDouble(d).cards[0] = 0);
  1745. {$endif FPC_BIG_ENDIAN}
  1746. Result:=expMaximal and fraczero;
  1747. end;
  1748. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1749. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1750. begin
  1751. if (Epsilon=0) then
  1752. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1753. if (A>B) then
  1754. Result:=((A-B)<=Epsilon)
  1755. else
  1756. Result:=((B-A)<=Epsilon);
  1757. end;
  1758. function SameValue(const A, B: Extended): Boolean;inline;
  1759. begin
  1760. Result:=SameValue(A,B,0);
  1761. end;
  1762. {$endif FPC_HAS_TYPE_EXTENDED}
  1763. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1764. function SameValue(const A, B: Double): Boolean;inline;
  1765. begin
  1766. Result:=SameValue(A,B,0);
  1767. end;
  1768. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1769. begin
  1770. if (Epsilon=0) then
  1771. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1772. if (A>B) then
  1773. Result:=((A-B)<=Epsilon)
  1774. else
  1775. Result:=((B-A)<=Epsilon);
  1776. end;
  1777. {$endif FPC_HAS_TYPE_DOUBLE}
  1778. function SameValue(const A, B: Single): Boolean;inline;
  1779. begin
  1780. Result:=SameValue(A,B,0);
  1781. end;
  1782. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  1783. begin
  1784. if (Epsilon=0) then
  1785. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  1786. if (A>B) then
  1787. Result:=((A-B)<=Epsilon)
  1788. else
  1789. Result:=((B-A)<=Epsilon);
  1790. end;
  1791. // Some CPUs probably allow a faster way of doing this in a single operation...
  1792. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  1793. {$ifndef FPC_MATH_HAS_DIVMOD}
  1794. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  1795. begin
  1796. if Dividend < 0 then
  1797. begin
  1798. { Use DivMod with >=0 dividend }
  1799. Dividend:=-Dividend;
  1800. { The documented behavior of Pascal's div/mod operators and DivMod
  1801. on negative dividends is to return Result closer to zero and
  1802. a negative Remainder. Which means that we can just negate both
  1803. Result and Remainder, and all it's Ok. }
  1804. Result:=-(Dividend Div Divisor);
  1805. Remainder:=-(Dividend+(Result*Divisor));
  1806. end
  1807. else
  1808. begin
  1809. Result:=Dividend Div Divisor;
  1810. Remainder:=Dividend-(Result*Divisor);
  1811. end;
  1812. end;
  1813. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: SmallInt);
  1814. begin
  1815. if Dividend < 0 then
  1816. begin
  1817. { Use DivMod with >=0 dividend }
  1818. Dividend:=-Dividend;
  1819. { The documented behavior of Pascal's div/mod operators and DivMod
  1820. on negative dividends is to return Result closer to zero and
  1821. a negative Remainder. Which means that we can just negate both
  1822. Result and Remainder, and all it's Ok. }
  1823. Result:=-(Dividend Div Divisor);
  1824. Remainder:=-(Dividend+(Result*Divisor));
  1825. end
  1826. else
  1827. begin
  1828. Result:=Dividend Div Divisor;
  1829. Remainder:=Dividend-(Result*Divisor);
  1830. end;
  1831. end;
  1832. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  1833. begin
  1834. Result:=Dividend Div Divisor;
  1835. Remainder:=Dividend-(Result*Divisor);
  1836. end;
  1837. procedure DivMod(Dividend: Integer; Divisor: Integer; var Result, Remainder: Integer);
  1838. begin
  1839. if Dividend < 0 then
  1840. begin
  1841. { Use DivMod with >=0 dividend }
  1842. Dividend:=-Dividend;
  1843. { The documented behavior of Pascal's div/mod operators and DivMod
  1844. on negative dividends is to return Result closer to zero and
  1845. a negative Remainder. Which means that we can just negate both
  1846. Result and Remainder, and all it's Ok. }
  1847. Result:=-(Dividend Div Divisor);
  1848. Remainder:=-(Dividend+(Result*Divisor));
  1849. end
  1850. else
  1851. begin
  1852. Result:=Dividend Div Divisor;
  1853. Remainder:=Dividend-(Result*Divisor);
  1854. end;
  1855. end;
  1856. {$endif FPC_MATH_HAS_DIVMOD}
  1857. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  1858. begin
  1859. if val then result:=iftrue else result:=iffalse;
  1860. end;
  1861. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  1862. begin
  1863. if val then result:=iftrue else result:=iffalse;
  1864. end;
  1865. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  1866. begin
  1867. if val then result:=iftrue else result:=iffalse;
  1868. end;
  1869. // dilemma here. asm can do the two comparisons in one go?
  1870. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  1871. function CompareValue(const A, B : Integer): TValueRelationship;
  1872. begin
  1873. result:=GreaterThanValue;
  1874. if a=b then
  1875. result:=EqualsValue
  1876. else
  1877. if a<b then
  1878. result:=LessThanValue;
  1879. end;
  1880. function CompareValue(const A, B: Int64): TValueRelationship;
  1881. begin
  1882. result:=GreaterThanValue;
  1883. if a=b then
  1884. result:=EqualsValue
  1885. else
  1886. if a<b then
  1887. result:=LessThanValue;
  1888. end;
  1889. function CompareValue(const A, B: QWord): TValueRelationship;
  1890. begin
  1891. result:=GreaterThanValue;
  1892. if a=b then
  1893. result:=EqualsValue
  1894. else
  1895. if a<b then
  1896. result:=LessThanValue;
  1897. end;
  1898. {$ifdef FPC_HAS_TYPE_SINGLE}
  1899. function CompareValue(const A, B: Single; delta: Single = 0.0): TValueRelationship;
  1900. begin
  1901. result:=GreaterThanValue;
  1902. if abs(a-b)<=delta then
  1903. result:=EqualsValue
  1904. else
  1905. if a<b then
  1906. result:=LessThanValue;
  1907. end;
  1908. {$endif}
  1909. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1910. function CompareValue(const A, B: Double; delta: Double = 0.0): TValueRelationship;
  1911. begin
  1912. result:=GreaterThanValue;
  1913. if abs(a-b)<=delta then
  1914. result:=EqualsValue
  1915. else
  1916. if a<b then
  1917. result:=LessThanValue;
  1918. end;
  1919. {$endif}
  1920. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1921. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  1922. begin
  1923. result:=GreaterThanValue;
  1924. if abs(a-b)<=delta then
  1925. result:=EqualsValue
  1926. else
  1927. if a<b then
  1928. result:=LessThanValue;
  1929. end;
  1930. {$endif}
  1931. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1932. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  1933. var
  1934. RV : Double;
  1935. begin
  1936. RV:=IntPower(10,Digits);
  1937. Result:=Round(AValue/RV)*RV;
  1938. end;
  1939. {$endif}
  1940. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1941. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  1942. var
  1943. RV : Extended;
  1944. begin
  1945. RV:=IntPower(10,Digits);
  1946. Result:=Round(AValue/RV)*RV;
  1947. end;
  1948. {$endif}
  1949. {$ifdef FPC_HAS_TYPE_SINGLE}
  1950. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  1951. var
  1952. RV : Single;
  1953. begin
  1954. RV:=IntPower(10,Digits);
  1955. Result:=Round(AValue/RV)*RV;
  1956. end;
  1957. {$endif}
  1958. {$ifdef FPC_HAS_TYPE_SINGLE}
  1959. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  1960. var
  1961. RV : Single;
  1962. begin
  1963. RV := IntPower(10, -Digits);
  1964. if AValue < 0 then
  1965. Result := Trunc((AValue*RV) - 0.5)/RV
  1966. else
  1967. Result := Trunc((AValue*RV) + 0.5)/RV;
  1968. end;
  1969. {$endif}
  1970. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1971. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  1972. var
  1973. RV : Double;
  1974. begin
  1975. RV := IntPower(10, -Digits);
  1976. if AValue < 0 then
  1977. Result := Trunc((AValue*RV) - 0.5)/RV
  1978. else
  1979. Result := Trunc((AValue*RV) + 0.5)/RV;
  1980. end;
  1981. {$endif}
  1982. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1983. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  1984. var
  1985. RV : Extended;
  1986. begin
  1987. RV := IntPower(10, -Digits);
  1988. if AValue < 0 then
  1989. Result := Trunc((AValue*RV) - 0.5)/RV
  1990. else
  1991. Result := Trunc((AValue*RV) + 0.5)/RV;
  1992. end;
  1993. {$endif}
  1994. function RandomFrom(const AValues: array of Double): Double; overload;
  1995. begin
  1996. result:=AValues[random(High(AValues)+1)];
  1997. end;
  1998. function RandomFrom(const AValues: array of Integer): Integer; overload;
  1999. begin
  2000. result:=AValues[random(High(AValues)+1)];
  2001. end;
  2002. function RandomFrom(const AValues: array of Int64): Int64; overload;
  2003. begin
  2004. result:=AValues[random(High(AValues)+1)];
  2005. end;
  2006. {$else}
  2007. implementation
  2008. {$endif FPUNONE}
  2009. end.