math.pp 59 KB

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