math.pp 70 KB

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