math.pp 71 KB

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