math.pp 58 KB

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