math.pp 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959
  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. result:=ArcTan(y/x);
  747. if x<0 then
  748. if y<0 then
  749. result:=result-pi
  750. else
  751. result:=result+pi;
  752. end;
  753. end;
  754. {$endif FPC_MATH_HAS_ARCTAN2}
  755. function cosh(x : float) : float;
  756. var
  757. temp : float;
  758. begin
  759. temp:=exp(x);
  760. cosh:=0.5*(temp+1.0/temp);
  761. end;
  762. function sinh(x : float) : float;
  763. var
  764. temp : float;
  765. begin
  766. temp:=exp(x);
  767. { copysign ensures that sinh(-0.0)=-0.0 }
  768. sinh:=copysign(0.5*(temp-1.0/temp),x);
  769. end;
  770. function tanh(x : float) : float;
  771. var
  772. tmp:float;
  773. begin
  774. if x < 0 then begin
  775. tmp:=exp(2*x);
  776. result:=(tmp-1)/(1+tmp)
  777. end
  778. else begin
  779. tmp:=exp(-2*x);
  780. result:=(1-tmp)/(1+tmp)
  781. end;
  782. end;
  783. function arccosh(x : float) : float; inline;
  784. begin
  785. arccosh:=arcosh(x);
  786. end;
  787. function arcsinh(x : float) : float;inline;
  788. begin
  789. arcsinh:=arsinh(x);
  790. end;
  791. function arctanh(x : float) : float;inline;
  792. begin
  793. arctanh:=artanh(x);
  794. end;
  795. function arcosh(x : float) : float;
  796. begin
  797. { Provides accuracy about 4*eps near 1.0 }
  798. arcosh:=Ln(x+Sqrt((x-1.0)*(x+1.0)));
  799. end;
  800. function arsinh(x : float) : float;
  801. var
  802. z: float;
  803. begin
  804. z:=abs(x);
  805. z:=Ln(z+Sqrt(1+z*z));
  806. { copysign ensures that arsinh(-Inf)=-Inf and arsinh(-0.0)=-0.0 }
  807. arsinh:=copysign(z,x);
  808. end;
  809. function artanh(x : float) : float;
  810. begin
  811. artanh:=(lnxp1(x)-lnxp1(-x))*0.5;
  812. end;
  813. { hypot function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  814. function hypot(x,y : float) : float;
  815. begin
  816. x:=abs(x);
  817. y:=abs(y);
  818. if (x>y) then
  819. hypot:=x*sqrt(1.0+sqr(y/x))
  820. else if (x>0.0) then
  821. hypot:=y*sqrt(1.0+sqr(x/y))
  822. else
  823. hypot:=y;
  824. end;
  825. function log10(x : float) : float;
  826. begin
  827. log10:=ln(x)*0.43429448190325182765; { 1/ln(10) }
  828. end;
  829. {$ifndef FPC_MATH_HAS_LOG2}
  830. function log2(x : float) : float;
  831. begin
  832. log2:=ln(x)*1.4426950408889634079; { 1/ln(2) }
  833. end;
  834. {$endif FPC_MATH_HAS_LOG2}
  835. function logn(n,x : float) : float;
  836. begin
  837. logn:=ln(x)/ln(n);
  838. end;
  839. { lnxp1 function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  840. function lnxp1(x : float) : float;
  841. var
  842. y: float;
  843. begin
  844. if (x>=4.0) then
  845. lnxp1:=ln(1.0+x)
  846. else
  847. begin
  848. y:=1.0+x;
  849. if (y=1.0) then
  850. lnxp1:=x
  851. else
  852. begin
  853. lnxp1:=ln(y); { lnxp1(-1) = ln(0) = -Inf }
  854. if y>0.0 then
  855. lnxp1:=lnxp1+(x-(y-1.0))/y;
  856. end;
  857. end;
  858. end;
  859. function power(base,exponent : float) : float;
  860. begin
  861. if Exponent=0.0 then
  862. result:=1.0
  863. else if (base=0.0) and (exponent>0.0) then
  864. result:=0.0
  865. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  866. result:=intpower(base,trunc(exponent))
  867. else
  868. result:=exp(exponent * ln (base));
  869. end;
  870. function intpower(base : float;const exponent : Integer) : float;
  871. var
  872. i : longint;
  873. begin
  874. if (base = 0.0) and (exponent = 0) then
  875. result:=1
  876. else
  877. begin
  878. if exponent<0 then
  879. base:=1.0/base;
  880. i:=abs(exponent);
  881. intpower:=1.0;
  882. while i>0 do
  883. begin
  884. while (i and 1)=0 do
  885. begin
  886. i:=i shr 1;
  887. base:=sqr(base);
  888. end;
  889. i:=i-1;
  890. intpower:=intpower*base;
  891. end;
  892. end;
  893. end;
  894. operator ** (bas,expo : float) e: float; inline;
  895. begin
  896. e:=power(bas,expo);
  897. end;
  898. operator ** (bas,expo : int64) i: int64; inline;
  899. begin
  900. i:=round(intpower(bas,expo));
  901. end;
  902. function ceil(x : float) : integer;
  903. begin
  904. Result:=Trunc(x)+ord(Frac(x)>0);
  905. end;
  906. function ceil64(x: float): Int64;
  907. begin
  908. Result:=Trunc(x)+ord(Frac(x)>0);
  909. end;
  910. function floor(x : float) : integer;
  911. begin
  912. Result:=Trunc(x)-ord(Frac(x)<0);
  913. end;
  914. function floor64(x: float): Int64;
  915. begin
  916. Result:=Trunc(x)-ord(Frac(x)<0);
  917. end;
  918. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  919. begin
  920. Exponent:=0;
  921. if (X<>0) then
  922. if (abs(X)<0.5) then
  923. repeat
  924. X:=X*2;
  925. Dec(Exponent);
  926. until (abs(X)>=0.5)
  927. else
  928. while (abs(X)>=1) do
  929. begin
  930. X:=X/2;
  931. Inc(Exponent);
  932. end;
  933. Mantissa:=X;
  934. end;
  935. function ldexp(x : float;const p : Integer) : float;
  936. begin
  937. ldexp:=x*intpower(2.0,p);
  938. end;
  939. const
  940. { Cutoff for https://en.wikipedia.org/wiki/Pairwise_summation; sums of at least this many elements are split in two halves. }
  941. RecursiveSumThreshold=12;
  942. {$ifdef FPC_HAS_TYPE_SINGLE}
  943. function mean(const data : array of Single) : float;
  944. begin
  945. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  946. end;
  947. function mean(const data : PSingle; Const N : longint) : float;
  948. begin
  949. mean:=sum(Data,N);
  950. mean:=mean/N;
  951. end;
  952. function sum(const data : array of Single) : float;inline;
  953. begin
  954. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  955. end;
  956. function sum(const data : PSingle;Const N : longint) : float;
  957. var
  958. i : SizeInt;
  959. begin
  960. if N>=RecursiveSumThreshold then
  961. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  962. else
  963. begin
  964. result:=0;
  965. for i:=0 to N-1 do
  966. result:=result+data[i];
  967. end;
  968. end;
  969. {$endif FPC_HAS_TYPE_SINGLE}
  970. {$ifdef FPC_HAS_TYPE_DOUBLE}
  971. function mean(const data : array of Double) : float; inline;
  972. begin
  973. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  974. end;
  975. function mean(const data : PDouble; Const N : longint) : float;
  976. begin
  977. mean:=sum(Data,N);
  978. mean:=mean/N;
  979. end;
  980. function sum(const data : array of Double) : float; inline;
  981. begin
  982. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  983. end;
  984. function sum(const data : PDouble;Const N : longint) : float;
  985. var
  986. i : SizeInt;
  987. begin
  988. if N>=RecursiveSumThreshold then
  989. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  990. else
  991. begin
  992. result:=0;
  993. for i:=0 to N-1 do
  994. result:=result+data[i];
  995. end;
  996. end;
  997. {$endif FPC_HAS_TYPE_DOUBLE}
  998. {$ifdef FPC_HAS_TYPE_EXTENDED}
  999. function mean(const data : array of Extended) : float;
  1000. begin
  1001. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  1002. end;
  1003. function mean(const data : PExtended; Const N : longint) : float;
  1004. begin
  1005. mean:=sum(Data,N);
  1006. mean:=mean/N;
  1007. end;
  1008. function sum(const data : array of Extended) : float; inline;
  1009. begin
  1010. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  1011. end;
  1012. function sum(const data : PExtended;Const N : longint) : float;
  1013. var
  1014. i : SizeInt;
  1015. begin
  1016. if N>=RecursiveSumThreshold then
  1017. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1018. else
  1019. begin
  1020. result:=0;
  1021. for i:=0 to N-1 do
  1022. result:=result+data[i];
  1023. end;
  1024. end;
  1025. {$endif FPC_HAS_TYPE_EXTENDED}
  1026. function sumInt(const data : PInt64;Const N : longint) : Int64;
  1027. var
  1028. i : SizeInt;
  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 Int64) : Int64; inline;
  1035. begin
  1036. Result:=SumInt(PInt64(@Data[0]),High(Data)+1);
  1037. end;
  1038. function mean(const data : PInt64; const N : Longint):Float;
  1039. begin
  1040. mean:=sumInt(Data,N);
  1041. mean:=mean/N;
  1042. end;
  1043. function mean(const data: array of Int64):Float;
  1044. begin
  1045. mean:=mean(PInt64(@data[0]),High(Data)+1);
  1046. end;
  1047. function sumInt(const data : PInteger; Const N : longint) : Int64;
  1048. var
  1049. i : SizeInt;
  1050. begin
  1051. sumInt:=0;
  1052. for i:=0 to N-1 do
  1053. sumInt:=sumInt+data[i];
  1054. end;
  1055. function sumInt(const data : array of Integer) : Int64;inline;
  1056. begin
  1057. Result:=sumInt(PInteger(@Data[0]),High(Data)+1);
  1058. end;
  1059. function mean(const data : PInteger; const N : Longint):Float;
  1060. begin
  1061. mean:=sumInt(Data,N);
  1062. mean:=mean/N;
  1063. end;
  1064. function mean(const data: array of Integer):Float;
  1065. begin
  1066. mean:=mean(PInteger(@data[0]),High(Data)+1);
  1067. end;
  1068. {$ifdef FPC_HAS_TYPE_SINGLE}
  1069. function sumofsquares(const data : array of Single) : float; inline;
  1070. begin
  1071. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  1072. end;
  1073. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  1074. var
  1075. i : SizeInt;
  1076. begin
  1077. if N>=RecursiveSumThreshold then
  1078. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1079. else
  1080. begin
  1081. result:=0;
  1082. for i:=0 to N-1 do
  1083. result:=result+sqr(data[i]);
  1084. end;
  1085. end;
  1086. procedure sumsandsquares(const data : array of Single;
  1087. var sum,sumofsquares : float); inline;
  1088. begin
  1089. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1090. end;
  1091. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  1092. var sum,sumofsquares : float);
  1093. var
  1094. i : SizeInt;
  1095. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  1096. begin
  1097. if N>=RecursiveSumThreshold then
  1098. begin
  1099. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  1100. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  1101. sum:=sum0+sum1;
  1102. sumofsquares:=sumofsquares0+sumofsquares1;
  1103. end
  1104. else
  1105. begin
  1106. tsum:=0;
  1107. tsumofsquares:=0;
  1108. for i:=0 to N-1 do
  1109. begin
  1110. temp:=data[i];
  1111. tsum:=tsum+temp;
  1112. tsumofsquares:=tsumofsquares+sqr(temp);
  1113. end;
  1114. sum:=tsum;
  1115. sumofsquares:=tsumofsquares;
  1116. end;
  1117. end;
  1118. {$endif FPC_HAS_TYPE_SINGLE}
  1119. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1120. function sumofsquares(const data : array of Double) : float; inline;
  1121. begin
  1122. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  1123. end;
  1124. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  1125. var
  1126. i : SizeInt;
  1127. begin
  1128. if N>=RecursiveSumThreshold then
  1129. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1130. else
  1131. begin
  1132. result:=0;
  1133. for i:=0 to N-1 do
  1134. result:=result+sqr(data[i]);
  1135. end;
  1136. end;
  1137. procedure sumsandsquares(const data : array of Double;
  1138. var sum,sumofsquares : float);
  1139. begin
  1140. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1141. end;
  1142. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  1143. var sum,sumofsquares : float);
  1144. var
  1145. i : SizeInt;
  1146. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  1147. begin
  1148. if N>=RecursiveSumThreshold then
  1149. begin
  1150. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  1151. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  1152. sum:=sum0+sum1;
  1153. sumofsquares:=sumofsquares0+sumofsquares1;
  1154. end
  1155. else
  1156. begin
  1157. tsum:=0;
  1158. tsumofsquares:=0;
  1159. for i:=0 to N-1 do
  1160. begin
  1161. temp:=data[i];
  1162. tsum:=tsum+temp;
  1163. tsumofsquares:=tsumofsquares+sqr(temp);
  1164. end;
  1165. sum:=tsum;
  1166. sumofsquares:=tsumofsquares;
  1167. end;
  1168. end;
  1169. {$endif FPC_HAS_TYPE_DOUBLE}
  1170. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1171. function sumofsquares(const data : array of Extended) : float; inline;
  1172. begin
  1173. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  1174. end;
  1175. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  1176. var
  1177. i : SizeInt;
  1178. begin
  1179. if N>=RecursiveSumThreshold then
  1180. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1181. else
  1182. begin
  1183. result:=0;
  1184. for i:=0 to N-1 do
  1185. result:=result+sqr(data[i]);
  1186. end;
  1187. end;
  1188. procedure sumsandsquares(const data : array of Extended;
  1189. var sum,sumofsquares : float); inline;
  1190. begin
  1191. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1192. end;
  1193. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  1194. var sum,sumofsquares : float);
  1195. var
  1196. i : SizeInt;
  1197. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  1198. begin
  1199. if N>=RecursiveSumThreshold then
  1200. begin
  1201. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  1202. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  1203. sum:=sum0+sum1;
  1204. sumofsquares:=sumofsquares0+sumofsquares1;
  1205. end
  1206. else
  1207. begin
  1208. tsum:=0;
  1209. tsumofsquares:=0;
  1210. for i:=0 to N-1 do
  1211. begin
  1212. temp:=data[i];
  1213. tsum:=tsum+temp;
  1214. tsumofsquares:=tsumofsquares+sqr(temp);
  1215. end;
  1216. sum:=tsum;
  1217. sumofsquares:=tsumofsquares;
  1218. end;
  1219. end;
  1220. {$endif FPC_HAS_TYPE_EXTENDED}
  1221. function randg(mean,stddev : float) : float;
  1222. Var U1,S2 : Float;
  1223. begin
  1224. repeat
  1225. u1:= 2*random-1;
  1226. S2:=Sqr(U1)+sqr(2*random-1);
  1227. until s2<1;
  1228. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  1229. end;
  1230. function RandomRange(const aFrom, aTo: Integer): Integer;
  1231. begin
  1232. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1233. end;
  1234. function RandomRange(const aFrom, aTo: Int64): Int64;
  1235. begin
  1236. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1237. end;
  1238. {$ifdef FPC_HAS_TYPE_SINGLE}
  1239. procedure MeanAndTotalVariance
  1240. (const data: PSingle; N: LongInt; var mu, variance: float);
  1241. function CalcVariance(data: PSingle; N: SizeInt; mu: float): float;
  1242. var
  1243. i: SizeInt;
  1244. begin
  1245. if N>=RecursiveSumThreshold then
  1246. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  1247. else
  1248. begin
  1249. result:=0;
  1250. for i:=0 to N-1 do
  1251. result:=result+Sqr(data[i]-mu);
  1252. end;
  1253. end;
  1254. begin
  1255. mu := Mean( data, N );
  1256. variance := CalcVariance( data, N, mu );
  1257. end;
  1258. function stddev(const data : array of Single) : float; inline;
  1259. begin
  1260. Result:=Stddev(PSingle(@Data[0]),High(Data)+1);
  1261. end;
  1262. function stddev(const data : PSingle; Const N : Integer) : float;
  1263. begin
  1264. StdDev:=Sqrt(Variance(Data,N));
  1265. end;
  1266. procedure meanandstddev(const data : array of Single;
  1267. var mean,stddev : float); inline;
  1268. begin
  1269. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  1270. end;
  1271. procedure meanandstddev
  1272. ( const data: PSingle;
  1273. const N: Longint;
  1274. var mean,
  1275. stdDev: Float
  1276. );
  1277. var totalVariance: float;
  1278. begin
  1279. MeanAndTotalVariance( data, N, mean, totalVariance );
  1280. if N < 2 then stdDev := 0
  1281. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1282. end;
  1283. function variance(const data : array of Single) : float; inline;
  1284. begin
  1285. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  1286. end;
  1287. function variance(const data : PSingle; Const N : Integer) : float;
  1288. begin
  1289. If N=1 then
  1290. Result:=0
  1291. else
  1292. Result:=TotalVariance(Data,N)/(N-1);
  1293. end;
  1294. function totalvariance(const data : array of Single) : float; inline;
  1295. begin
  1296. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  1297. end;
  1298. function totalvariance(const data : PSingle; const N : Integer) : float;
  1299. var mu: float;
  1300. begin
  1301. MeanAndTotalVariance( data, N, mu, result );
  1302. end;
  1303. function popnstddev(const data : array of Single) : float;
  1304. begin
  1305. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  1306. end;
  1307. function popnstddev(const data : PSingle; Const N : Integer) : float;
  1308. begin
  1309. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1310. end;
  1311. function popnvariance(const data : array of Single) : float; inline;
  1312. begin
  1313. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  1314. end;
  1315. function popnvariance(const data : PSingle; Const N : Integer) : float;
  1316. begin
  1317. PopnVariance:=TotalVariance(Data,N)/N;
  1318. end;
  1319. procedure momentskewkurtosis(const data : array of single;
  1320. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1321. begin
  1322. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1323. end;
  1324. type
  1325. TMoments2to4 = array[2 .. 4] of float;
  1326. procedure momentskewkurtosis(
  1327. const data: pSingle;
  1328. Const N: integer;
  1329. out m1: float;
  1330. out m2: float;
  1331. out m3: float;
  1332. out m4: float;
  1333. out skew: float;
  1334. out kurtosis: float
  1335. );
  1336. procedure CalcDevSums2to4(data: PSingle; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  1337. var
  1338. tm2, tm3, tm4, dev, dev2: float;
  1339. i: SizeInt;
  1340. m2to4Part0, m2to4Part1: TMoments2to4;
  1341. begin
  1342. if N >= RecursiveSumThreshold then
  1343. begin
  1344. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  1345. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  1346. for i := Low(TMoments2to4) to High(TMoments2to4) do
  1347. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  1348. end
  1349. else
  1350. begin
  1351. tm2 := 0;
  1352. tm3 := 0;
  1353. tm4 := 0;
  1354. for i := 0 to N - 1 do
  1355. begin
  1356. dev := data[i] - m1;
  1357. dev2 := sqr(dev);
  1358. tm2 := tm2 + dev2;
  1359. tm3 := tm3 + dev2 * dev;
  1360. tm4 := tm4 + sqr(dev2);
  1361. end;
  1362. m2to4[2] := tm2;
  1363. m2to4[3] := tm3;
  1364. m2to4[4] := tm4;
  1365. end;
  1366. end;
  1367. var
  1368. reciprocalN: float;
  1369. m2to4: TMoments2to4;
  1370. begin
  1371. m1 := 0;
  1372. reciprocalN := 1/N;
  1373. m1 := reciprocalN * sum(data, N);
  1374. CalcDevSums2to4(data, N, m1, m2to4);
  1375. m2 := reciprocalN * m2to4[2];
  1376. m3 := reciprocalN * m2to4[3];
  1377. m4 := reciprocalN * m2to4[4];
  1378. skew := m3 / (sqrt(m2)*m2);
  1379. kurtosis := m4 / (m2 * m2);
  1380. end;
  1381. function norm(const data : array of Single) : float; inline;
  1382. begin
  1383. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  1384. end;
  1385. function norm(const data : PSingle; Const N : Integer) : float;
  1386. begin
  1387. norm:=sqrt(sumofsquares(data,N));
  1388. end;
  1389. {$endif FPC_HAS_TYPE_SINGLE}
  1390. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1391. procedure MeanAndTotalVariance
  1392. (const data: PDouble; N: LongInt; var mu, variance: float);
  1393. function CalcVariance(data: PDouble; N: SizeInt; mu: float): float;
  1394. var
  1395. i: SizeInt;
  1396. begin
  1397. if N>=RecursiveSumThreshold then
  1398. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  1399. else
  1400. begin
  1401. result:=0;
  1402. for i:=0 to N-1 do
  1403. result:=result+Sqr(data[i]-mu);
  1404. end;
  1405. end;
  1406. begin
  1407. mu := Mean( data, N );
  1408. variance := CalcVariance( data, N, mu );
  1409. end;
  1410. function stddev(const data : array of Double) : float; inline;
  1411. begin
  1412. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  1413. end;
  1414. function stddev(const data : PDouble; Const N : Integer) : float;
  1415. begin
  1416. StdDev:=Sqrt(Variance(Data,N));
  1417. end;
  1418. procedure meanandstddev(const data : array of Double;
  1419. var mean,stddev : float);
  1420. begin
  1421. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  1422. end;
  1423. procedure meanandstddev
  1424. ( const data: PDouble;
  1425. const N: Longint;
  1426. var mean,
  1427. stdDev: Float
  1428. );
  1429. var totalVariance: float;
  1430. begin
  1431. MeanAndTotalVariance( data, N, mean, totalVariance );
  1432. if N < 2 then stdDev := 0
  1433. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1434. end;
  1435. function variance(const data : array of Double) : float; inline;
  1436. begin
  1437. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  1438. end;
  1439. function variance(const data : PDouble; Const N : Integer) : float;
  1440. begin
  1441. If N=1 then
  1442. Result:=0
  1443. else
  1444. Result:=TotalVariance(Data,N)/(N-1);
  1445. end;
  1446. function totalvariance(const data : array of Double) : float; inline;
  1447. begin
  1448. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  1449. end;
  1450. function totalvariance(const data : PDouble; const N : Integer) : float;
  1451. var mu: float;
  1452. begin
  1453. MeanAndTotalVariance( data, N, mu, result );
  1454. end;
  1455. function popnstddev(const data : array of Double) : float;
  1456. begin
  1457. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  1458. end;
  1459. function popnstddev(const data : PDouble; Const N : Integer) : float;
  1460. begin
  1461. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1462. end;
  1463. function popnvariance(const data : array of Double) : float; inline;
  1464. begin
  1465. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  1466. end;
  1467. function popnvariance(const data : PDouble; Const N : Integer) : float;
  1468. begin
  1469. PopnVariance:=TotalVariance(Data,N)/N;
  1470. end;
  1471. procedure momentskewkurtosis(const data : array of Double;
  1472. out m1,m2,m3,m4,skew,kurtosis : float);
  1473. begin
  1474. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1475. end;
  1476. procedure momentskewkurtosis(
  1477. const data: pdouble;
  1478. Const N: integer;
  1479. out m1: float;
  1480. out m2: float;
  1481. out m3: float;
  1482. out m4: float;
  1483. out skew: float;
  1484. out kurtosis: float
  1485. );
  1486. procedure CalcDevSums2to4(data: PDouble; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  1487. var
  1488. tm2, tm3, tm4, dev, dev2: float;
  1489. i: SizeInt;
  1490. m2to4Part0, m2to4Part1: TMoments2to4;
  1491. begin
  1492. if N >= RecursiveSumThreshold then
  1493. begin
  1494. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  1495. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  1496. for i := Low(TMoments2to4) to High(TMoments2to4) do
  1497. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  1498. end
  1499. else
  1500. begin
  1501. tm2 := 0;
  1502. tm3 := 0;
  1503. tm4 := 0;
  1504. for i := 0 to N - 1 do
  1505. begin
  1506. dev := data[i] - m1;
  1507. dev2 := sqr(dev);
  1508. tm2 := tm2 + dev2;
  1509. tm3 := tm3 + dev2 * dev;
  1510. tm4 := tm4 + sqr(dev2);
  1511. end;
  1512. m2to4[2] := tm2;
  1513. m2to4[3] := tm3;
  1514. m2to4[4] := tm4;
  1515. end;
  1516. end;
  1517. var
  1518. reciprocalN: float;
  1519. m2to4: TMoments2to4;
  1520. begin
  1521. m1 := 0;
  1522. reciprocalN := 1/N;
  1523. m1 := reciprocalN * sum(data, N);
  1524. CalcDevSums2to4(data, N, m1, m2to4);
  1525. m2 := reciprocalN * m2to4[2];
  1526. m3 := reciprocalN * m2to4[3];
  1527. m4 := reciprocalN * m2to4[4];
  1528. skew := m3 / (sqrt(m2)*m2);
  1529. kurtosis := m4 / (m2 * m2);
  1530. end;
  1531. function norm(const data : array of Double) : float; inline;
  1532. begin
  1533. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  1534. end;
  1535. function norm(const data : PDouble; Const N : Integer) : float;
  1536. begin
  1537. norm:=sqrt(sumofsquares(data,N));
  1538. end;
  1539. {$endif FPC_HAS_TYPE_DOUBLE}
  1540. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1541. procedure MeanAndTotalVariance
  1542. (const data: PExtended; N: LongInt; var mu, variance: float);
  1543. function CalcVariance(data: PExtended; N: SizeInt; mu: float): float;
  1544. var
  1545. i: SizeInt;
  1546. begin
  1547. if N>=RecursiveSumThreshold then
  1548. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  1549. else
  1550. begin
  1551. result:=0;
  1552. for i:=0 to N-1 do
  1553. result:=result+Sqr(data[i]-mu);
  1554. end;
  1555. end;
  1556. begin
  1557. mu := Mean( data, N );
  1558. variance := CalcVariance( data, N, mu );
  1559. end;
  1560. function stddev(const data : array of Extended) : float; inline;
  1561. begin
  1562. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  1563. end;
  1564. function stddev(const data : PExtended; Const N : Integer) : float;
  1565. begin
  1566. StdDev:=Sqrt(Variance(Data,N));
  1567. end;
  1568. procedure meanandstddev(const data : array of Extended;
  1569. var mean,stddev : float); inline;
  1570. begin
  1571. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  1572. end;
  1573. procedure meanandstddev
  1574. ( const data: PExtended;
  1575. const N: Longint;
  1576. var mean,
  1577. stdDev: Float
  1578. );
  1579. var totalVariance: float;
  1580. begin
  1581. MeanAndTotalVariance( data, N, mean, totalVariance );
  1582. if N < 2 then stdDev := 0
  1583. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1584. end;
  1585. function variance(const data : array of Extended) : float; inline;
  1586. begin
  1587. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  1588. end;
  1589. function variance(const data : PExtended; Const N : Integer) : float;
  1590. begin
  1591. If N=1 then
  1592. Result:=0
  1593. else
  1594. Result:=TotalVariance(Data,N)/(N-1);
  1595. end;
  1596. function totalvariance(const data : array of Extended) : float; inline;
  1597. begin
  1598. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  1599. end;
  1600. function totalvariance(const data : PExtended;Const N : Integer) : float;
  1601. var mu: float;
  1602. begin
  1603. MeanAndTotalVariance( data, N, mu, result );
  1604. end;
  1605. function popnstddev(const data : array of Extended) : float;
  1606. begin
  1607. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  1608. end;
  1609. function popnstddev(const data : PExtended; Const N : Integer) : float;
  1610. begin
  1611. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  1612. end;
  1613. function popnvariance(const data : array of Extended) : float; inline;
  1614. begin
  1615. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  1616. end;
  1617. function popnvariance(const data : PExtended; Const N : Integer) : float;
  1618. begin
  1619. PopnVariance:=TotalVariance(Data,N)/N;
  1620. end;
  1621. procedure momentskewkurtosis(const data : array of Extended;
  1622. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  1623. begin
  1624. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  1625. end;
  1626. procedure momentskewkurtosis(
  1627. const data: pExtended;
  1628. Const N: Integer;
  1629. out m1: float;
  1630. out m2: float;
  1631. out m3: float;
  1632. out m4: float;
  1633. out skew: float;
  1634. out kurtosis: float
  1635. );
  1636. procedure CalcDevSums2to4(data: PExtended; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  1637. var
  1638. tm2, tm3, tm4, dev, dev2: float;
  1639. i: SizeInt;
  1640. m2to4Part0, m2to4Part1: TMoments2to4;
  1641. begin
  1642. if N >= RecursiveSumThreshold then
  1643. begin
  1644. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  1645. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  1646. for i := Low(TMoments2to4) to High(TMoments2to4) do
  1647. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  1648. end
  1649. else
  1650. begin
  1651. tm2 := 0;
  1652. tm3 := 0;
  1653. tm4 := 0;
  1654. for i := 0 to N - 1 do
  1655. begin
  1656. dev := data[i] - m1;
  1657. dev2 := sqr(dev);
  1658. tm2 := tm2 + dev2;
  1659. tm3 := tm3 + dev2 * dev;
  1660. tm4 := tm4 + sqr(dev2);
  1661. end;
  1662. m2to4[2] := tm2;
  1663. m2to4[3] := tm3;
  1664. m2to4[4] := tm4;
  1665. end;
  1666. end;
  1667. var
  1668. reciprocalN: float;
  1669. m2to4: TMoments2to4;
  1670. begin
  1671. m1 := 0;
  1672. reciprocalN := 1/N;
  1673. m1 := reciprocalN * sum(data, N);
  1674. CalcDevSums2to4(data, N, m1, m2to4);
  1675. m2 := reciprocalN * m2to4[2];
  1676. m3 := reciprocalN * m2to4[3];
  1677. m4 := reciprocalN * m2to4[4];
  1678. skew := m3 / (sqrt(m2)*m2);
  1679. kurtosis := m4 / (m2 * m2);
  1680. end;
  1681. function norm(const data : array of Extended) : float; inline;
  1682. begin
  1683. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  1684. end;
  1685. function norm(const data : PExtended; Const N : Integer) : float;
  1686. begin
  1687. norm:=sqrt(sumofsquares(data,N));
  1688. end;
  1689. {$endif FPC_HAS_TYPE_EXTENDED}
  1690. function MinIntValue(const Data: array of Integer): Integer;
  1691. var
  1692. I: SizeInt;
  1693. begin
  1694. Result := Data[Low(Data)];
  1695. For I := Succ(Low(Data)) To High(Data) Do
  1696. If Data[I] < Result Then Result := Data[I];
  1697. end;
  1698. function MaxIntValue(const Data: array of Integer): Integer;
  1699. var
  1700. I: SizeInt;
  1701. begin
  1702. Result := Data[Low(Data)];
  1703. For I := Succ(Low(Data)) To High(Data) Do
  1704. If Data[I] > Result Then Result := Data[I];
  1705. end;
  1706. function MinValue(const Data: array of Integer): Integer; inline;
  1707. begin
  1708. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  1709. end;
  1710. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  1711. var
  1712. I: SizeInt;
  1713. begin
  1714. Result := Data[0];
  1715. For I := 1 To N-1 do
  1716. If Data[I] < Result Then Result := Data[I];
  1717. end;
  1718. function MaxValue(const Data: array of Integer): Integer; inline;
  1719. begin
  1720. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  1721. end;
  1722. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  1723. var
  1724. i : SizeInt;
  1725. begin
  1726. { get an initial value }
  1727. maxvalue:=data[0];
  1728. for i:=1 to N-1 do
  1729. if data[i]>maxvalue then
  1730. maxvalue:=data[i];
  1731. end;
  1732. {$ifdef FPC_HAS_TYPE_SINGLE}
  1733. function minvalue(const data : array of Single) : Single; inline;
  1734. begin
  1735. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  1736. end;
  1737. function minvalue(const data : PSingle; Const N : Integer) : Single;
  1738. var
  1739. i : SizeInt;
  1740. begin
  1741. { get an initial value }
  1742. minvalue:=data[0];
  1743. for i:=1 to N-1 do
  1744. if data[i]<minvalue then
  1745. minvalue:=data[i];
  1746. end;
  1747. function maxvalue(const data : array of Single) : Single; inline;
  1748. begin
  1749. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  1750. end;
  1751. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  1752. var
  1753. i : SizeInt;
  1754. begin
  1755. { get an initial value }
  1756. maxvalue:=data[0];
  1757. for i:=1 to N-1 do
  1758. if data[i]>maxvalue then
  1759. maxvalue:=data[i];
  1760. end;
  1761. {$endif FPC_HAS_TYPE_SINGLE}
  1762. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1763. function minvalue(const data : array of Double) : Double; inline;
  1764. begin
  1765. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  1766. end;
  1767. function minvalue(const data : PDouble; Const N : Integer) : Double;
  1768. var
  1769. i : SizeInt;
  1770. begin
  1771. { get an initial value }
  1772. minvalue:=data[0];
  1773. for i:=1 to N-1 do
  1774. if data[i]<minvalue then
  1775. minvalue:=data[i];
  1776. end;
  1777. function maxvalue(const data : array of Double) : Double; inline;
  1778. begin
  1779. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  1780. end;
  1781. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  1782. var
  1783. i : SizeInt;
  1784. begin
  1785. { get an initial value }
  1786. maxvalue:=data[0];
  1787. for i:=1 to N-1 do
  1788. if data[i]>maxvalue then
  1789. maxvalue:=data[i];
  1790. end;
  1791. {$endif FPC_HAS_TYPE_DOUBLE}
  1792. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1793. function minvalue(const data : array of Extended) : Extended; inline;
  1794. begin
  1795. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  1796. end;
  1797. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  1798. var
  1799. i : SizeInt;
  1800. begin
  1801. { get an initial value }
  1802. minvalue:=data[0];
  1803. for i:=1 to N-1 do
  1804. if data[i]<minvalue then
  1805. minvalue:=data[i];
  1806. end;
  1807. function maxvalue(const data : array of Extended) : Extended; inline;
  1808. begin
  1809. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  1810. end;
  1811. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  1812. var
  1813. i : SizeInt;
  1814. begin
  1815. { get an initial value }
  1816. maxvalue:=data[0];
  1817. for i:=1 to N-1 do
  1818. if data[i]>maxvalue then
  1819. maxvalue:=data[i];
  1820. end;
  1821. {$endif FPC_HAS_TYPE_EXTENDED}
  1822. function Min(a, b: Integer): Integer;inline;
  1823. begin
  1824. if a < b then
  1825. Result := a
  1826. else
  1827. Result := b;
  1828. end;
  1829. function Max(a, b: Integer): Integer;inline;
  1830. begin
  1831. if a > b then
  1832. Result := a
  1833. else
  1834. Result := b;
  1835. end;
  1836. {
  1837. function Min(a, b: Cardinal): Cardinal;inline;
  1838. begin
  1839. if a < b then
  1840. Result := a
  1841. else
  1842. Result := b;
  1843. end;
  1844. function Max(a, b: Cardinal): Cardinal;inline;
  1845. begin
  1846. if a > b then
  1847. Result := a
  1848. else
  1849. Result := b;
  1850. end;
  1851. }
  1852. function Min(a, b: Int64): Int64;inline;
  1853. begin
  1854. if a < b then
  1855. Result := a
  1856. else
  1857. Result := b;
  1858. end;
  1859. function Max(a, b: Int64): Int64;inline;
  1860. begin
  1861. if a > b then
  1862. Result := a
  1863. else
  1864. Result := b;
  1865. end;
  1866. function Min(a, b: QWord): QWord; inline;
  1867. begin
  1868. if a < b then
  1869. Result := a
  1870. else
  1871. Result := b;
  1872. end;
  1873. function Max(a, b: QWord): Qword;inline;
  1874. begin
  1875. if a > b then
  1876. Result := a
  1877. else
  1878. Result := b;
  1879. end;
  1880. {$ifdef FPC_HAS_TYPE_SINGLE}
  1881. function Min(a, b: Single): Single;inline;
  1882. begin
  1883. if a < b then
  1884. Result := a
  1885. else
  1886. Result := b;
  1887. end;
  1888. function Max(a, b: Single): Single;inline;
  1889. begin
  1890. if a > b then
  1891. Result := a
  1892. else
  1893. Result := b;
  1894. end;
  1895. {$endif FPC_HAS_TYPE_SINGLE}
  1896. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1897. function Min(a, b: Double): Double;inline;
  1898. begin
  1899. if a < b then
  1900. Result := a
  1901. else
  1902. Result := b;
  1903. end;
  1904. function Max(a, b: Double): Double;inline;
  1905. begin
  1906. if a > b then
  1907. Result := a
  1908. else
  1909. Result := b;
  1910. end;
  1911. {$endif FPC_HAS_TYPE_DOUBLE}
  1912. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1913. function Min(a, b: Extended): Extended;inline;
  1914. begin
  1915. if a < b then
  1916. Result := a
  1917. else
  1918. Result := b;
  1919. end;
  1920. function Max(a, b: Extended): Extended;inline;
  1921. begin
  1922. if a > b then
  1923. Result := a
  1924. else
  1925. Result := b;
  1926. end;
  1927. {$endif FPC_HAS_TYPE_EXTENDED}
  1928. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  1929. begin
  1930. Result:=(AValue>=AMin) and (AValue<=AMax);
  1931. end;
  1932. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  1933. begin
  1934. Result:=(AValue>=AMin) and (AValue<=AMax);
  1935. end;
  1936. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1937. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  1938. begin
  1939. Result:=(AValue>=AMin) and (AValue<=AMax);
  1940. end;
  1941. {$endif FPC_HAS_TYPE_DOUBLE}
  1942. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  1943. begin
  1944. Result:=AValue;
  1945. If Result<AMin then
  1946. Result:=AMin;
  1947. if Result>AMax then
  1948. Result:=AMax;
  1949. end;
  1950. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  1951. begin
  1952. Result:=AValue;
  1953. If Result<AMin then
  1954. Result:=AMin;
  1955. if Result>AMax then
  1956. Result:=AMax;
  1957. end;
  1958. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1959. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  1960. begin
  1961. Result:=AValue;
  1962. If Result<AMin then
  1963. Result:=AMin;
  1964. if Result>AMax then
  1965. Result:=AMax;
  1966. end;
  1967. {$endif FPC_HAS_TYPE_DOUBLE}
  1968. Const
  1969. EZeroResolution = 1E-16;
  1970. DZeroResolution = 1E-12;
  1971. SZeroResolution = 1E-4;
  1972. function IsZero(const A: Single; Epsilon: Single): Boolean;
  1973. begin
  1974. if (Epsilon=0) then
  1975. Epsilon:=SZeroResolution;
  1976. Result:=Abs(A)<=Epsilon;
  1977. end;
  1978. function IsZero(const A: Single): Boolean;inline;
  1979. begin
  1980. Result:=IsZero(A,single(SZeroResolution));
  1981. end;
  1982. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1983. function IsZero(const A: Double; Epsilon: Double): Boolean;
  1984. begin
  1985. if (Epsilon=0) then
  1986. Epsilon:=DZeroResolution;
  1987. Result:=Abs(A)<=Epsilon;
  1988. end;
  1989. function IsZero(const A: Double): Boolean;inline;
  1990. begin
  1991. Result:=IsZero(A,DZeroResolution);
  1992. end;
  1993. {$endif FPC_HAS_TYPE_DOUBLE}
  1994. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1995. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  1996. begin
  1997. if (Epsilon=0) then
  1998. Epsilon:=EZeroResolution;
  1999. Result:=Abs(A)<=Epsilon;
  2000. end;
  2001. function IsZero(const A: Extended): Boolean;inline;
  2002. begin
  2003. Result:=IsZero(A,EZeroResolution);
  2004. end;
  2005. {$endif FPC_HAS_TYPE_EXTENDED}
  2006. type
  2007. TSplitDouble = packed record
  2008. cards: Array[0..1] of cardinal;
  2009. end;
  2010. TSplitExtended = packed record
  2011. cards: Array[0..1] of cardinal;
  2012. w: word;
  2013. end;
  2014. function IsNan(const d : Single): Boolean; overload;
  2015. begin
  2016. result:=(longword(d) and $7fffffff)>$7f800000;
  2017. end;
  2018. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2019. function IsNan(const d : Double): Boolean;
  2020. var
  2021. fraczero, expMaximal: boolean;
  2022. begin
  2023. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2024. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  2025. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  2026. (TSplitDouble(d).cards[1] = 0);
  2027. {$else FPC_BIG_ENDIAN}
  2028. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  2029. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  2030. (TSplitDouble(d).cards[0] = 0);
  2031. {$endif FPC_BIG_ENDIAN}
  2032. Result:=expMaximal and not(fraczero);
  2033. end;
  2034. {$endif FPC_HAS_TYPE_DOUBLE}
  2035. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2036. function IsNan(const d : Extended): Boolean; overload;
  2037. var
  2038. fraczero, expMaximal: boolean;
  2039. begin
  2040. {$ifdef FPC_BIG_ENDIAN}
  2041. {$error no support for big endian extended type yet}
  2042. {$else FPC_BIG_ENDIAN}
  2043. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  2044. fraczero := (TSplitExtended(d).cards[0] = 0) and
  2045. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  2046. {$endif FPC_BIG_ENDIAN}
  2047. Result:=expMaximal and not(fraczero);
  2048. end;
  2049. {$endif FPC_HAS_TYPE_EXTENDED}
  2050. function IsInfinite(const d : Single): Boolean; overload;
  2051. begin
  2052. result:=(longword(d) and $7fffffff)=$7f800000;
  2053. end;
  2054. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2055. function IsInfinite(const d : Double): Boolean; overload;
  2056. var
  2057. fraczero, expMaximal: boolean;
  2058. begin
  2059. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2060. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  2061. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  2062. (TSplitDouble(d).cards[1] = 0);
  2063. {$else FPC_BIG_ENDIAN}
  2064. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  2065. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  2066. (TSplitDouble(d).cards[0] = 0);
  2067. {$endif FPC_BIG_ENDIAN}
  2068. Result:=expMaximal and fraczero;
  2069. end;
  2070. {$endif FPC_HAS_TYPE_DOUBLE}
  2071. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2072. function IsInfinite(const d : Extended): Boolean; overload;
  2073. var
  2074. fraczero, expMaximal: boolean;
  2075. begin
  2076. {$ifdef FPC_BIG_ENDIAN}
  2077. {$error no support for big endian extended type yet}
  2078. {$else FPC_BIG_ENDIAN}
  2079. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  2080. fraczero := (TSplitExtended(d).cards[0] = 0) and
  2081. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  2082. {$endif FPC_BIG_ENDIAN}
  2083. Result:=expMaximal and fraczero;
  2084. end;
  2085. {$endif FPC_HAS_TYPE_EXTENDED}
  2086. function copysign(x,y: float): float;
  2087. begin
  2088. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  2089. {$error copysign not yet implemented for float128}
  2090. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  2091. TSplitExtended(x).w:=(TSplitExtended(x).w and $7fff) or (TSplitExtended(y).w and $8000);
  2092. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  2093. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2094. TSplitDouble(x).cards[0]:=(TSplitDouble(x).cards[0] and $7fffffff) or (TSplitDouble(y).cards[0] and longword($80000000));
  2095. {$else}
  2096. TSplitDouble(x).cards[1]:=(TSplitDouble(x).cards[1] and $7fffffff) or (TSplitDouble(y).cards[1] and longword($80000000));
  2097. {$endif}
  2098. {$else}
  2099. longword(x):=longword(x and $7fffffff) or (longword(y) and longword($80000000));
  2100. {$endif}
  2101. result:=x;
  2102. end;
  2103. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2104. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  2105. begin
  2106. if (Epsilon=0) then
  2107. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  2108. if (A>B) then
  2109. Result:=((A-B)<=Epsilon)
  2110. else
  2111. Result:=((B-A)<=Epsilon);
  2112. end;
  2113. function SameValue(const A, B: Extended): Boolean;inline;
  2114. begin
  2115. Result:=SameValue(A,B,0.0);
  2116. end;
  2117. {$endif FPC_HAS_TYPE_EXTENDED}
  2118. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2119. function SameValue(const A, B: Double): Boolean;inline;
  2120. begin
  2121. Result:=SameValue(A,B,0.0);
  2122. end;
  2123. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  2124. begin
  2125. if (Epsilon=0) then
  2126. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  2127. if (A>B) then
  2128. Result:=((A-B)<=Epsilon)
  2129. else
  2130. Result:=((B-A)<=Epsilon);
  2131. end;
  2132. {$endif FPC_HAS_TYPE_DOUBLE}
  2133. function SameValue(const A, B: Single): Boolean;inline;
  2134. begin
  2135. Result:=SameValue(A,B,0);
  2136. end;
  2137. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  2138. begin
  2139. if (Epsilon=0) then
  2140. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  2141. if (A>B) then
  2142. Result:=((A-B)<=Epsilon)
  2143. else
  2144. Result:=((B-A)<=Epsilon);
  2145. end;
  2146. // Some CPUs probably allow a faster way of doing this in a single operation...
  2147. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  2148. {$ifndef FPC_MATH_HAS_DIVMOD}
  2149. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  2150. begin
  2151. if Dividend < 0 then
  2152. begin
  2153. { Use DivMod with >=0 dividend }
  2154. Dividend:=-Dividend;
  2155. { The documented behavior of Pascal's div/mod operators and DivMod
  2156. on negative dividends is to return Result closer to zero and
  2157. a negative Remainder. Which means that we can just negate both
  2158. Result and Remainder, and all it's Ok. }
  2159. Result:=-(Dividend Div Divisor);
  2160. Remainder:=-(Dividend+(Result*Divisor));
  2161. end
  2162. else
  2163. begin
  2164. Result:=Dividend Div Divisor;
  2165. Remainder:=Dividend-(Result*Divisor);
  2166. end;
  2167. end;
  2168. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  2169. begin
  2170. if Dividend < 0 then
  2171. begin
  2172. { Use DivMod with >=0 dividend }
  2173. Dividend:=-Dividend;
  2174. { The documented behavior of Pascal's div/mod operators and DivMod
  2175. on negative dividends is to return Result closer to zero and
  2176. a negative Remainder. Which means that we can just negate both
  2177. Result and Remainder, and all it's Ok. }
  2178. Result:=-(Dividend Div Divisor);
  2179. Remainder:=-(Dividend+(Result*Divisor));
  2180. end
  2181. else
  2182. begin
  2183. Result:=Dividend Div Divisor;
  2184. Remainder:=Dividend-(Result*Divisor);
  2185. end;
  2186. end;
  2187. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  2188. begin
  2189. Result:=Dividend Div Divisor;
  2190. Remainder:=Dividend-(Result*Divisor);
  2191. end;
  2192. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  2193. begin
  2194. if Dividend < 0 then
  2195. begin
  2196. { Use DivMod with >=0 dividend }
  2197. Dividend:=-Dividend;
  2198. { The documented behavior of Pascal's div/mod operators and DivMod
  2199. on negative dividends is to return Result closer to zero and
  2200. a negative Remainder. Which means that we can just negate both
  2201. Result and Remainder, and all it's Ok. }
  2202. Result:=-(Dividend Div Divisor);
  2203. Remainder:=-(Dividend+(Result*Divisor));
  2204. end
  2205. else
  2206. begin
  2207. Result:=Dividend Div Divisor;
  2208. Remainder:=Dividend-(Result*Divisor);
  2209. end;
  2210. end;
  2211. {$endif FPC_MATH_HAS_DIVMOD}
  2212. { Floating point modulo}
  2213. {$ifdef FPC_HAS_TYPE_SINGLE}
  2214. function FMod(const a, b: Single): Single;inline;overload;
  2215. begin
  2216. result:= a-b * Int(a/b);
  2217. end;
  2218. {$endif FPC_HAS_TYPE_SINGLE}
  2219. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2220. function FMod(const a, b: Double): Double;inline;overload;
  2221. begin
  2222. result:= a-b * Int(a/b);
  2223. end;
  2224. {$endif FPC_HAS_TYPE_DOUBLE}
  2225. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2226. function FMod(const a, b: Extended): Extended;inline;overload;
  2227. begin
  2228. result:= a-b * Int(a/b);
  2229. end;
  2230. {$endif FPC_HAS_TYPE_EXTENDED}
  2231. operator mod(const a,b:float) c:float;inline;
  2232. begin
  2233. c:= a-b * Int(a/b);
  2234. if SameValue(abs(c),abs(b)) then
  2235. c:=0.0;
  2236. end;
  2237. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  2238. begin
  2239. if val then result:=iftrue else result:=iffalse;
  2240. end;
  2241. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  2242. begin
  2243. if val then result:=iftrue else result:=iffalse;
  2244. end;
  2245. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  2246. begin
  2247. if val then result:=iftrue else result:=iffalse;
  2248. end;
  2249. // dilemma here. asm can do the two comparisons in one go?
  2250. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  2251. function CompareValue(const A, B : Integer): TValueRelationship;
  2252. begin
  2253. result:=GreaterThanValue;
  2254. if a=b then
  2255. result:=EqualsValue
  2256. else
  2257. if a<b then
  2258. result:=LessThanValue;
  2259. end;
  2260. function CompareValue(const A, B: Int64): TValueRelationship;
  2261. begin
  2262. result:=GreaterThanValue;
  2263. if a=b then
  2264. result:=EqualsValue
  2265. else
  2266. if a<b then
  2267. result:=LessThanValue;
  2268. end;
  2269. function CompareValue(const A, B: QWord): TValueRelationship;
  2270. begin
  2271. result:=GreaterThanValue;
  2272. if a=b then
  2273. result:=EqualsValue
  2274. else
  2275. if a<b then
  2276. result:=LessThanValue;
  2277. end;
  2278. {$ifdef FPC_HAS_TYPE_SINGLE}
  2279. function CompareValue(const A, B: Single; delta: Single = 0.0): TValueRelationship;
  2280. begin
  2281. result:=GreaterThanValue;
  2282. if abs(a-b)<=delta then
  2283. result:=EqualsValue
  2284. else
  2285. if a<b then
  2286. result:=LessThanValue;
  2287. end;
  2288. {$endif}
  2289. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2290. function CompareValue(const A, B: Double; delta: Double = 0.0): TValueRelationship;
  2291. begin
  2292. result:=GreaterThanValue;
  2293. if abs(a-b)<=delta then
  2294. result:=EqualsValue
  2295. else
  2296. if a<b then
  2297. result:=LessThanValue;
  2298. end;
  2299. {$endif}
  2300. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2301. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  2302. begin
  2303. result:=GreaterThanValue;
  2304. if abs(a-b)<=delta then
  2305. result:=EqualsValue
  2306. else
  2307. if a<b then
  2308. result:=LessThanValue;
  2309. end;
  2310. {$endif}
  2311. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2312. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  2313. var
  2314. RV : Double;
  2315. begin
  2316. RV:=IntPower(10,Digits);
  2317. Result:=Round(AValue/RV)*RV;
  2318. end;
  2319. {$endif}
  2320. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2321. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  2322. var
  2323. RV : Extended;
  2324. begin
  2325. RV:=IntPower(10,Digits);
  2326. Result:=Round(AValue/RV)*RV;
  2327. end;
  2328. {$endif}
  2329. {$ifdef FPC_HAS_TYPE_SINGLE}
  2330. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  2331. var
  2332. RV : Single;
  2333. begin
  2334. RV:=IntPower(10,Digits);
  2335. Result:=Round(AValue/RV)*RV;
  2336. end;
  2337. {$endif}
  2338. {$ifdef FPC_HAS_TYPE_SINGLE}
  2339. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  2340. var
  2341. RV : Single;
  2342. begin
  2343. RV := IntPower(10, -Digits);
  2344. if AValue < 0 then
  2345. Result := Int((AValue*RV) - 0.5)/RV
  2346. else
  2347. Result := Int((AValue*RV) + 0.5)/RV;
  2348. end;
  2349. {$endif}
  2350. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2351. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  2352. var
  2353. RV : Double;
  2354. begin
  2355. RV := IntPower(10, -Digits);
  2356. if AValue < 0 then
  2357. Result := Int((AValue*RV) - 0.5)/RV
  2358. else
  2359. Result := Int((AValue*RV) + 0.5)/RV;
  2360. end;
  2361. {$endif}
  2362. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2363. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  2364. var
  2365. RV : Extended;
  2366. begin
  2367. RV := IntPower(10, -Digits);
  2368. if AValue < 0 then
  2369. Result := Int((AValue*RV) - 0.5)/RV
  2370. else
  2371. Result := Int((AValue*RV) + 0.5)/RV;
  2372. end;
  2373. {$endif}
  2374. function RandomFrom(const AValues: array of Double): Double; overload;
  2375. begin
  2376. result:=AValues[random(High(AValues)+1)];
  2377. end;
  2378. function RandomFrom(const AValues: array of Integer): Integer; overload;
  2379. begin
  2380. result:=AValues[random(High(AValues)+1)];
  2381. end;
  2382. function RandomFrom(const AValues: array of Int64): Int64; overload;
  2383. begin
  2384. result:=AValues[random(High(AValues)+1)];
  2385. end;
  2386. {$if FPC_FULLVERSION >=30101}
  2387. generic function RandomFrom<T>(const AValues:array of T):T;
  2388. begin
  2389. result:=AValues[random(High(AValues)+1)];
  2390. end;
  2391. {$endif}
  2392. function FutureValue(ARate: Float; NPeriods: Integer;
  2393. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  2394. var
  2395. q, qn, factor: Float;
  2396. begin
  2397. if ARate = 0 then
  2398. Result := -APresentValue - APayment * NPeriods
  2399. else begin
  2400. q := 1.0 + ARate;
  2401. qn := power(q, NPeriods);
  2402. factor := (qn - 1) / (q - 1);
  2403. if APaymentTime = ptStartOfPeriod then
  2404. factor := factor * q;
  2405. Result := -(APresentValue * qn + APayment*factor);
  2406. end;
  2407. end;
  2408. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  2409. APaymentTime: TPaymentTime): Float;
  2410. { The interest rate cannot be calculated analytically. We solve the equation
  2411. numerically by means of the Newton method:
  2412. - guess value for the interest reate
  2413. - calculate at which interest rate the tangent of the curve fv(rate)
  2414. (straight line!) has the requested future vale.
  2415. - use this rate for the next iteration. }
  2416. const
  2417. DELTA = 0.001;
  2418. EPS = 1E-9; // required precision of interest rate (after typ. 6 iterations)
  2419. MAXIT = 20; // max iteration count to protect agains non-convergence
  2420. var
  2421. r1, r2, dr: Float;
  2422. fv1, fv2: Float;
  2423. iteration: Integer;
  2424. begin
  2425. iteration := 0;
  2426. r1 := 0.05; // inital guess
  2427. repeat
  2428. r2 := r1 + DELTA;
  2429. fv1 := FutureValue(r1, NPeriods, APayment, APresentValue, APaymentTime);
  2430. fv2 := FutureValue(r2, NPeriods, APayment, APresentValue, APaymentTime);
  2431. dr := (AFutureValue - fv1) / (fv2 - fv1) * delta; // tangent at fv(r)
  2432. r1 := r1 + dr; // next guess
  2433. inc(iteration);
  2434. until (abs(dr) < EPS) or (iteration >= MAXIT);
  2435. Result := r1;
  2436. end;
  2437. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  2438. APaymentTime: TPaymentTime): Float;
  2439. { Solve the cash flow equation (1) for q^n and take the logarithm }
  2440. var
  2441. q, x1, x2: Float;
  2442. begin
  2443. if ARate = 0 then
  2444. Result := -(APresentValue + AFutureValue) / APayment
  2445. else begin
  2446. q := 1.0 + ARate;
  2447. if APaymentTime = ptStartOfPeriod then
  2448. APayment := APayment * q;
  2449. x1 := APayment - AFutureValue * ARate;
  2450. x2 := APayment + APresentValue * ARate;
  2451. if (x2 = 0) // we have to divide by x2
  2452. or (sign(x1) * sign(x2) < 0) // the argument of the log is negative
  2453. then
  2454. Result := Infinity
  2455. else begin
  2456. Result := ln(x1/x2) / ln(q);
  2457. end;
  2458. end;
  2459. end;
  2460. function Payment(ARate: Float; NPeriods: Integer;
  2461. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  2462. var
  2463. q, qn, factor: Float;
  2464. begin
  2465. if ARate = 0 then
  2466. Result := -(AFutureValue + APresentValue) / NPeriods
  2467. else begin
  2468. q := 1.0 + ARate;
  2469. qn := power(q, NPeriods);
  2470. factor := (qn - 1) / (q - 1);
  2471. if APaymentTime = ptStartOfPeriod then
  2472. factor := factor * q;
  2473. Result := -(AFutureValue + APresentValue * qn) / factor;
  2474. end;
  2475. end;
  2476. function PresentValue(ARate: Float; NPeriods: Integer;
  2477. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  2478. var
  2479. q, qn, factor: Float;
  2480. begin
  2481. if ARate = 0.0 then
  2482. Result := -AFutureValue - APayment * NPeriods
  2483. else begin
  2484. q := 1.0 + ARate;
  2485. qn := power(q, NPeriods);
  2486. factor := (qn - 1) / (q - 1);
  2487. if APaymentTime = ptStartOfPeriod then
  2488. factor := factor * q;
  2489. Result := -(AFutureValue + APayment*factor) / qn;
  2490. end;
  2491. end;
  2492. {$else}
  2493. implementation
  2494. {$endif FPUNONE}
  2495. end.