math.pp 59 KB

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