math.pp 66 KB

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