math.pp 71 KB

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