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