math.pp 66 KB

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