math.pp 59 KB

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