math.pp 60 KB

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