math.pp 57 KB

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