math.pp 59 KB

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