math.pp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  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. lnxp1:=ln(y)+(x-(y-1.0))/y;
  788. end;
  789. end;
  790. function power(base,exponent : float) : float;
  791. begin
  792. if Exponent=0.0 then
  793. result:=1.0
  794. else if (base=0.0) and (exponent>0.0) then
  795. result:=0.0
  796. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  797. result:=intpower(base,trunc(exponent))
  798. else
  799. result:=exp(exponent * ln (base));
  800. end;
  801. function intpower(base : float;const exponent : Integer) : float;
  802. var
  803. i : longint;
  804. begin
  805. if (base = 0.0) and (exponent = 0) then
  806. result:=1
  807. else
  808. begin
  809. i:=abs(exponent);
  810. intpower:=1.0;
  811. while i>0 do
  812. begin
  813. while (i and 1)=0 do
  814. begin
  815. i:=i shr 1;
  816. base:=sqr(base);
  817. end;
  818. i:=i-1;
  819. intpower:=intpower*base;
  820. end;
  821. if exponent<0 then
  822. intpower:=1.0/intpower;
  823. end;
  824. end;
  825. operator ** (bas,expo : float) e: float; inline;
  826. begin
  827. e:=power(bas,expo);
  828. end;
  829. operator ** (bas,expo : int64) i: int64; inline;
  830. begin
  831. i:=round(intpower(bas,expo));
  832. end;
  833. function ceil(x : float) : integer;
  834. begin
  835. Ceil:=Trunc(x);
  836. If Frac(x)>0 then
  837. Ceil:=Ceil+1;
  838. end;
  839. function floor(x : float) : integer;
  840. begin
  841. Floor:=Trunc(x);
  842. If Frac(x)<0 then
  843. Floor := Floor-1;
  844. end;
  845. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  846. begin
  847. Exponent:=0;
  848. if (X<>0) then
  849. if (abs(X)<0.5) then
  850. repeat
  851. X:=X*2;
  852. Dec(Exponent);
  853. until (abs(X)>=0.5)
  854. else
  855. while (abs(X)>=1) do
  856. begin
  857. X:=X/2;
  858. Inc(Exponent);
  859. end;
  860. Mantissa:=X;
  861. end;
  862. function ldexp(x : float;const p : Integer) : float;
  863. begin
  864. ldexp:=x*intpower(2.0,p);
  865. end;
  866. {$ifdef FPC_HAS_TYPE_SINGLE}
  867. function mean(const data : array of Single) : float;
  868. begin
  869. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  870. end;
  871. function mean(const data : PSingle; Const N : longint) : float;
  872. begin
  873. mean:=sum(Data,N);
  874. mean:=mean/N;
  875. end;
  876. function sum(const data : array of Single) : float;inline;
  877. begin
  878. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  879. end;
  880. function sum(const data : PSingle;Const N : longint) : float;
  881. var
  882. i : longint;
  883. begin
  884. sum:=0.0;
  885. for i:=0 to N-1 do
  886. sum:=sum+data[i];
  887. end;
  888. {$endif FPC_HAS_TYPE_SINGLE}
  889. {$ifdef FPC_HAS_TYPE_DOUBLE}
  890. function mean(const data : array of Double) : float; inline;
  891. begin
  892. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  893. end;
  894. function mean(const data : PDouble; Const N : longint) : float;
  895. begin
  896. mean:=sum(Data,N);
  897. mean:=mean/N;
  898. end;
  899. function sum(const data : array of Double) : float; inline;
  900. begin
  901. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  902. end;
  903. function sum(const data : PDouble;Const N : longint) : float;
  904. var
  905. i : longint;
  906. begin
  907. sum:=0.0;
  908. for i:=0 to N-1 do
  909. sum:=sum+data[i];
  910. end;
  911. {$endif FPC_HAS_TYPE_DOUBLE}
  912. {$ifdef FPC_HAS_TYPE_EXTENDED}
  913. function mean(const data : array of Extended) : float;
  914. begin
  915. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  916. end;
  917. function mean(const data : PExtended; Const N : longint) : float;
  918. begin
  919. mean:=sum(Data,N);
  920. mean:=mean/N;
  921. end;
  922. function sum(const data : array of Extended) : float; inline;
  923. begin
  924. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  925. end;
  926. function sum(const data : PExtended;Const N : longint) : float;
  927. var
  928. i : longint;
  929. begin
  930. sum:=0.0;
  931. for i:=0 to N-1 do
  932. sum:=sum+data[i];
  933. end;
  934. {$endif FPC_HAS_TYPE_EXTENDED}
  935. function sumInt(const data : PInt64;Const N : longint) : Int64;
  936. var
  937. i : longint;
  938. begin
  939. sumInt:=0;
  940. for i:=0 to N-1 do
  941. sumInt:=sumInt+data[i];
  942. end;
  943. function sumInt(const data : array of Int64) : Int64; inline;
  944. begin
  945. Result:=SumInt(@Data[0],High(Data)+1);
  946. end;
  947. {$ifdef FPC_HAS_TYPE_SINGLE}
  948. function sumofsquares(const data : array of Single) : float; inline;
  949. begin
  950. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  951. end;
  952. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  953. var
  954. i : longint;
  955. begin
  956. sumofsquares:=0.0;
  957. for i:=0 to N-1 do
  958. sumofsquares:=sumofsquares+sqr(data[i]);
  959. end;
  960. procedure sumsandsquares(const data : array of Single;
  961. var sum,sumofsquares : float); inline;
  962. begin
  963. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  964. end;
  965. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  966. var sum,sumofsquares : float);
  967. var
  968. i : Integer;
  969. temp : float;
  970. begin
  971. sumofsquares:=0.0;
  972. sum:=0.0;
  973. for i:=0 to N-1 do
  974. begin
  975. temp:=data[i];
  976. sumofsquares:=sumofsquares+sqr(temp);
  977. sum:=sum+temp;
  978. end;
  979. end;
  980. {$endif FPC_HAS_TYPE_SINGLE}
  981. {$ifdef FPC_HAS_TYPE_DOUBLE}
  982. function sumofsquares(const data : array of Double) : float; inline;
  983. begin
  984. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  985. end;
  986. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  987. var
  988. i : longint;
  989. begin
  990. sumofsquares:=0.0;
  991. for i:=0 to N-1 do
  992. sumofsquares:=sumofsquares+sqr(data[i]);
  993. end;
  994. procedure sumsandsquares(const data : array of Double;
  995. var sum,sumofsquares : float);
  996. begin
  997. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  998. end;
  999. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  1000. var sum,sumofsquares : float);
  1001. var
  1002. i : Integer;
  1003. temp : float;
  1004. begin
  1005. sumofsquares:=0.0;
  1006. sum:=0.0;
  1007. for i:=0 to N-1 do
  1008. begin
  1009. temp:=data[i];
  1010. sumofsquares:=sumofsquares+sqr(temp);
  1011. sum:=sum+temp;
  1012. end;
  1013. end;
  1014. {$endif FPC_HAS_TYPE_DOUBLE}
  1015. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1016. function sumofsquares(const data : array of Extended) : float; inline;
  1017. begin
  1018. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  1019. end;
  1020. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  1021. var
  1022. i : longint;
  1023. begin
  1024. sumofsquares:=0.0;
  1025. for i:=0 to N-1 do
  1026. sumofsquares:=sumofsquares+sqr(data[i]);
  1027. end;
  1028. procedure sumsandsquares(const data : array of Extended;
  1029. var sum,sumofsquares : float); inline;
  1030. begin
  1031. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1032. end;
  1033. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  1034. var sum,sumofsquares : float);
  1035. var
  1036. i : Integer;
  1037. temp : float;
  1038. begin
  1039. sumofsquares:=0.0;
  1040. sum:=0.0;
  1041. for i:=0 to N-1 do
  1042. begin
  1043. temp:=data[i];
  1044. sumofsquares:=sumofsquares+sqr(temp);
  1045. sum:=sum+temp;
  1046. end;
  1047. end;
  1048. {$endif FPC_HAS_TYPE_EXTENDED}
  1049. function randg(mean,stddev : float) : float;
  1050. Var U1,S2 : Float;
  1051. begin
  1052. repeat
  1053. u1:= 2*random-1;
  1054. S2:=Sqr(U1)+sqr(2*random-1);
  1055. until s2<1;
  1056. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  1057. end;
  1058. function RandomRange(const aFrom, aTo: Integer): Integer;
  1059. begin
  1060. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1061. end;
  1062. function RandomRange(const aFrom, aTo: Int64): Int64;
  1063. begin
  1064. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1065. end;
  1066. {$ifdef FPC_HAS_TYPE_SINGLE}
  1067. function stddev(const data : array of Single) : float; inline;
  1068. begin
  1069. Result:=Stddev(PSingle(@Data[0]),High(Data)+1);
  1070. end;
  1071. function stddev(const data : PSingle; Const N : Integer) : float;
  1072. begin
  1073. StdDev:=Sqrt(Variance(Data,N));
  1074. end;
  1075. procedure meanandstddev(const data : array of Single;
  1076. var mean,stddev : float); inline;
  1077. begin
  1078. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  1079. end;
  1080. procedure meanandstddev(const data : PSingle;
  1081. Const N : Longint;var mean,stddev : float);
  1082. Var I : longint;
  1083. begin
  1084. Mean:=0;
  1085. StdDev:=0;
  1086. For I:=0 to N-1 do
  1087. begin
  1088. Mean:=Mean+Data[i];
  1089. StdDev:=StdDev+Sqr(Data[i]);
  1090. end;
  1091. Mean:=Mean/N;
  1092. StdDev:=(StdDev-N*Sqr(Mean));
  1093. If N>1 then
  1094. StdDev:=Sqrt(Stddev/(N-1))
  1095. else
  1096. StdDev:=0;
  1097. end;
  1098. function variance(const data : array of Single) : float; inline;
  1099. begin
  1100. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  1101. end;
  1102. function variance(const data : PSingle; Const N : Integer) : float;
  1103. begin
  1104. If N=1 then
  1105. Result:=0
  1106. else
  1107. Result:=TotalVariance(Data,N)/(N-1);
  1108. end;
  1109. function totalvariance(const data : array of Single) : float; inline;
  1110. begin
  1111. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  1112. end;
  1113. function totalvariance(const data : PSingle;Const N : Integer) : float;
  1114. var S,SS : Float;
  1115. begin
  1116. If N=1 then
  1117. Result:=0
  1118. else
  1119. begin
  1120. SumsAndSquares(Data,N,S,SS);
  1121. Result := SS-Sqr(S)/N;
  1122. end;
  1123. end;
  1124. function popnstddev(const data : array of Single) : float;
  1125. begin
  1126. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  1127. end;
  1128. function popnstddev(const data : PSingle; Const N : Integer) : float;
  1129. begin
  1130. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1131. end;
  1132. function popnvariance(const data : array of Single) : float; inline;
  1133. begin
  1134. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  1135. end;
  1136. function popnvariance(const data : PSingle; Const N : Integer) : float;
  1137. begin
  1138. PopnVariance:=TotalVariance(Data,N)/N;
  1139. end;
  1140. procedure momentskewkurtosis(const data : array of single;
  1141. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1142. begin
  1143. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1144. end;
  1145. procedure momentskewkurtosis(
  1146. const data: pSingle;
  1147. Const N: integer;
  1148. out m1: float;
  1149. out m2: float;
  1150. out m3: float;
  1151. out m4: float;
  1152. out skew: float;
  1153. out kurtosis: float
  1154. );
  1155. var
  1156. i: integer;
  1157. value : psingle;
  1158. deviation, deviation2: single;
  1159. reciprocalN: float;
  1160. begin
  1161. m1 := 0;
  1162. reciprocalN := 1/N;
  1163. value := data;
  1164. for i := 0 to N-1 do
  1165. begin
  1166. m1 := m1 + value^;
  1167. inc(value);
  1168. end;
  1169. m1 := reciprocalN * m1;
  1170. m2 := 0;
  1171. m3 := 0;
  1172. m4 := 0;
  1173. value := data;
  1174. for i := 0 to N-1 do
  1175. begin
  1176. deviation := (value^-m1);
  1177. deviation2 := deviation * deviation;
  1178. m2 := m2 + deviation2;
  1179. m3 := m3 + deviation2 * deviation;
  1180. m4 := m4 + deviation2 * deviation2;
  1181. inc(value);
  1182. end;
  1183. m2 := reciprocalN * m2;
  1184. m3 := reciprocalN * m3;
  1185. m4 := reciprocalN * m4;
  1186. skew := m3 / (sqrt(m2)*m2);
  1187. kurtosis := m4 / (m2 * m2);
  1188. end;
  1189. function norm(const data : array of Single) : float; inline;
  1190. begin
  1191. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  1192. end;
  1193. function norm(const data : PSingle; Const N : Integer) : float;
  1194. begin
  1195. norm:=sqrt(sumofsquares(data,N));
  1196. end;
  1197. {$endif FPC_HAS_TYPE_SINGLE}
  1198. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1199. function stddev(const data : array of Double) : float; inline;
  1200. begin
  1201. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  1202. end;
  1203. function stddev(const data : PDouble; Const N : Integer) : float;
  1204. begin
  1205. StdDev:=Sqrt(Variance(Data,N));
  1206. end;
  1207. procedure meanandstddev(const data : array of Double;
  1208. var mean,stddev : float);
  1209. begin
  1210. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  1211. end;
  1212. procedure meanandstddev(const data : PDouble;
  1213. Const N : Longint;var mean,stddev : float);
  1214. Var I : longint;
  1215. begin
  1216. Mean:=0;
  1217. StdDev:=0;
  1218. For I:=0 to N-1 do
  1219. begin
  1220. Mean:=Mean+Data[i];
  1221. StdDev:=StdDev+Sqr(Data[i]);
  1222. end;
  1223. Mean:=Mean/N;
  1224. StdDev:=(StdDev-N*Sqr(Mean));
  1225. If N>1 then
  1226. StdDev:=Sqrt(Stddev/(N-1))
  1227. else
  1228. StdDev:=0;
  1229. end;
  1230. function variance(const data : array of Double) : float; inline;
  1231. begin
  1232. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  1233. end;
  1234. function variance(const data : PDouble; Const N : Integer) : float;
  1235. begin
  1236. If N=1 then
  1237. Result:=0
  1238. else
  1239. Result:=TotalVariance(Data,N)/(N-1);
  1240. end;
  1241. function totalvariance(const data : array of Double) : float; inline;
  1242. begin
  1243. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  1244. end;
  1245. function totalvariance(const data : PDouble;Const N : Integer) : float;
  1246. var S,SS : Float;
  1247. begin
  1248. If N=1 then
  1249. Result:=0
  1250. else
  1251. begin
  1252. SumsAndSquares(Data,N,S,SS);
  1253. Result := SS-Sqr(S)/N;
  1254. end;
  1255. end;
  1256. function popnstddev(const data : array of Double) : float;
  1257. begin
  1258. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  1259. end;
  1260. function popnstddev(const data : PDouble; Const N : Integer) : float;
  1261. begin
  1262. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1263. end;
  1264. function popnvariance(const data : array of Double) : float; inline;
  1265. begin
  1266. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  1267. end;
  1268. function popnvariance(const data : PDouble; Const N : Integer) : float;
  1269. begin
  1270. PopnVariance:=TotalVariance(Data,N)/N;
  1271. end;
  1272. procedure momentskewkurtosis(const data : array of Double;
  1273. out m1,m2,m3,m4,skew,kurtosis : float);
  1274. begin
  1275. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1276. end;
  1277. procedure momentskewkurtosis(
  1278. const data: pdouble;
  1279. Const N: integer;
  1280. out m1: float;
  1281. out m2: float;
  1282. out m3: float;
  1283. out m4: float;
  1284. out skew: float;
  1285. out kurtosis: float
  1286. );
  1287. var
  1288. i: integer;
  1289. value : pdouble;
  1290. deviation, deviation2: double;
  1291. reciprocalN: float;
  1292. begin
  1293. m1 := 0;
  1294. reciprocalN := 1/N;
  1295. value := data;
  1296. for i := 0 to N-1 do
  1297. begin
  1298. m1 := m1 + value^;
  1299. inc(value);
  1300. end;
  1301. m1 := reciprocalN * m1;
  1302. m2 := 0;
  1303. m3 := 0;
  1304. m4 := 0;
  1305. value := data;
  1306. for i := 0 to N-1 do
  1307. begin
  1308. deviation := (value^-m1);
  1309. deviation2 := deviation * deviation;
  1310. m2 := m2 + deviation2;
  1311. m3 := m3 + deviation2 * deviation;
  1312. m4 := m4 + deviation2 * deviation2;
  1313. inc(value);
  1314. end;
  1315. m2 := reciprocalN * m2;
  1316. m3 := reciprocalN * m3;
  1317. m4 := reciprocalN * m4;
  1318. skew := m3 / (sqrt(m2)*m2);
  1319. kurtosis := m4 / (m2 * m2);
  1320. end;
  1321. function norm(const data : array of Double) : float; inline;
  1322. begin
  1323. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  1324. end;
  1325. function norm(const data : PDouble; Const N : Integer) : float;
  1326. begin
  1327. norm:=sqrt(sumofsquares(data,N));
  1328. end;
  1329. {$endif FPC_HAS_TYPE_DOUBLE}
  1330. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1331. function stddev(const data : array of Extended) : float; inline;
  1332. begin
  1333. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  1334. end;
  1335. function stddev(const data : PExtended; Const N : Integer) : float;
  1336. begin
  1337. StdDev:=Sqrt(Variance(Data,N));
  1338. end;
  1339. procedure meanandstddev(const data : array of Extended;
  1340. var mean,stddev : float); inline;
  1341. begin
  1342. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  1343. end;
  1344. procedure meanandstddev(const data : PExtended;
  1345. Const N : Longint;var mean,stddev : float);
  1346. Var I : longint;
  1347. begin
  1348. Mean:=0;
  1349. StdDev:=0;
  1350. For I:=0 to N-1 do
  1351. begin
  1352. Mean:=Mean+Data[i];
  1353. StdDev:=StdDev+Sqr(Data[i]);
  1354. end;
  1355. Mean:=Mean/N;
  1356. StdDev:=(StdDev-N*Sqr(Mean));
  1357. If N>1 then
  1358. StdDev:=Sqrt(Stddev/(N-1))
  1359. else
  1360. StdDev:=0;
  1361. end;
  1362. function variance(const data : array of Extended) : float; inline;
  1363. begin
  1364. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  1365. end;
  1366. function variance(const data : PExtended; Const N : Integer) : float;
  1367. begin
  1368. If N=1 then
  1369. Result:=0
  1370. else
  1371. Result:=TotalVariance(Data,N)/(N-1);
  1372. end;
  1373. function totalvariance(const data : array of Extended) : float; inline;
  1374. begin
  1375. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  1376. end;
  1377. function totalvariance(const data : PExtended;Const N : Integer) : float;
  1378. var S,SS : Float;
  1379. begin
  1380. If N=1 then
  1381. Result:=0
  1382. else
  1383. begin
  1384. SumsAndSquares(Data,N,S,SS);
  1385. Result := SS-Sqr(S)/N;
  1386. end;
  1387. end;
  1388. function popnstddev(const data : array of Extended) : float;
  1389. begin
  1390. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  1391. end;
  1392. function popnstddev(const data : PExtended; Const N : Integer) : float;
  1393. begin
  1394. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1395. end;
  1396. function popnvariance(const data : array of Extended) : float; inline;
  1397. begin
  1398. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  1399. end;
  1400. function popnvariance(const data : PExtended; Const N : Integer) : float;
  1401. begin
  1402. PopnVariance:=TotalVariance(Data,N)/N;
  1403. end;
  1404. procedure momentskewkurtosis(const data : array of Extended;
  1405. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1406. begin
  1407. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1408. end;
  1409. procedure momentskewkurtosis(
  1410. const data: pExtended;
  1411. Const N: integer;
  1412. out m1: float;
  1413. out m2: float;
  1414. out m3: float;
  1415. out m4: float;
  1416. out skew: float;
  1417. out kurtosis: float
  1418. );
  1419. var
  1420. i: integer;
  1421. value : pextended;
  1422. deviation, deviation2: extended;
  1423. reciprocalN: float;
  1424. begin
  1425. m1 := 0;
  1426. reciprocalN := 1/N;
  1427. value := data;
  1428. for i := 0 to N-1 do
  1429. begin
  1430. m1 := m1 + value^;
  1431. inc(value);
  1432. end;
  1433. m1 := reciprocalN * m1;
  1434. m2 := 0;
  1435. m3 := 0;
  1436. m4 := 0;
  1437. value := data;
  1438. for i := 0 to N-1 do
  1439. begin
  1440. deviation := (value^-m1);
  1441. deviation2 := deviation * deviation;
  1442. m2 := m2 + deviation2;
  1443. m3 := m3 + deviation2 * deviation;
  1444. m4 := m4 + deviation2 * deviation2;
  1445. inc(value);
  1446. end;
  1447. m2 := reciprocalN * m2;
  1448. m3 := reciprocalN * m3;
  1449. m4 := reciprocalN * m4;
  1450. skew := m3 / (sqrt(m2)*m2);
  1451. kurtosis := m4 / (m2 * m2);
  1452. end;
  1453. function norm(const data : array of Extended) : float; inline;
  1454. begin
  1455. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  1456. end;
  1457. function norm(const data : PExtended; Const N : Integer) : float;
  1458. begin
  1459. norm:=sqrt(sumofsquares(data,N));
  1460. end;
  1461. {$endif FPC_HAS_TYPE_EXTENDED}
  1462. function MinIntValue(const Data: array of Integer): Integer;
  1463. var
  1464. I: Integer;
  1465. begin
  1466. Result := Data[Low(Data)];
  1467. For I := Succ(Low(Data)) To High(Data) Do
  1468. If Data[I] < Result Then Result := Data[I];
  1469. end;
  1470. function MaxIntValue(const Data: array of Integer): Integer;
  1471. var
  1472. I: Integer;
  1473. begin
  1474. Result := Data[Low(Data)];
  1475. For I := Succ(Low(Data)) To High(Data) Do
  1476. If Data[I] > Result Then Result := Data[I];
  1477. end;
  1478. function MinValue(const Data: array of Integer): Integer; inline;
  1479. begin
  1480. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  1481. end;
  1482. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  1483. var
  1484. I: Integer;
  1485. begin
  1486. Result := Data[0];
  1487. For I := 1 To N-1 do
  1488. If Data[I] < Result Then Result := Data[I];
  1489. end;
  1490. function MaxValue(const Data: array of Integer): Integer; inline;
  1491. begin
  1492. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  1493. end;
  1494. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  1495. var
  1496. i : longint;
  1497. begin
  1498. { get an initial value }
  1499. maxvalue:=data[0];
  1500. for i:=1 to N-1 do
  1501. if data[i]>maxvalue then
  1502. maxvalue:=data[i];
  1503. end;
  1504. {$ifdef FPC_HAS_TYPE_SINGLE}
  1505. function minvalue(const data : array of Single) : Single; inline;
  1506. begin
  1507. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  1508. end;
  1509. function minvalue(const data : PSingle; Const N : Integer) : Single;
  1510. var
  1511. i : longint;
  1512. begin
  1513. { get an initial value }
  1514. minvalue:=data[0];
  1515. for i:=1 to N-1 do
  1516. if data[i]<minvalue then
  1517. minvalue:=data[i];
  1518. end;
  1519. function maxvalue(const data : array of Single) : Single; inline;
  1520. begin
  1521. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  1522. end;
  1523. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  1524. var
  1525. i : longint;
  1526. begin
  1527. { get an initial value }
  1528. maxvalue:=data[0];
  1529. for i:=1 to N-1 do
  1530. if data[i]>maxvalue then
  1531. maxvalue:=data[i];
  1532. end;
  1533. {$endif FPC_HAS_TYPE_SINGLE}
  1534. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1535. function minvalue(const data : array of Double) : Double; inline;
  1536. begin
  1537. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  1538. end;
  1539. function minvalue(const data : PDouble; Const N : Integer) : Double;
  1540. var
  1541. i : longint;
  1542. begin
  1543. { get an initial value }
  1544. minvalue:=data[0];
  1545. for i:=1 to N-1 do
  1546. if data[i]<minvalue then
  1547. minvalue:=data[i];
  1548. end;
  1549. function maxvalue(const data : array of Double) : Double; inline;
  1550. begin
  1551. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  1552. end;
  1553. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  1554. var
  1555. i : longint;
  1556. begin
  1557. { get an initial value }
  1558. maxvalue:=data[0];
  1559. for i:=1 to N-1 do
  1560. if data[i]>maxvalue then
  1561. maxvalue:=data[i];
  1562. end;
  1563. {$endif FPC_HAS_TYPE_DOUBLE}
  1564. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1565. function minvalue(const data : array of Extended) : Extended; inline;
  1566. begin
  1567. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  1568. end;
  1569. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  1570. var
  1571. i : longint;
  1572. begin
  1573. { get an initial value }
  1574. minvalue:=data[0];
  1575. for i:=1 to N-1 do
  1576. if data[i]<minvalue then
  1577. minvalue:=data[i];
  1578. end;
  1579. function maxvalue(const data : array of Extended) : Extended; inline;
  1580. begin
  1581. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  1582. end;
  1583. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  1584. var
  1585. i : longint;
  1586. begin
  1587. { get an initial value }
  1588. maxvalue:=data[0];
  1589. for i:=1 to N-1 do
  1590. if data[i]>maxvalue then
  1591. maxvalue:=data[i];
  1592. end;
  1593. {$endif FPC_HAS_TYPE_EXTENDED}
  1594. function Min(a, b: Integer): Integer;inline;
  1595. begin
  1596. if a < b then
  1597. Result := a
  1598. else
  1599. Result := b;
  1600. end;
  1601. function Max(a, b: Integer): Integer;inline;
  1602. begin
  1603. if a > b then
  1604. Result := a
  1605. else
  1606. Result := b;
  1607. end;
  1608. {
  1609. function Min(a, b: Cardinal): Cardinal;inline;
  1610. begin
  1611. if a < b then
  1612. Result := a
  1613. else
  1614. Result := b;
  1615. end;
  1616. function Max(a, b: Cardinal): Cardinal;inline;
  1617. begin
  1618. if a > b then
  1619. Result := a
  1620. else
  1621. Result := b;
  1622. end;
  1623. }
  1624. function Min(a, b: Int64): Int64;inline;
  1625. begin
  1626. if a < b then
  1627. Result := a
  1628. else
  1629. Result := b;
  1630. end;
  1631. function Max(a, b: Int64): Int64;inline;
  1632. begin
  1633. if a > b then
  1634. Result := a
  1635. else
  1636. Result := b;
  1637. end;
  1638. {$ifdef FPC_HAS_TYPE_SINGLE}
  1639. function Min(a, b: Single): Single;inline;
  1640. begin
  1641. if a < b then
  1642. Result := a
  1643. else
  1644. Result := b;
  1645. end;
  1646. function Max(a, b: Single): Single;inline;
  1647. begin
  1648. if a > b then
  1649. Result := a
  1650. else
  1651. Result := b;
  1652. end;
  1653. {$endif FPC_HAS_TYPE_SINGLE}
  1654. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1655. function Min(a, b: Double): Double;inline;
  1656. begin
  1657. if a < b then
  1658. Result := a
  1659. else
  1660. Result := b;
  1661. end;
  1662. function Max(a, b: Double): Double;inline;
  1663. begin
  1664. if a > b then
  1665. Result := a
  1666. else
  1667. Result := b;
  1668. end;
  1669. {$endif FPC_HAS_TYPE_DOUBLE}
  1670. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1671. function Min(a, b: Extended): Extended;inline;
  1672. begin
  1673. if a < b then
  1674. Result := a
  1675. else
  1676. Result := b;
  1677. end;
  1678. function Max(a, b: Extended): Extended;inline;
  1679. begin
  1680. if a > b then
  1681. Result := a
  1682. else
  1683. Result := b;
  1684. end;
  1685. {$endif FPC_HAS_TYPE_EXTENDED}
  1686. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  1687. begin
  1688. Result:=(AValue>=AMin) and (AValue<=AMax);
  1689. end;
  1690. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  1691. begin
  1692. Result:=(AValue>=AMin) and (AValue<=AMax);
  1693. end;
  1694. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1695. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  1696. begin
  1697. Result:=(AValue>=AMin) and (AValue<=AMax);
  1698. end;
  1699. {$endif FPC_HAS_TYPE_DOUBLE}
  1700. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  1701. begin
  1702. Result:=AValue;
  1703. If Result<AMin then
  1704. Result:=AMin
  1705. else if Result>AMax then
  1706. Result:=AMax;
  1707. end;
  1708. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  1709. begin
  1710. Result:=AValue;
  1711. If Result<AMin then
  1712. Result:=AMin
  1713. else if Result>AMax then
  1714. Result:=AMax;
  1715. end;
  1716. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1717. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  1718. begin
  1719. Result:=AValue;
  1720. If Result<AMin then
  1721. Result:=AMin
  1722. else if Result>AMax then
  1723. Result:=AMax;
  1724. end;
  1725. {$endif FPC_HAS_TYPE_DOUBLE}
  1726. Const
  1727. EZeroResolution = 1E-16;
  1728. DZeroResolution = 1E-12;
  1729. SZeroResolution = 1E-4;
  1730. function IsZero(const A: Single; Epsilon: Single): Boolean;
  1731. begin
  1732. if (Epsilon=0) then
  1733. Epsilon:=SZeroResolution;
  1734. Result:=Abs(A)<=Epsilon;
  1735. end;
  1736. function IsZero(const A: Single): Boolean;inline;
  1737. begin
  1738. Result:=IsZero(A,single(SZeroResolution));
  1739. end;
  1740. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1741. function IsZero(const A: Double; Epsilon: Double): Boolean;
  1742. begin
  1743. if (Epsilon=0) then
  1744. Epsilon:=DZeroResolution;
  1745. Result:=Abs(A)<=Epsilon;
  1746. end;
  1747. function IsZero(const A: Double): Boolean;inline;
  1748. begin
  1749. Result:=IsZero(A,DZeroResolution);
  1750. end;
  1751. {$endif FPC_HAS_TYPE_DOUBLE}
  1752. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1753. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  1754. begin
  1755. if (Epsilon=0) then
  1756. Epsilon:=EZeroResolution;
  1757. Result:=Abs(A)<=Epsilon;
  1758. end;
  1759. function IsZero(const A: Extended): Boolean;inline;
  1760. begin
  1761. Result:=IsZero(A,EZeroResolution);
  1762. end;
  1763. {$endif FPC_HAS_TYPE_EXTENDED}
  1764. type
  1765. TSplitDouble = packed record
  1766. cards: Array[0..1] of cardinal;
  1767. end;
  1768. function IsNan(const d : Single): Boolean; overload;
  1769. begin
  1770. result:=(longword(d) and $7fffffff)>$7f800000;
  1771. end;
  1772. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1773. function IsNan(const d : Double): Boolean;
  1774. var
  1775. fraczero, expMaximal: boolean;
  1776. begin
  1777. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1778. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1779. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1780. (TSplitDouble(d).cards[1] = 0);
  1781. {$else FPC_BIG_ENDIAN}
  1782. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1783. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1784. (TSplitDouble(d).cards[0] = 0);
  1785. {$endif FPC_BIG_ENDIAN}
  1786. Result:=expMaximal and not(fraczero);
  1787. end;
  1788. {$endif FPC_HAS_TYPE_DOUBLE}
  1789. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1790. function IsNan(const d : Extended): Boolean; overload;
  1791. type
  1792. TSplitExtended = packed record
  1793. case byte of
  1794. 0: (bytes: Array[0..9] of byte);
  1795. 1: (words: Array[0..4] of word);
  1796. 2: (cards: Array[0..1] of cardinal; w: word);
  1797. end;
  1798. var
  1799. fraczero, expMaximal: boolean;
  1800. begin
  1801. {$ifdef FPC_BIG_ENDIAN}
  1802. {$error no support for big endian extended type yet}
  1803. {$else FPC_BIG_ENDIAN}
  1804. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  1805. fraczero := (TSplitExtended(d).cards[0] = 0) and
  1806. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  1807. {$endif FPC_BIG_ENDIAN}
  1808. Result:=expMaximal and not(fraczero);
  1809. end;
  1810. {$endif FPC_HAS_TYPE_EXTENDED}
  1811. function IsInfinite(const d : Double): Boolean;
  1812. var
  1813. fraczero, expMaximal: boolean;
  1814. begin
  1815. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  1816. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1817. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1818. (TSplitDouble(d).cards[1] = 0);
  1819. {$else FPC_BIG_ENDIAN}
  1820. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1821. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1822. (TSplitDouble(d).cards[0] = 0);
  1823. {$endif FPC_BIG_ENDIAN}
  1824. Result:=expMaximal and fraczero;
  1825. end;
  1826. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1827. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1828. begin
  1829. if (Epsilon=0) then
  1830. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1831. if (A>B) then
  1832. Result:=((A-B)<=Epsilon)
  1833. else
  1834. Result:=((B-A)<=Epsilon);
  1835. end;
  1836. function SameValue(const A, B: Extended): Boolean;inline;
  1837. begin
  1838. Result:=SameValue(A,B,0.0);
  1839. end;
  1840. {$endif FPC_HAS_TYPE_EXTENDED}
  1841. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1842. function SameValue(const A, B: Double): Boolean;inline;
  1843. begin
  1844. Result:=SameValue(A,B,0.0);
  1845. end;
  1846. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1847. begin
  1848. if (Epsilon=0) then
  1849. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1850. if (A>B) then
  1851. Result:=((A-B)<=Epsilon)
  1852. else
  1853. Result:=((B-A)<=Epsilon);
  1854. end;
  1855. {$endif FPC_HAS_TYPE_DOUBLE}
  1856. function SameValue(const A, B: Single): Boolean;inline;
  1857. begin
  1858. Result:=SameValue(A,B,0);
  1859. end;
  1860. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  1861. begin
  1862. if (Epsilon=0) then
  1863. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  1864. if (A>B) then
  1865. Result:=((A-B)<=Epsilon)
  1866. else
  1867. Result:=((B-A)<=Epsilon);
  1868. end;
  1869. // Some CPUs probably allow a faster way of doing this in a single operation...
  1870. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  1871. {$ifndef FPC_MATH_HAS_DIVMOD}
  1872. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  1873. begin
  1874. if Dividend < 0 then
  1875. begin
  1876. { Use DivMod with >=0 dividend }
  1877. Dividend:=-Dividend;
  1878. { The documented behavior of Pascal's div/mod operators and DivMod
  1879. on negative dividends is to return Result closer to zero and
  1880. a negative Remainder. Which means that we can just negate both
  1881. Result and Remainder, and all it's Ok. }
  1882. Result:=-(Dividend Div Divisor);
  1883. Remainder:=-(Dividend+(Result*Divisor));
  1884. end
  1885. else
  1886. begin
  1887. Result:=Dividend Div Divisor;
  1888. Remainder:=Dividend-(Result*Divisor);
  1889. end;
  1890. end;
  1891. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  1892. begin
  1893. if Dividend < 0 then
  1894. begin
  1895. { Use DivMod with >=0 dividend }
  1896. Dividend:=-Dividend;
  1897. { The documented behavior of Pascal's div/mod operators and DivMod
  1898. on negative dividends is to return Result closer to zero and
  1899. a negative Remainder. Which means that we can just negate both
  1900. Result and Remainder, and all it's Ok. }
  1901. Result:=-(Dividend Div Divisor);
  1902. Remainder:=-(Dividend+(Result*Divisor));
  1903. end
  1904. else
  1905. begin
  1906. Result:=Dividend Div Divisor;
  1907. Remainder:=Dividend-(Result*Divisor);
  1908. end;
  1909. end;
  1910. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  1911. begin
  1912. Result:=Dividend Div Divisor;
  1913. Remainder:=Dividend-(Result*Divisor);
  1914. end;
  1915. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  1916. begin
  1917. if Dividend < 0 then
  1918. begin
  1919. { Use DivMod with >=0 dividend }
  1920. Dividend:=-Dividend;
  1921. { The documented behavior of Pascal's div/mod operators and DivMod
  1922. on negative dividends is to return Result closer to zero and
  1923. a negative Remainder. Which means that we can just negate both
  1924. Result and Remainder, and all it's Ok. }
  1925. Result:=-(Dividend Div Divisor);
  1926. Remainder:=-(Dividend+(Result*Divisor));
  1927. end
  1928. else
  1929. begin
  1930. Result:=Dividend Div Divisor;
  1931. Remainder:=Dividend-(Result*Divisor);
  1932. end;
  1933. end;
  1934. {$endif FPC_MATH_HAS_DIVMOD}
  1935. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  1936. begin
  1937. if val then result:=iftrue else result:=iffalse;
  1938. end;
  1939. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  1940. begin
  1941. if val then result:=iftrue else result:=iffalse;
  1942. end;
  1943. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  1944. begin
  1945. if val then result:=iftrue else result:=iffalse;
  1946. end;
  1947. // dilemma here. asm can do the two comparisons in one go?
  1948. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  1949. function CompareValue(const A, B : Integer): TValueRelationship;
  1950. begin
  1951. result:=GreaterThanValue;
  1952. if a=b then
  1953. result:=EqualsValue
  1954. else
  1955. if a<b then
  1956. result:=LessThanValue;
  1957. end;
  1958. function CompareValue(const A, B: Int64): TValueRelationship;
  1959. begin
  1960. result:=GreaterThanValue;
  1961. if a=b then
  1962. result:=EqualsValue
  1963. else
  1964. if a<b then
  1965. result:=LessThanValue;
  1966. end;
  1967. function CompareValue(const A, B: QWord): TValueRelationship;
  1968. begin
  1969. result:=GreaterThanValue;
  1970. if a=b then
  1971. result:=EqualsValue
  1972. else
  1973. if a<b then
  1974. result:=LessThanValue;
  1975. end;
  1976. {$ifdef FPC_HAS_TYPE_SINGLE}
  1977. function CompareValue(const A, B: Single; delta: Single = 0.0): TValueRelationship;
  1978. begin
  1979. result:=GreaterThanValue;
  1980. if abs(a-b)<=delta then
  1981. result:=EqualsValue
  1982. else
  1983. if a<b then
  1984. result:=LessThanValue;
  1985. end;
  1986. {$endif}
  1987. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1988. function CompareValue(const A, B: Double; delta: Double = 0.0): TValueRelationship;
  1989. begin
  1990. result:=GreaterThanValue;
  1991. if abs(a-b)<=delta then
  1992. result:=EqualsValue
  1993. else
  1994. if a<b then
  1995. result:=LessThanValue;
  1996. end;
  1997. {$endif}
  1998. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1999. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  2000. begin
  2001. result:=GreaterThanValue;
  2002. if abs(a-b)<=delta then
  2003. result:=EqualsValue
  2004. else
  2005. if a<b then
  2006. result:=LessThanValue;
  2007. end;
  2008. {$endif}
  2009. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2010. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  2011. var
  2012. RV : Double;
  2013. begin
  2014. RV:=IntPower(10,Digits);
  2015. Result:=Round(AValue/RV)*RV;
  2016. end;
  2017. {$endif}
  2018. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2019. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  2020. var
  2021. RV : Extended;
  2022. begin
  2023. RV:=IntPower(10,Digits);
  2024. Result:=Round(AValue/RV)*RV;
  2025. end;
  2026. {$endif}
  2027. {$ifdef FPC_HAS_TYPE_SINGLE}
  2028. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  2029. var
  2030. RV : Single;
  2031. begin
  2032. RV:=IntPower(10,Digits);
  2033. Result:=Round(AValue/RV)*RV;
  2034. end;
  2035. {$endif}
  2036. {$ifdef FPC_HAS_TYPE_SINGLE}
  2037. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  2038. var
  2039. RV : Single;
  2040. begin
  2041. RV := IntPower(10, -Digits);
  2042. if AValue < 0 then
  2043. Result := Trunc((AValue*RV) - 0.5)/RV
  2044. else
  2045. Result := Trunc((AValue*RV) + 0.5)/RV;
  2046. end;
  2047. {$endif}
  2048. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2049. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  2050. var
  2051. RV : Double;
  2052. begin
  2053. RV := IntPower(10, -Digits);
  2054. if AValue < 0 then
  2055. Result := Trunc((AValue*RV) - 0.5)/RV
  2056. else
  2057. Result := Trunc((AValue*RV) + 0.5)/RV;
  2058. end;
  2059. {$endif}
  2060. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2061. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  2062. var
  2063. RV : Extended;
  2064. begin
  2065. RV := IntPower(10, -Digits);
  2066. if AValue < 0 then
  2067. Result := Trunc((AValue*RV) - 0.5)/RV
  2068. else
  2069. Result := Trunc((AValue*RV) + 0.5)/RV;
  2070. end;
  2071. {$endif}
  2072. function RandomFrom(const AValues: array of Double): Double; overload;
  2073. begin
  2074. result:=AValues[random(High(AValues)+1)];
  2075. end;
  2076. function RandomFrom(const AValues: array of Integer): Integer; overload;
  2077. begin
  2078. result:=AValues[random(High(AValues)+1)];
  2079. end;
  2080. function RandomFrom(const AValues: array of Int64): Int64; overload;
  2081. begin
  2082. result:=AValues[random(High(AValues)+1)];
  2083. end;
  2084. {$else}
  2085. implementation
  2086. {$endif FPUNONE}
  2087. end.