math.pp 59 KB

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