math.pp 68 KB

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