math.pp 71 KB

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