math.pp 60 KB

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