math.pp 71 KB

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