math.pp 57 KB

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