math.pp 62 KB

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