math.pp 66 KB

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