math.pp 66 KB

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