math.pp 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931
  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. {$IFNDEF FPC_DOTTEDUNITS}
  40. unit Math;
  41. {$ENDIF FPC_DOTTEDUNITS}
  42. interface
  43. {$ifndef FPUNONE}
  44. {$IFDEF FPC_DOTTEDUNITS}
  45. uses
  46. System.SysUtils, System.Types;
  47. {$ELSE FPC_DOTTEDUNITS}
  48. uses
  49. sysutils, types;
  50. {$ENDIF FPC_DOTTEDUNITS}
  51. {$IFDEF FPDOC_MATH}
  52. Type
  53. Float = MaxFloatType;
  54. Const
  55. MinFloat = 0;
  56. MaxFloat = 0;
  57. {$ENDIF}
  58. { Ranges of the IEEE floating point types, including denormals }
  59. {$ifdef FPC_HAS_TYPE_SINGLE}
  60. const
  61. { values according to
  62. https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Single-precision_examples
  63. }
  64. MinSingle = 1.1754943508e-38;
  65. MaxSingle = 3.4028234664e+38;
  66. {$endif FPC_HAS_TYPE_SINGLE}
  67. {$ifdef FPC_HAS_TYPE_DOUBLE}
  68. const
  69. { values according to
  70. https://en.wikipedia.org/wiki/Double-precision_floating-point_format#Double-precision_examples
  71. }
  72. MinDouble = 2.2250738585072014e-308;
  73. MaxDouble = 1.7976931348623157e+308;
  74. {$endif FPC_HAS_TYPE_DOUBLE}
  75. {$ifdef FPC_HAS_TYPE_EXTENDED}
  76. const
  77. MinExtended = 3.4e-4932;
  78. MaxExtended = 1.1e+4932;
  79. {$endif FPC_HAS_TYPE_EXTENDED}
  80. {$ifdef FPC_HAS_TYPE_COMP}
  81. const
  82. MinComp = -9.223372036854775807e+18;
  83. MaxComp = 9.223372036854775807e+18;
  84. {$endif FPC_HAS_TYPE_COMP}
  85. { the original delphi functions use extended as argument, }
  86. { but I would prefer double, because 8 bytes is a very }
  87. { natural size for the processor }
  88. { WARNING : changing float type will }
  89. { break all assembler code PM }
  90. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  91. type
  92. Float = Float128;
  93. const
  94. MinFloat = MinFloat128;
  95. MaxFloat = MaxFloat128;
  96. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  97. type
  98. Float = extended;
  99. const
  100. MinFloat = MinExtended;
  101. MaxFloat = MaxExtended;
  102. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  103. type
  104. Float = double;
  105. const
  106. MinFloat = MinDouble;
  107. MaxFloat = MaxDouble;
  108. {$elseif defined(FPC_HAS_TYPE_SINGLE)}
  109. type
  110. Float = single;
  111. const
  112. MinFloat = MinSingle;
  113. MaxFloat = MaxSingle;
  114. {$else}
  115. {$fatal At least one floating point type must be supported}
  116. {$endif}
  117. type
  118. PFloat = ^Float;
  119. PInteger = ObjPas.PInteger;
  120. TPaymentTime = (ptEndOfPeriod,ptStartOfPeriod);
  121. EInvalidArgument = class(ematherror);
  122. {$IFDEF FPC_DOTTEDUNITS}
  123. TValueRelationship = System.Types.TValueRelationship;
  124. {$ELSE FPC_DOTTEDUNITS}
  125. TValueRelationship = types.TValueRelationship;
  126. {$ENDIF FPC_DOTTEDUNITS}
  127. const
  128. {$IFDEF FPC_DOTTEDUNITS}
  129. EqualsValue = System.Types.EqualsValue;
  130. LessThanValue = System.Types.LessThanValue;
  131. GreaterThanValue = System.Types.GreaterThanValue;
  132. {$ELSE FPC_DOTTEDUNITS}
  133. EqualsValue = types.EqualsValue;
  134. LessThanValue = types.LessThanValue;
  135. GreaterThanValue = types.GreaterThanValue;
  136. {$ENDIF FPC_DOTTEDUNITS}
  137. {$push}
  138. {$R-}
  139. {$Q-}
  140. NaN = 0.0/0.0;
  141. Infinity = 1.0/0.0;
  142. NegInfinity = -1.0/0.0;
  143. {$pop}
  144. {$IFDEF FPDOC_MATH}
  145. // This must be after the above defines.
  146. {$DEFINE FPC_HAS_TYPE_SINGLE}
  147. {$DEFINE FPC_HAS_TYPE_DOUBLE}
  148. {$DEFINE FPC_HAS_TYPE_EXTENDED}
  149. {$DEFINE FPC_HAS_TYPE_COMP}
  150. {$ENDIF}
  151. { Min/max determination }
  152. function MinIntValue(const Data: array of Integer): Integer;
  153. function MaxIntValue(const Data: array of Integer): Integer;
  154. { Extra, not present in Delphi, but used frequently }
  155. function Min(a, b: Integer): Integer;inline; overload;
  156. function Max(a, b: Integer): Integer;inline; overload;
  157. { this causes more trouble than it solves
  158. function Min(a, b: Cardinal): Cardinal; overload;
  159. function Max(a, b: Cardinal): Cardinal; overload;
  160. }
  161. function Min(a, b: Int64): Int64;inline; overload;
  162. function Max(a, b: Int64): Int64;inline; overload;
  163. function Min(a, b: QWord): QWord;inline; overload;
  164. function Max(a, b: QWord): QWord;inline; overload;
  165. {$ifdef FPC_HAS_TYPE_SINGLE}
  166. function Min(a, b: Single): Single;inline; overload;
  167. function Max(a, b: Single): Single;inline; overload;
  168. {$endif FPC_HAS_TYPE_SINGLE}
  169. {$ifdef FPC_HAS_TYPE_DOUBLE}
  170. function Min(a, b: Double): Double;inline; overload;
  171. function Max(a, b: Double): Double;inline; overload;
  172. {$endif FPC_HAS_TYPE_DOUBLE}
  173. {$ifdef FPC_HAS_TYPE_EXTENDED}
  174. function Min(a, b: Extended): Extended;inline; overload;
  175. function Max(a, b: Extended): Extended;inline; overload;
  176. {$endif FPC_HAS_TYPE_EXTENDED}
  177. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline; overload;
  178. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline; overload;
  179. {$ifdef FPC_HAS_TYPE_DOUBLE}
  180. function InRange(const AValue, AMin, AMax: Double): Boolean;inline; overload;
  181. {$endif FPC_HAS_TYPE_DOUBLE}
  182. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline; overload;
  183. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline; overload;
  184. {$ifdef FPC_HAS_TYPE_DOUBLE}
  185. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline; overload;
  186. {$endif FPC_HAS_TYPE_DOUBLE}
  187. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  188. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  189. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  190. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  191. { Floating point modulo}
  192. {$ifdef FPC_HAS_TYPE_SINGLE}
  193. function FMod(const a, b: Single): Single;inline;overload;
  194. {$endif FPC_HAS_TYPE_SINGLE}
  195. {$ifdef FPC_HAS_TYPE_DOUBLE}
  196. function FMod(const a, b: Double): Double;inline;overload;
  197. {$endif FPC_HAS_TYPE_DOUBLE}
  198. {$ifdef FPC_HAS_TYPE_EXTENDED}
  199. function FMod(const a, b: Extended): Extended;inline;overload;
  200. {$endif FPC_HAS_TYPE_EXTENDED}
  201. operator mod(const a,b:float) c:float;inline;
  202. // Sign functions
  203. Type
  204. TValueSign = -1..1;
  205. const
  206. NegativeValue = Low(TValueSign);
  207. ZeroValue = 0;
  208. PositiveValue = High(TValueSign);
  209. function Sign(const AValue: Integer): TValueSign;inline; overload;
  210. function Sign(const AValue: Int64): TValueSign;inline; overload;
  211. {$ifdef FPC_HAS_TYPE_SINGLE}
  212. function Sign(const AValue: Single): TValueSign;inline; overload;
  213. {$endif}
  214. function Sign(const AValue: Double): TValueSign;inline; overload;
  215. {$ifdef FPC_HAS_TYPE_EXTENDED}
  216. function Sign(const AValue: Extended): TValueSign;inline; overload;
  217. {$endif}
  218. function IsZero(const A: Single; Epsilon: Single): Boolean; overload;
  219. function IsZero(const A: Single): Boolean;inline; overload;
  220. {$ifdef FPC_HAS_TYPE_DOUBLE}
  221. function IsZero(const A: Double; Epsilon: Double): Boolean; overload;
  222. function IsZero(const A: Double): Boolean;inline; overload;
  223. {$endif FPC_HAS_TYPE_DOUBLE}
  224. {$ifdef FPC_HAS_TYPE_EXTENDED}
  225. function IsZero(const A: Extended; Epsilon: Extended): Boolean; overload;
  226. function IsZero(const A: Extended): Boolean;inline; overload;
  227. {$endif FPC_HAS_TYPE_EXTENDED}
  228. function IsNan(const d : Single): Boolean; overload;
  229. {$ifdef FPC_HAS_TYPE_DOUBLE}
  230. function IsNan(const d : Double): Boolean; overload;
  231. {$endif FPC_HAS_TYPE_DOUBLE}
  232. {$ifdef FPC_HAS_TYPE_EXTENDED}
  233. function IsNan(const d : Extended): Boolean; overload;
  234. {$endif FPC_HAS_TYPE_EXTENDED}
  235. function IsInfinite(const d : Single): Boolean; overload;
  236. {$ifdef FPC_HAS_TYPE_DOUBLE}
  237. function IsInfinite(const d : Double): Boolean; overload;
  238. {$endif FPC_HAS_TYPE_DOUBLE}
  239. {$ifdef FPC_HAS_TYPE_EXTENDED}
  240. function IsInfinite(const d : Extended): Boolean; overload;
  241. {$endif FPC_HAS_TYPE_EXTENDED}
  242. {$ifdef FPC_HAS_TYPE_EXTENDED}
  243. function SameValue(const A, B: Extended): Boolean;inline; overload;
  244. {$endif}
  245. {$ifdef FPC_HAS_TYPE_DOUBLE}
  246. function SameValue(const A, B: Double): Boolean;inline; overload;
  247. {$endif}
  248. function SameValue(const A, B: Single): Boolean;inline; overload;
  249. {$ifdef FPC_HAS_TYPE_EXTENDED}
  250. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean; overload;
  251. {$endif}
  252. {$ifdef FPC_HAS_TYPE_DOUBLE}
  253. function SameValue(const A, B: Double; Epsilon: Double): Boolean; overload;
  254. {$endif}
  255. function SameValue(const A, B: Single; Epsilon: Single): Boolean; overload;
  256. type
  257. TRoundToRange = -37..37;
  258. {$ifdef FPC_HAS_TYPE_DOUBLE}
  259. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  260. {$endif}
  261. {$ifdef FPC_HAS_TYPE_EXTENDED}
  262. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  263. {$endif}
  264. {$ifdef FPC_HAS_TYPE_SINGLE}
  265. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  266. {$endif}
  267. {$ifdef FPC_HAS_TYPE_SINGLE}
  268. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  269. {$endif}
  270. {$ifdef FPC_HAS_TYPE_DOUBLE}
  271. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  272. {$endif}
  273. {$ifdef FPC_HAS_TYPE_EXTENDED}
  274. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  275. {$endif}
  276. { angle conversion }
  277. function DegToRad(deg : float) : float;inline;
  278. function RadToDeg(rad : float) : float;inline;
  279. function GradToRad(grad : float) : float;inline;
  280. function RadToGrad(rad : float) : float;inline;
  281. function DegToGrad(deg : float) : float;inline;
  282. function GradToDeg(grad : float) : float;inline;
  283. {$ifdef FPC_HAS_TYPE_SINGLE}
  284. function CycleToDeg(const Cycles: Single): Single;
  285. {$ENDIF}
  286. {$ifdef FPC_HAS_TYPE_DOUBLE}
  287. function CycleToDeg(const Cycles: Double): Double;
  288. {$ENDIF}
  289. {$ifdef FPC_HAS_TYPE_EXTENDED}
  290. function CycleToDeg(const Cycles: Extended): Extended;
  291. {$ENDIF}
  292. {$ifdef FPC_HAS_TYPE_SINGLE}
  293. function DegToCycle(const Degrees: Single): Single;
  294. {$ENDIF}
  295. {$ifdef FPC_HAS_TYPE_DOUBLE}
  296. function DegToCycle(const Degrees: Double): Double;
  297. {$ENDIF}
  298. {$ifdef FPC_HAS_TYPE_EXTENDED}
  299. function DegToCycle(const Degrees: Extended): Extended;
  300. {$ENDIF}
  301. {$ifdef FPC_HAS_TYPE_SINGLE}
  302. function CycleToGrad(const Cycles: Single): Single;
  303. {$ENDIF}
  304. {$ifdef FPC_HAS_TYPE_DOUBLE}
  305. function CycleToGrad(const Cycles: Double): Double;
  306. {$ENDIF}
  307. {$ifdef FPC_HAS_TYPE_EXTENDED}
  308. function CycleToGrad(const Cycles: Extended): Extended;
  309. {$ENDIF}
  310. {$ifdef FPC_HAS_TYPE_SINGLE}
  311. function GradToCycle(const Grads: Single): Single;
  312. {$ENDIF}
  313. {$ifdef FPC_HAS_TYPE_DOUBLE}
  314. function GradToCycle(const Grads: Double): Double;
  315. {$ENDIF}
  316. {$ifdef FPC_HAS_TYPE_EXTENDED}
  317. function GradToCycle(const Grads: Extended): Extended;
  318. {$ENDIF}
  319. {$ifdef FPC_HAS_TYPE_SINGLE}
  320. function CycleToRad(const Cycles: Single): Single;
  321. {$ENDIF}
  322. {$ifdef FPC_HAS_TYPE_DOUBLE}
  323. function CycleToRad(const Cycles: Double): Double;
  324. {$ENDIF}
  325. {$ifdef FPC_HAS_TYPE_EXTENDED}
  326. function CycleToRad(const Cycles: Extended): Extended;
  327. {$ENDIF}
  328. {$ifdef FPC_HAS_TYPE_SINGLE}
  329. function RadToCycle(const Rads: Single): Single;
  330. {$ENDIF}
  331. {$ifdef FPC_HAS_TYPE_DOUBLE}
  332. function RadToCycle(const Rads: Double): Double;
  333. {$ENDIF}
  334. {$ifdef FPC_HAS_TYPE_EXTENDED}
  335. function RadToCycle(const Rads: Extended): Extended;
  336. {$ENDIF}
  337. {$ifdef FPC_HAS_TYPE_SINGLE}
  338. Function DegNormalize(deg : single) : single; inline;
  339. {$ENDIF}
  340. {$ifdef FPC_HAS_TYPE_DOUBLE}
  341. Function DegNormalize(deg : double) : double; inline;
  342. {$ENDIF}
  343. {$ifdef FPC_HAS_TYPE_EXTENDED}
  344. Function DegNormalize(deg : extended) : extended; inline;
  345. {$ENDIF}
  346. { trigoniometric functions }
  347. function Tan(x : float) : float;
  348. function Cotan(x : float) : float;
  349. function Cot(x : float) : float; inline;
  350. {$ifdef FPC_HAS_TYPE_SINGLE}
  351. procedure SinCos(theta : single;out sinus,cosinus : single);
  352. {$endif}
  353. {$ifdef FPC_HAS_TYPE_DOUBLE}
  354. procedure SinCos(theta : double;out sinus,cosinus : double);
  355. {$endif}
  356. {$ifdef FPC_HAS_TYPE_EXTENDED}
  357. procedure SinCos(theta : extended;out sinus,cosinus : extended);
  358. {$endif}
  359. function Secant(x : float) : float; inline;
  360. function Cosecant(x : float) : float; inline;
  361. function Sec(x : float) : float; inline;
  362. function Csc(x : float) : float; inline;
  363. { inverse functions }
  364. {$ifdef FPC_HAS_TYPE_SINGLE}
  365. function ArcCos(x : Single) : Single;
  366. {$ENDIF}
  367. {$ifdef FPC_HAS_TYPE_DOUBLE}
  368. function ArcCos(x : Double) : Double;
  369. {$ENDIF}
  370. {$ifdef FPC_HAS_TYPE_EXTENDED}
  371. function ArcCos(x : Extended) : Extended;
  372. {$ENDIF}
  373. {$ifdef FPC_HAS_TYPE_SINGLE}
  374. function ArcSin(x : Single) : Single;
  375. {$ENDIF}
  376. {$ifdef FPC_HAS_TYPE_DOUBLE}
  377. function ArcSin(x : Double) : Double;
  378. {$ENDIF}
  379. {$ifdef FPC_HAS_TYPE_EXTENDED}
  380. function ArcSin(x : Extended) : Extended;
  381. {$ENDIF}
  382. { calculates arctan(y/x) and returns an angle in the correct quadrant }
  383. function ArcTan2(y,x : float) : float;
  384. { hyperbolic functions }
  385. {$ifdef FPC_HAS_TYPE_SINGLE}
  386. function cosh(x : Single) : Single;
  387. {$ENDIF}
  388. {$ifdef FPC_HAS_TYPE_DOUBLE}
  389. function cosh(x : Double) : Double;
  390. {$ENDIF}
  391. {$ifdef FPC_HAS_TYPE_EXTENDED}
  392. function cosh(x : Extended) : Extended;
  393. {$ENDIF}
  394. {$ifdef FPC_HAS_TYPE_SINGLE}
  395. function sinh(x : Single) : Single;
  396. {$ENDIF}
  397. {$ifdef FPC_HAS_TYPE_DOUBLE}
  398. function sinh(x : Double) : Double;
  399. {$ENDIF}
  400. {$ifdef FPC_HAS_TYPE_EXTENDED}
  401. function sinh(x : Extended) : Extended;
  402. {$ENDIF}
  403. {$ifdef FPC_HAS_TYPE_SINGLE}
  404. function tanh(x : Single) : Single;
  405. {$ENDIF}
  406. {$ifdef FPC_HAS_TYPE_DOUBLE}
  407. function tanh(x : Double) : Double;
  408. {$ENDIF}
  409. {$ifdef FPC_HAS_TYPE_EXTENDED}
  410. function tanh(x : Extended) : Extended;
  411. {$ENDIF}
  412. {$ifdef FPC_HAS_TYPE_SINGLE}
  413. function SecH(const X: Single): Single;
  414. {$ENDIF}
  415. {$ifdef FPC_HAS_TYPE_DOUBLE}
  416. function SecH(const X: Double): Double;
  417. {$ENDIF}
  418. {$ifdef FPC_HAS_TYPE_EXTENDED}
  419. function SecH(const X: Extended): Extended;
  420. {$ENDIF}
  421. {$ifdef FPC_HAS_TYPE_SINGLE}
  422. function CscH(const X: Single): Single;
  423. {$ENDIF}
  424. {$ifdef FPC_HAS_TYPE_DOUBLE}
  425. function CscH(const X: Double): Double;
  426. {$ENDIF}
  427. {$ifdef FPC_HAS_TYPE_EXTENDED}
  428. function CscH(const X: Extended): Extended;
  429. {$ENDIF}
  430. {$ifdef FPC_HAS_TYPE_SINGLE}
  431. function CotH(const X: Single): Single;
  432. {$ENDIF}
  433. {$ifdef FPC_HAS_TYPE_DOUBLE}
  434. function CotH(const X: Double): Double;
  435. {$ENDIF}
  436. {$ifdef FPC_HAS_TYPE_EXTENDED}
  437. function CotH(const X: Extended): Extended;
  438. {$ENDIF}
  439. { area functions }
  440. { delphi names: }
  441. function ArcCosH(x : float) : float;inline;
  442. function ArcSinH(x : float) : float;inline;
  443. function ArcTanH(x : float) : float;inline;
  444. { IMHO the function should be called as follows (FK) }
  445. function ArCosH(x : float) : float;
  446. function ArSinH(x : float) : float;
  447. function ArTanH(x : float) : float;
  448. {$ifdef FPC_HAS_TYPE_SINGLE}
  449. function ArcSec(X: Single): Single;
  450. {$ENDIF}
  451. {$ifdef FPC_HAS_TYPE_DOUBLE}
  452. function ArcSec(X: Double): Double;
  453. {$ENDIF}
  454. {$ifdef FPC_HAS_TYPE_EXTENDED}
  455. function ArcSec(X: Extended): Extended;
  456. {$ENDIF}
  457. {$ifdef FPC_HAS_TYPE_SINGLE}
  458. function ArcCsc(X: Single): Single;
  459. {$ENDIF}
  460. {$ifdef FPC_HAS_TYPE_DOUBLE}
  461. function ArcCsc(X: Double): Double;
  462. {$ENDIF}
  463. {$ifdef FPC_HAS_TYPE_EXTENDED}
  464. function ArcCsc(X: Extended): Extended;
  465. {$ENDIF}
  466. {$ifdef FPC_HAS_TYPE_SINGLE}
  467. function ArcCot(X: Single): Single;
  468. {$ENDIF}
  469. {$ifdef FPC_HAS_TYPE_DOUBLE}
  470. function ArcCot(X: Double): Double;
  471. {$ENDIF}
  472. {$ifdef FPC_HAS_TYPE_EXTENDED}
  473. function ArcCot(X: Extended): Extended;
  474. {$ENDIF}
  475. {$ifdef FPC_HAS_TYPE_SINGLE}
  476. function ArcSecH(X : Single): Single;
  477. {$ENDIF}
  478. {$ifdef FPC_HAS_TYPE_DOUBLE}
  479. function ArcSecH(X : Double): Double;
  480. {$ENDIF}
  481. {$ifdef FPC_HAS_TYPE_EXTENDED}
  482. function ArcSecH(X : Extended): Extended;
  483. {$ENDIF}
  484. {$ifdef FPC_HAS_TYPE_SINGLE}
  485. function ArcCscH(X: Single): Single;
  486. {$ENDIF}
  487. {$ifdef FPC_HAS_TYPE_DOUBLE}
  488. function ArcCscH(X: Double): Double;
  489. {$ENDIF}
  490. {$ifdef FPC_HAS_TYPE_EXTENDED}
  491. function ArcCscH(X: Extended): Extended;
  492. {$ENDIF}
  493. {$ifdef FPC_HAS_TYPE_SINGLE}
  494. function ArcCotH(X: Single): Single;
  495. {$ENDIF}
  496. {$ifdef FPC_HAS_TYPE_DOUBLE}
  497. function ArcCotH(X: Double): Double;
  498. {$ENDIF}
  499. {$ifdef FPC_HAS_TYPE_EXTENDED}
  500. function ArcCotH(X: Extended): Extended;
  501. {$ENDIF}
  502. { triangle functions }
  503. { returns the length of the hypotenuse of a right triangle }
  504. { if x and y are the other sides }
  505. function Hypot(x,y : float) : float;
  506. { logarithm functions }
  507. function Log10(x : float) : float;
  508. function Log2(x : float) : float;
  509. function LogN(n,x : float) : float;
  510. { returns natural logarithm of x+1, accurate for x values near zero }
  511. function LnXP1(x : float) : float;
  512. { exponential functions }
  513. function Power(base,exponent : float) : float;
  514. { base^exponent }
  515. function IntPower(base : float;exponent : longint) : float;
  516. operator ** (base,exponent : float) e: float; inline;
  517. operator ** (base,exponent : int64) res: int64;
  518. { number converting }
  519. { rounds x towards positive infinity }
  520. function Ceil(x : float) : Integer;
  521. function Ceil64(x: float): Int64;
  522. { rounds x towards negative infinity }
  523. function Floor(x : float) : Integer;
  524. function Floor64(x: float): Int64;
  525. { misc. functions }
  526. {$ifdef FPC_HAS_TYPE_SINGLE}
  527. { splits x into mantissa and exponent (to base 2) }
  528. procedure Frexp(X: single; out Mantissa: single; out Exponent: integer);
  529. { returns x*(2^p) }
  530. function Ldexp(X: single; p: Integer) : single;
  531. {$endif}
  532. {$ifdef FPC_HAS_TYPE_DOUBLE}
  533. procedure Frexp(X: double; out Mantissa: double; out Exponent: integer);
  534. function Ldexp(X: double; p: Integer) : double;
  535. {$endif}
  536. {$ifdef FPC_HAS_TYPE_EXTENDED}
  537. procedure Frexp(X: extended; out Mantissa: extended; out Exponent: integer);
  538. function Ldexp(X: extended; p: Integer) : extended;
  539. {$endif}
  540. { statistical functions }
  541. {$ifdef FPC_HAS_TYPE_SINGLE}
  542. function Mean(const data : array of Single) : float;
  543. function Sum(const data : array of Single) : float;inline;
  544. function Mean(const data : PSingle; Const N : longint) : float;
  545. function Sum(const data : PSingle; Const N : Longint) : float;
  546. {$endif FPC_HAS_TYPE_SINGLE}
  547. {$ifdef FPC_HAS_TYPE_DOUBLE}
  548. function Mean(const data : array of double) : float;inline;
  549. function Sum(const data : array of double) : float;inline;
  550. function Mean(const data : PDouble; Const N : longint) : float;
  551. function Sum(const data : PDouble; Const N : Longint) : float;
  552. {$endif FPC_HAS_TYPE_DOUBLE}
  553. {$ifdef FPC_HAS_TYPE_EXTENDED}
  554. function Mean(const data : array of Extended) : float;
  555. function Sum(const data : array of Extended) : float;inline;
  556. function Mean(const data : PExtended; Const N : longint) : float;
  557. function Sum(const data : PExtended; Const N : Longint) : float;
  558. {$endif FPC_HAS_TYPE_EXTENDED}
  559. function SumInt(const data : PInt64;Const N : longint) : Int64;
  560. function SumInt(const data : array of Int64) : Int64;inline;
  561. function Mean(const data : PInt64; const N : Longint):Float;
  562. function Mean(const data: array of Int64):Float;
  563. function SumInt(const data : PInteger; Const N : longint) : Int64;
  564. function SumInt(const data : array of Integer) : Int64;inline;
  565. function Mean(const data : PInteger; const N : Longint):Float;
  566. function Mean(const data: array of Integer):Float;
  567. {$ifdef FPC_HAS_TYPE_SINGLE}
  568. function SumOfSquares(const data : array of Single) : float;inline;
  569. function SumOfSquares(const data : PSingle; Const N : Integer) : float;
  570. { calculates the sum and the sum of squares of data }
  571. procedure SumsAndSquares(const data : array of Single;
  572. var sum,sumofsquares : float);inline;
  573. procedure SumsAndSquares(const data : PSingle; Const N : Integer;
  574. var sum,sumofsquares : float);
  575. {$endif FPC_HAS_TYPE_SINGLE}
  576. {$ifdef FPC_HAS_TYPE_DOUBLE}
  577. function SumOfSquares(const data : array of double) : float;inline;
  578. function SumOfSquares(const data : PDouble; Const N : Integer) : float;
  579. { calculates the sum and the sum of squares of data }
  580. procedure SumsAndSquares(const data : array of Double;
  581. var sum,sumofsquares : float);inline;
  582. procedure SumsAndSquares(const data : PDouble; Const N : Integer;
  583. var sum,sumofsquares : float);
  584. {$endif FPC_HAS_TYPE_DOUBLE}
  585. {$ifdef FPC_HAS_TYPE_EXTENDED}
  586. function SumOfSquares(const data : array of Extended) : float;inline;
  587. function SumOfSquares(const data : PExtended; Const N : Integer) : float;
  588. { calculates the sum and the sum of squares of data }
  589. procedure SumsAndSquares(const data : array of Extended;
  590. var sum,sumofsquares : float);inline;
  591. procedure SumsAndSquares(const data : PExtended; Const N : Integer;
  592. var sum,sumofsquares : float);
  593. {$endif FPC_HAS_TYPE_EXTENDED}
  594. {$ifdef FPC_HAS_TYPE_SINGLE}
  595. function MinValue(const data : array of Single) : Single;inline;
  596. function MinValue(const data : PSingle; Const N : Integer) : Single;
  597. function MaxValue(const data : array of Single) : Single;inline;
  598. function MaxValue(const data : PSingle; Const N : Integer) : Single;
  599. {$endif FPC_HAS_TYPE_SINGLE}
  600. {$ifdef FPC_HAS_TYPE_DOUBLE}
  601. function MinValue(const data : array of Double) : Double;inline;
  602. function MinValue(const data : PDouble; Const N : Integer) : Double;
  603. function MaxValue(const data : array of Double) : Double;inline;
  604. function MaxValue(const data : PDouble; Const N : Integer) : Double;
  605. {$endif FPC_HAS_TYPE_DOUBLE}
  606. {$ifdef FPC_HAS_TYPE_EXTENDED}
  607. function MinValue(const data : array of Extended) : Extended;inline;
  608. function MinValue(const data : PExtended; Const N : Integer) : Extended;
  609. function MaxValue(const data : array of Extended) : Extended;inline;
  610. function MaxValue(const data : PExtended; Const N : Integer) : Extended;
  611. {$endif FPC_HAS_TYPE_EXTENDED}
  612. function MinValue(const data : array of integer) : Integer;inline;
  613. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  614. function MaxValue(const data : array of integer) : Integer;inline;
  615. function MaxValue(const data : PInteger; Const N : Integer) : Integer;
  616. { returns random values with gaussian distribution }
  617. function RandG(mean,stddev : float) : float;
  618. function RandomRange(const aFrom, aTo: Integer): Integer;
  619. function RandomRange(const aFrom, aTo: Int64): Int64;
  620. {$ifdef FPC_HAS_TYPE_SINGLE}
  621. { calculates the standard deviation }
  622. function StdDev(const data : array of Single) : float;inline;
  623. function StdDev(const data : PSingle; Const N : Integer) : float;
  624. { calculates the mean and stddev }
  625. procedure MeanAndStdDev(const data : array of Single;
  626. var mean,stddev : float);inline;
  627. procedure MeanAndStdDev(const data : PSingle;
  628. Const N : Longint;var mean,stddev : float);
  629. function Variance(const data : array of Single) : float;inline;
  630. function TotalVariance(const data : array of Single) : float;inline;
  631. function Variance(const data : PSingle; Const N : Integer) : float;
  632. function TotalVariance(const data : PSingle; Const N : Integer) : float;
  633. { Population (aka uncorrected) variance and standard deviation }
  634. function PopnStdDev(const data : array of Single) : float;inline;
  635. function PopnStdDev(const data : PSingle; Const N : Integer) : float;
  636. function PopnVariance(const data : PSingle; Const N : Integer) : float;
  637. function PopnVariance(const data : array of Single) : float;inline;
  638. procedure MomentSkewKurtosis(const data : array of Single;
  639. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  640. procedure MomentSkewKurtosis(const data : PSingle; Const N : Integer;
  641. out m1,m2,m3,m4,skew,kurtosis : float);
  642. { geometrical function }
  643. { returns the euclidean L2 norm }
  644. function Norm(const data : array of Single) : float;inline;
  645. function Norm(const data : PSingle; Const N : Integer) : float;
  646. {$endif FPC_HAS_TYPE_SINGLE}
  647. {$ifdef FPC_HAS_TYPE_DOUBLE}
  648. { calculates the standard deviation }
  649. function StdDev(const data : array of Double) : float;inline;
  650. function StdDev(const data : PDouble; Const N : Integer) : float;
  651. { calculates the mean and stddev }
  652. procedure MeanAndStdDev(const data : array of Double;
  653. var mean,stddev : float);inline;
  654. procedure MeanAndStdDev(const data : PDouble;
  655. Const N : Longint;var mean,stddev : float);
  656. function Variance(const data : array of Double) : float;inline;
  657. function TotalVariance(const data : array of Double) : float;inline;
  658. function Variance(const data : PDouble; Const N : Integer) : float;
  659. function TotalVariance(const data : PDouble; Const N : Integer) : float;
  660. { Population (aka uncorrected) variance and standard deviation }
  661. function PopnStdDev(const data : array of Double) : float;inline;
  662. function PopnStdDev(const data : PDouble; Const N : Integer) : float;
  663. function PopnVariance(const data : PDouble; Const N : Integer) : float;
  664. function PopnVariance(const data : array of Double) : float;inline;
  665. procedure MomentSkewKurtosis(const data : array of Double;
  666. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  667. procedure MomentSkewKurtosis(const data : PDouble; Const N : Integer;
  668. out m1,m2,m3,m4,skew,kurtosis : float);
  669. { geometrical function }
  670. { returns the euclidean L2 norm }
  671. function Norm(const data : array of double) : float;inline;
  672. function Norm(const data : PDouble; Const N : Integer) : float;
  673. {$endif FPC_HAS_TYPE_DOUBLE}
  674. {$ifdef FPC_HAS_TYPE_EXTENDED}
  675. { calculates the standard deviation }
  676. function StdDev(const data : array of Extended) : float;inline;
  677. function StdDev(const data : PExtended; Const N : Integer) : float;
  678. { calculates the mean and stddev }
  679. procedure MeanAndStdDev(const data : array of Extended;
  680. var mean,stddev : float);inline;
  681. procedure MeanAndStdDev(const data : PExtended;
  682. Const N : Longint;var mean,stddev : float);
  683. function Variance(const data : array of Extended) : float;inline;
  684. function TotalVariance(const data : array of Extended) : float;inline;
  685. function Variance(const data : PExtended; Const N : Integer) : float;
  686. function TotalVariance(const data : PExtended; Const N : Integer) : float;
  687. { Population (aka uncorrected) variance and standard deviation }
  688. function PopnStdDev(const data : array of Extended) : float;inline;
  689. function PopnStdDev(const data : PExtended; Const N : Integer) : float;
  690. function PopnVariance(const data : PExtended; Const N : Integer) : float;
  691. function PopnVariance(const data : array of Extended) : float;inline;
  692. procedure MomentSkewKurtosis(const data : array of Extended;
  693. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  694. procedure MomentSkewKurtosis(const data : PExtended; Const N : Integer;
  695. out m1,m2,m3,m4,skew,kurtosis : float);
  696. { geometrical function }
  697. { returns the euclidean L2 norm }
  698. function Norm(const data : array of Extended) : float;inline;
  699. function Norm(const data : PExtended; Const N : Integer) : float;
  700. {$endif FPC_HAS_TYPE_EXTENDED}
  701. { Financial functions }
  702. function FutureValue(ARate: Float; NPeriods: Integer;
  703. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  704. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  705. APaymentTime: TPaymentTime): Float;
  706. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  707. APaymentTime: TPaymentTime): Float;
  708. function Payment(ARate: Float; NPeriods: Integer;
  709. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  710. function PresentValue(ARate: Float; NPeriods: Integer;
  711. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  712. { Misc functions }
  713. function IfThen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer; inline; overload;
  714. function IfThen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64; inline; overload;
  715. function IfThen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double; inline; overload;
  716. function CompareValue ( const A, B : Integer) : TValueRelationship; inline;
  717. function CompareValue ( const A, B : Int64) : TValueRelationship; inline;
  718. function CompareValue ( const A, B : QWord) : TValueRelationship; inline;
  719. {$ifdef FPC_HAS_TYPE_SINGLE}
  720. function CompareValue ( const A, B : Single; delta : Single = 0.0 ) : TValueRelationship; inline;
  721. {$endif}
  722. {$ifdef FPC_HAS_TYPE_DOUBLE}
  723. function CompareValue ( const A, B : Double; delta : Double = 0.0) : TValueRelationship; inline;
  724. {$endif}
  725. {$ifdef FPC_HAS_TYPE_EXTENDED}
  726. function CompareValue ( const A, B : Extended; delta : Extended = 0.0 ) : TValueRelationship; inline;
  727. {$endif}
  728. function RandomFrom(const AValues: array of Double): Double; overload;
  729. function RandomFrom(const AValues: array of Integer): Integer; overload;
  730. function RandomFrom(const AValues: array of Int64): Int64; overload;
  731. {$if FPC_FULLVERSION >=30101}
  732. generic function RandomFrom<T>(const AValues:array of T):T;
  733. {$endif}
  734. { cpu specific stuff }
  735. type
  736. TFPURoundingMode = system.TFPURoundingMode;
  737. TFPUPrecisionMode = system.TFPUPrecisionMode;
  738. TFPUException = system.TFPUException;
  739. TFPUExceptionMask = system.TFPUExceptionMask;
  740. function GetRoundMode: TFPURoundingMode;
  741. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  742. function GetPrecisionMode: TFPUPrecisionMode;
  743. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  744. function GetExceptionMask: TFPUExceptionMask;
  745. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  746. procedure ClearExceptions(RaisePending: Boolean =true);
  747. implementation
  748. function copysign(x,y: float): float; forward; { returns abs(x)*sign(y) }
  749. { include cpu specific stuff }
  750. {$i mathu.inc}
  751. ResourceString
  752. SMathError = 'Math Error : %s';
  753. SInvalidArgument = 'Invalid argument';
  754. Procedure DoMathError(Const S : String);
  755. begin
  756. Raise EMathError.CreateFmt(SMathError,[S]);
  757. end;
  758. Procedure InvalidArgument;
  759. begin
  760. Raise EInvalidArgument.Create(SInvalidArgument);
  761. end;
  762. function Sign(const AValue: Integer): TValueSign;inline;
  763. begin
  764. result:=TValueSign(
  765. SarLongint(AValue,sizeof(AValue)*8-1) or { gives -1 for negative values, 0 otherwise }
  766. (longint(-AValue) shr (sizeof(AValue)*8-1)) { gives 1 for positive values, 0 otherwise }
  767. );
  768. end;
  769. function Sign(const AValue: Int64): TValueSign;inline;
  770. begin
  771. {$ifdef cpu64}
  772. result:=TValueSign(
  773. SarInt64(AValue,sizeof(AValue)*8-1) or
  774. (-AValue shr (sizeof(AValue)*8-1))
  775. );
  776. {$else cpu64}
  777. If Avalue<0 then
  778. Result:=NegativeValue
  779. else If Avalue>0 then
  780. Result:=PositiveValue
  781. else
  782. Result:=ZeroValue;
  783. {$endif}
  784. end;
  785. {$ifdef FPC_HAS_TYPE_SINGLE}
  786. function Sign(const AValue: Single): TValueSign;inline;
  787. begin
  788. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  789. end;
  790. {$endif}
  791. function Sign(const AValue: Double): TValueSign;inline;
  792. begin
  793. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  794. end;
  795. {$ifdef FPC_HAS_TYPE_EXTENDED}
  796. function Sign(const AValue: Extended): TValueSign;inline;
  797. begin
  798. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  799. end;
  800. {$endif}
  801. function degtorad(deg : float) : float;inline;
  802. begin
  803. degtorad:=deg*(pi/180.0);
  804. end;
  805. function radtodeg(rad : float) : float;inline;
  806. begin
  807. radtodeg:=rad*(180.0/pi);
  808. end;
  809. function gradtorad(grad : float) : float;inline;
  810. begin
  811. gradtorad:=grad*(pi/200.0);
  812. end;
  813. function radtograd(rad : float) : float;inline;
  814. begin
  815. radtograd:=rad*(200.0/pi);
  816. end;
  817. function degtograd(deg : float) : float;inline;
  818. begin
  819. degtograd:=deg*(200.0/180.0);
  820. end;
  821. function gradtodeg(grad : float) : float;inline;
  822. begin
  823. gradtodeg:=grad*(180.0/200.0);
  824. end;
  825. {$ifdef FPC_HAS_TYPE_SINGLE}
  826. function CycleToDeg(const Cycles: Single): Single;
  827. begin
  828. CycleToDeg:=Cycles*360.0;
  829. end;
  830. {$ENDIF}
  831. {$ifdef FPC_HAS_TYPE_DOUBLE}
  832. function CycleToDeg(const Cycles: Double): Double;
  833. begin
  834. CycleToDeg:=Cycles*360.0;
  835. end;
  836. {$ENDIF}
  837. {$ifdef FPC_HAS_TYPE_EXTENDED}
  838. function CycleToDeg(const Cycles: Extended): Extended;
  839. begin
  840. CycleToDeg:=Cycles*360.0;
  841. end;
  842. {$ENDIF}
  843. {$ifdef FPC_HAS_TYPE_SINGLE}
  844. function DegToCycle(const Degrees: Single): Single;
  845. begin
  846. DegToCycle:=Degrees*(1/360.0);
  847. end;
  848. {$ENDIF}
  849. {$ifdef FPC_HAS_TYPE_DOUBLE}
  850. function DegToCycle(const Degrees: Double): Double;
  851. begin
  852. DegToCycle:=Degrees*(1/360.0);
  853. end;
  854. {$ENDIF}
  855. {$ifdef FPC_HAS_TYPE_EXTENDED}
  856. function DegToCycle(const Degrees: Extended): Extended;
  857. begin
  858. DegToCycle:=Degrees*(1/360.0);
  859. end;
  860. {$ENDIF}
  861. {$ifdef FPC_HAS_TYPE_SINGLE}
  862. function CycleToGrad(const Cycles: Single): Single;
  863. begin
  864. CycleToGrad:=Cycles*400.0;
  865. end;
  866. {$ENDIF}
  867. {$ifdef FPC_HAS_TYPE_DOUBLE}
  868. function CycleToGrad(const Cycles: Double): Double;
  869. begin
  870. CycleToGrad:=Cycles*400.0;
  871. end;
  872. {$ENDIF}
  873. {$ifdef FPC_HAS_TYPE_EXTENDED}
  874. function CycleToGrad(const Cycles: Extended): Extended;
  875. begin
  876. CycleToGrad:=Cycles*400.0;
  877. end;
  878. {$ENDIF}
  879. {$ifdef FPC_HAS_TYPE_SINGLE}
  880. function GradToCycle(const Grads: Single): Single;
  881. begin
  882. GradToCycle:=Grads*(1/400.0);
  883. end;
  884. {$ENDIF}
  885. {$ifdef FPC_HAS_TYPE_DOUBLE}
  886. function GradToCycle(const Grads: Double): Double;
  887. begin
  888. GradToCycle:=Grads*(1/400.0);
  889. end;
  890. {$ENDIF}
  891. {$ifdef FPC_HAS_TYPE_EXTENDED}
  892. function GradToCycle(const Grads: Extended): Extended;
  893. begin
  894. GradToCycle:=Grads*(1/400.0);
  895. end;
  896. {$ENDIF}
  897. {$ifdef FPC_HAS_TYPE_SINGLE}
  898. function CycleToRad(const Cycles: Single): Single;
  899. begin
  900. CycleToRad:=Cycles*2*pi;
  901. end;
  902. {$ENDIF}
  903. {$ifdef FPC_HAS_TYPE_DOUBLE}
  904. function CycleToRad(const Cycles: Double): Double;
  905. begin
  906. CycleToRad:=Cycles*2*pi;
  907. end;
  908. {$ENDIF}
  909. {$ifdef FPC_HAS_TYPE_EXTENDED}
  910. function CycleToRad(const Cycles: Extended): Extended;
  911. begin
  912. CycleToRad:=Cycles*2*pi;
  913. end;
  914. {$ENDIF}
  915. {$ifdef FPC_HAS_TYPE_SINGLE}
  916. function RadToCycle(const Rads: Single): Single;
  917. begin
  918. RadToCycle:=Rads*(1/(2*pi));
  919. end;
  920. {$ENDIF}
  921. {$ifdef FPC_HAS_TYPE_DOUBLE}
  922. function RadToCycle(const Rads: Double): Double;
  923. begin
  924. RadToCycle:=Rads*(1/(2*pi));
  925. end;
  926. {$ENDIF}
  927. {$ifdef FPC_HAS_TYPE_EXTENDED}
  928. function RadToCycle(const Rads: Extended): Extended;
  929. begin
  930. RadToCycle:=Rads*(1/(2*pi));
  931. end;
  932. {$ENDIF}
  933. {$ifdef FPC_HAS_TYPE_SINGLE}
  934. Function DegNormalize(deg : single) : single;
  935. begin
  936. Result:=Deg-Int(Deg/360)*360;
  937. If Result<0 then Result:=Result+360;
  938. end;
  939. {$ENDIF}
  940. {$ifdef FPC_HAS_TYPE_DOUBLE}
  941. Function DegNormalize(deg : double) : double; inline;
  942. begin
  943. Result:=Deg-Int(Deg/360)*360;
  944. If (Result<0) then Result:=Result+360;
  945. end;
  946. {$ENDIF}
  947. {$ifdef FPC_HAS_TYPE_EXTENDED}
  948. Function DegNormalize(deg : extended) : extended; inline;
  949. begin
  950. Result:=Deg-Int(Deg/360)*360;
  951. If Result<0 then Result:=Result+360;
  952. end;
  953. {$ENDIF}
  954. {$ifndef FPC_MATH_HAS_TAN}
  955. function tan(x : float) : float;
  956. var
  957. _sin,_cos : float;
  958. begin
  959. sincos(x,_sin,_cos);
  960. tan:=_sin/_cos;
  961. end;
  962. {$endif FPC_MATH_HAS_TAN}
  963. {$ifndef FPC_MATH_HAS_COTAN}
  964. function cotan(x : float) : float;
  965. var
  966. _sin,_cos : float;
  967. begin
  968. sincos(x,_sin,_cos);
  969. cotan:=_cos/_sin;
  970. end;
  971. {$endif FPC_MATH_HAS_COTAN}
  972. function cot(x : float) : float; inline;
  973. begin
  974. cot := cotan(x);
  975. end;
  976. {$ifndef FPC_MATH_HAS_SINCOS}
  977. {$ifdef FPC_HAS_TYPE_SINGLE}
  978. procedure sincos(theta : single;out sinus,cosinus : single);
  979. begin
  980. sinus:=sin(theta);
  981. cosinus:=cos(theta);
  982. end;
  983. {$endif}
  984. {$ifdef FPC_HAS_TYPE_DOUBLE}
  985. procedure sincos(theta : double;out sinus,cosinus : double);
  986. begin
  987. sinus:=sin(theta);
  988. cosinus:=cos(theta);
  989. end;
  990. {$endif}
  991. {$ifdef FPC_HAS_TYPE_EXTENDED}
  992. procedure sincos(theta : extended;out sinus,cosinus : extended);
  993. begin
  994. sinus:=sin(theta);
  995. cosinus:=cos(theta);
  996. end;
  997. {$endif}
  998. {$endif FPC_MATH_HAS_SINCOS}
  999. function secant(x : float) : float; inline;
  1000. begin
  1001. secant := 1 / cos(x);
  1002. end;
  1003. function cosecant(x : float) : float; inline;
  1004. begin
  1005. cosecant := 1 / sin(x);
  1006. end;
  1007. function sec(x : float) : float; inline;
  1008. begin
  1009. sec := secant(x);
  1010. end;
  1011. function csc(x : float) : float; inline;
  1012. begin
  1013. csc := cosecant(x);
  1014. end;
  1015. { arcsin and arccos functions from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  1016. {$ifdef FPC_HAS_TYPE_SINGLE}
  1017. function arcsin(x : Single) : Single;
  1018. begin
  1019. arcsin:=arctan2(x,sqrt((1.0-x)*(1.0+x)));
  1020. end;
  1021. {$ENDIF}
  1022. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1023. function arcsin(x : Double) : Double;
  1024. begin
  1025. arcsin:=arctan2(x,sqrt((1.0-x)*(1.0+x)));
  1026. end;
  1027. {$ENDIF}
  1028. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1029. function arcsin(x : Extended) : Extended;
  1030. begin
  1031. arcsin:=arctan2(x,sqrt((1.0-x)*(1.0+x)));
  1032. end;
  1033. {$ENDIF}
  1034. {$ifdef FPC_HAS_TYPE_SINGLE}
  1035. function Arccos(x : Single) : Single;
  1036. begin
  1037. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  1038. end;
  1039. {$ENDIF}
  1040. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1041. function Arccos(x : Double) : Double;
  1042. begin
  1043. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  1044. end;
  1045. {$ENDIF}
  1046. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1047. function Arccos(x : Extended) : Extended;
  1048. begin
  1049. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  1050. end;
  1051. {$ENDIF}
  1052. {$ifndef FPC_MATH_HAS_ARCTAN2}
  1053. function arctan2(y,x : float) : float;
  1054. begin
  1055. if x=0 then
  1056. begin
  1057. if y=0 then
  1058. result:=0.0
  1059. else if y>0 then
  1060. result:=pi/2
  1061. else
  1062. result:=-pi/2;
  1063. end
  1064. else
  1065. begin
  1066. result:=ArcTan(y/x);
  1067. if x<0 then
  1068. if y<0 then
  1069. result:=result-pi
  1070. else
  1071. result:=result+pi;
  1072. end;
  1073. end;
  1074. {$endif FPC_MATH_HAS_ARCTAN2}
  1075. const
  1076. huge: double = 1e300;
  1077. {$ifdef FPC_HAS_TYPE_SINGLE}
  1078. function cosh(x : Single) : Single;
  1079. var
  1080. temp : ValReal;
  1081. begin
  1082. if x>8.94159862326326216608E+0001 then
  1083. begin
  1084. cosh:=huge+huge;
  1085. exit;
  1086. end;
  1087. temp:=exp(x);
  1088. {$push}
  1089. {$safefpuexceptions on}
  1090. cosh:=0.5*(temp+1.0/temp);
  1091. {$pop}
  1092. end;
  1093. {$ENDIF}
  1094. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1095. function cosh(x : Double) : Double;
  1096. var
  1097. temp : ValReal;
  1098. begin
  1099. if x>7.10475860073943942030E+0002 then
  1100. begin
  1101. cosh:=huge+huge;
  1102. exit;
  1103. end;
  1104. temp:=exp(x);
  1105. {$push}
  1106. {$safefpuexceptions on}
  1107. cosh:=0.5*(temp+1.0/temp);
  1108. {$pop}
  1109. end;
  1110. {$ENDIF}
  1111. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1112. function cosh(x : Extended) : Extended;
  1113. var
  1114. temp : ValReal;
  1115. begin
  1116. temp:=exp(x);
  1117. cosh:=0.5*(temp+1.0/temp);
  1118. end;
  1119. {$ENDIF}
  1120. {$ifdef FPC_HAS_TYPE_SINGLE}
  1121. function sinh(x : Single) : Single;
  1122. var
  1123. temp : ValReal;
  1124. begin
  1125. if x>8.94159862326326216608E+0001 then
  1126. begin
  1127. sinh:=huge+huge;
  1128. exit;
  1129. end;
  1130. temp:=exp(x);
  1131. { gives better behavior around zero, and in particular ensures that sinh(-0.0)=-0.0 }
  1132. if temp=1 then
  1133. exit(x);
  1134. {$push}
  1135. {$safefpuexceptions on}
  1136. sinh:=0.5*(temp-1.0/temp);
  1137. {$pop}
  1138. end;
  1139. {$ENDIF}
  1140. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1141. function sinh(x : Double) : Double;
  1142. var
  1143. temp : ValReal;
  1144. begin
  1145. if x>7.10475860073943942030E+0002 then
  1146. begin
  1147. sinh:=huge+huge;
  1148. exit;
  1149. end;
  1150. temp:=exp(x);
  1151. if temp=1 then
  1152. exit(x);
  1153. {$push}
  1154. {$safefpuexceptions on}
  1155. sinh:=0.5*(temp-1.0/temp);
  1156. {$pop}
  1157. end;
  1158. {$ENDIF}
  1159. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1160. function sinh(x : Extended) : Extended;
  1161. var
  1162. temp : ValReal;
  1163. begin
  1164. temp:=exp(x);
  1165. if temp=1 then
  1166. exit(x);
  1167. sinh:=0.5*(temp-1.0/temp);
  1168. end;
  1169. {$ENDIF}
  1170. {$ifdef FPC_HAS_TYPE_SINGLE}
  1171. function tanh(x : Single) : Single;
  1172. var
  1173. tmp:ValReal;
  1174. begin
  1175. if x < 0 then begin
  1176. tmp:=exp(2*x);
  1177. if tmp=1 then
  1178. exit(x);
  1179. {$push}
  1180. {$safefpuexceptions on}
  1181. result:=(tmp-1)/(1+tmp)
  1182. {$pop}
  1183. end
  1184. else begin
  1185. tmp:=exp(-2*x);
  1186. if tmp=1 then
  1187. exit(x);
  1188. {$push}
  1189. {$safefpuexceptions on}
  1190. result:=(1-tmp)/(1+tmp)
  1191. {$pop}
  1192. end;
  1193. end;
  1194. {$ENDIF}
  1195. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1196. function tanh(x : Double) : Double;
  1197. var
  1198. tmp:ValReal;
  1199. begin
  1200. if x < 0 then begin
  1201. tmp:=exp(2*x);
  1202. if tmp=1 then
  1203. exit(x);
  1204. {$push}
  1205. {$safefpuexceptions on}
  1206. result:=(tmp-1)/(1+tmp)
  1207. {$pop}
  1208. end
  1209. else begin
  1210. tmp:=exp(-2*x);
  1211. if tmp=1 then
  1212. exit(x);
  1213. {$push}
  1214. {$safefpuexceptions on}
  1215. result:=(1-tmp)/(1+tmp)
  1216. {$pop}
  1217. end;
  1218. end;
  1219. {$ENDIF}
  1220. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1221. function tanh(x : Extended) : Extended;
  1222. var
  1223. tmp:Extended;
  1224. begin
  1225. if x < 0 then begin
  1226. tmp:=exp(2*x);
  1227. if tmp=1 then
  1228. exit(x);
  1229. result:=(tmp-1)/(1+tmp)
  1230. end
  1231. else begin
  1232. tmp:=exp(-2*x);
  1233. if tmp=1 then
  1234. exit(x);
  1235. result:=(1-tmp)/(1+tmp)
  1236. end;
  1237. end;
  1238. {$ENDIF}
  1239. {$ifdef FPC_HAS_TYPE_SINGLE}
  1240. function SecH(const X: Single): Single;
  1241. var
  1242. Ex: ValReal;
  1243. begin
  1244. //https://en.wikipedia.org/wiki/Hyperbolic_functions#Definitions
  1245. //SecH = 2 / (e^X + e^-X)
  1246. Ex:=Exp(X);
  1247. {$push}
  1248. {$safefpuexceptions on}
  1249. SecH:=2/(Ex+1/Ex);
  1250. {$pop}
  1251. end;
  1252. {$ENDIF}
  1253. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1254. function SecH(const X: Double): Double;
  1255. var
  1256. Ex: ValReal;
  1257. begin
  1258. Ex:=Exp(X);
  1259. {$push}
  1260. {$safefpuexceptions on}
  1261. SecH:=2/(Ex+1/Ex);
  1262. {$pop}
  1263. end;
  1264. {$ENDIF}
  1265. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1266. function SecH(const X: Extended): Extended;
  1267. var
  1268. Ex: ValReal;
  1269. begin
  1270. Ex:=Exp(X);
  1271. SecH:=2/(Ex+1/Ex);
  1272. end;
  1273. {$ENDIF}
  1274. {$ifdef FPC_HAS_TYPE_SINGLE}
  1275. function CscH(const X: Single): Single;
  1276. var
  1277. Ex: ValReal;
  1278. begin
  1279. //CscH = 2 / (e^X - e^-X)
  1280. Ex:=Exp(X);
  1281. {$push}
  1282. {$safefpuexceptions on}
  1283. CscH:=2/(Ex-1/Ex);
  1284. {$pop}
  1285. end;
  1286. {$ENDIF}
  1287. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1288. function CscH(const X: Double): Double;
  1289. var
  1290. Ex: ValReal;
  1291. begin
  1292. Ex:=Exp(X);
  1293. {$push}
  1294. {$safefpuexceptions on}
  1295. CscH:=2/(Ex-1/Ex);
  1296. {$pop}
  1297. end;
  1298. {$ENDIF}
  1299. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1300. function CscH(const X: Extended): Extended;
  1301. var
  1302. Ex: ValReal;
  1303. begin
  1304. Ex:=Exp(X);
  1305. CscH:=2/(Ex-1/Ex);
  1306. end;
  1307. {$ENDIF}
  1308. {$ifdef FPC_HAS_TYPE_SINGLE}
  1309. function CotH(const X: Single): Single;
  1310. var
  1311. e2: ValReal;
  1312. begin
  1313. if x < 0 then begin
  1314. e2:=exp(2*x);
  1315. if e2=1 then
  1316. exit(1/x);
  1317. {$push}
  1318. {$safefpuexceptions on}
  1319. result:=(1+e2)/(e2-1)
  1320. {$pop}
  1321. end
  1322. else begin
  1323. e2:=exp(-2*x);
  1324. if e2=1 then
  1325. exit(1/x);
  1326. {$push}
  1327. {$safefpuexceptions on}
  1328. result:=(1+e2)/(1-e2)
  1329. {$pop}
  1330. end;
  1331. end;
  1332. {$ENDIF}
  1333. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1334. function CotH(const X: Double): Double;
  1335. var
  1336. e2: ValReal;
  1337. begin
  1338. if x < 0 then begin
  1339. e2:=exp(2*x);
  1340. if e2=1 then
  1341. exit(1/x);
  1342. {$push}
  1343. {$safefpuexceptions on}
  1344. result:=(1+e2)/(e2-1)
  1345. {$pop}
  1346. end
  1347. else begin
  1348. e2:=exp(-2*x);
  1349. if e2=1 then
  1350. exit(1/x);
  1351. {$push}
  1352. {$safefpuexceptions on}
  1353. result:=(1+e2)/(1-e2)
  1354. {$pop}
  1355. end;
  1356. end;
  1357. {$ENDIF}
  1358. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1359. function CotH(const X: Extended): Extended;
  1360. var
  1361. e2: ValReal;
  1362. begin
  1363. if x < 0 then begin
  1364. e2:=exp(2*x);
  1365. if e2=1 then
  1366. exit(1/x);
  1367. result:=(1+e2)/(e2-1)
  1368. end
  1369. else begin
  1370. e2:=exp(-2*x);
  1371. if e2=1 then
  1372. exit(1/x);
  1373. result:=(1+e2)/(1-e2)
  1374. end;
  1375. end;
  1376. {$ENDIF}
  1377. function arccosh(x : float) : float; inline;
  1378. begin
  1379. arccosh:=arcosh(x);
  1380. end;
  1381. function arcsinh(x : float) : float;inline;
  1382. begin
  1383. arcsinh:=arsinh(x);
  1384. end;
  1385. function arctanh(x : float) : float;inline;
  1386. begin
  1387. arctanh:=artanh(x);
  1388. end;
  1389. function arcosh(x : float) : float;
  1390. begin
  1391. { Provides accuracy about 4*eps near 1.0 }
  1392. arcosh:=Ln(x+Sqrt((x-1.0)*(x+1.0)));
  1393. end;
  1394. function arsinh(x : float) : float;
  1395. var
  1396. z: float;
  1397. begin
  1398. z:=abs(x);
  1399. z:=Ln(z+Sqrt(1+z*z));
  1400. { copysign ensures that arsinh(-Inf)=-Inf and arsinh(-0.0)=-0.0 }
  1401. arsinh:=copysign(z,x);
  1402. end;
  1403. function artanh(x : float) : float;
  1404. begin
  1405. artanh:=(lnxp1(x)-lnxp1(-x))*0.5;
  1406. end;
  1407. {$ifdef FPC_HAS_TYPE_SINGLE}
  1408. function ArcSec(X: Single): Single;
  1409. begin
  1410. ArcSec:=ArcCos(1/X);
  1411. end;
  1412. {$ENDIF}
  1413. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1414. function ArcSec(X: Double): Double;
  1415. begin
  1416. ArcSec:=ArcCos(1/X);
  1417. end;
  1418. {$ENDIF}
  1419. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1420. function ArcSec(X: Extended): Extended;
  1421. begin
  1422. ArcSec:=ArcCos(1/X);
  1423. end;
  1424. {$ENDIF}
  1425. {$ifdef FPC_HAS_TYPE_SINGLE}
  1426. function ArcCsc(X: Single): Single;
  1427. begin
  1428. ArcCsc:=ArcSin(1/X);
  1429. end;
  1430. {$ENDIF}
  1431. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1432. function ArcCsc(X: Double): Double;
  1433. begin
  1434. ArcCsc:=ArcSin(1/X);
  1435. end;
  1436. {$ENDIF}
  1437. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1438. function ArcCsc(X: Extended): Extended;
  1439. begin
  1440. ArcCsc:=ArcSin(1/X);
  1441. end;
  1442. {$ENDIF}
  1443. {$ifdef FPC_HAS_TYPE_SINGLE}
  1444. function ArcCot(X: Single): Single;
  1445. begin
  1446. if x=0 then
  1447. ArcCot:=0.5*pi
  1448. else
  1449. ArcCot:=ArcTan(1/X);
  1450. end;
  1451. {$ENDIF}
  1452. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1453. function ArcCot(X: Double): Double;
  1454. begin
  1455. begin
  1456. if x=0 then
  1457. ArcCot:=0.5*pi
  1458. else
  1459. ArcCot:=ArcTan(1/X);
  1460. end;
  1461. end;
  1462. {$ENDIF}
  1463. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1464. function ArcCot(X: Extended): Extended;
  1465. begin
  1466. begin
  1467. if x=0 then
  1468. ArcCot:=0.5*pi
  1469. else
  1470. ArcCot:=ArcTan(1/X);
  1471. end;
  1472. end;
  1473. {$ENDIF}
  1474. {$ifdef FPC_HAS_TYPE_SINGLE}
  1475. function ArcSecH(X : Single): Single;
  1476. begin
  1477. ArcSecH:=ln((1+(sqrt(1.0-sqr(X))))/X); //replacing division inside ln() by subtracting 2 ln()'s seems to be slower
  1478. end;
  1479. {$ENDIF}
  1480. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1481. function ArcSecH(X : Double): Double;
  1482. begin
  1483. ArcSecH:=ln((1+(sqrt(1.0-sqr(X))))/X);
  1484. end;
  1485. {$ENDIF}
  1486. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1487. function ArcSecH(X : Extended): Extended;
  1488. begin
  1489. ArcSecH:=ln((1+(sqrt(1.0-sqr(X))))/X);
  1490. end;
  1491. {$ENDIF}
  1492. {$ifdef FPC_HAS_TYPE_SINGLE}
  1493. function ArcCscH(X: Single): Single;
  1494. begin
  1495. ArcCscH:=ln((1.0/X)+sqrt(1.0/(sqr(x))+1.0));
  1496. end;
  1497. {$ENDIF}
  1498. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1499. function ArcCscH(X: Double): Double;
  1500. begin
  1501. ArcCscH:=ln((1.0/X)+sqrt(1.0/(sqr(x))+1.0));
  1502. end;
  1503. {$ENDIF}
  1504. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1505. function ArcCscH(X: Extended): Extended;
  1506. begin
  1507. ArcCscH:=ln((1.0/X)+sqrt(1.0/(sqr(x))+1.0));
  1508. end;
  1509. {$ENDIF}
  1510. {$ifdef FPC_HAS_TYPE_SINGLE}
  1511. function ArcCotH(X: Single): Single;
  1512. begin
  1513. ArcCotH:=0.5*ln((x + 1.0)/(x - 1.0));
  1514. end;
  1515. {$ENDIF}
  1516. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1517. function ArcCotH(X: Double): Double;
  1518. begin
  1519. ArcCotH:=0.5*ln((x + 1.0)/(x - 1.0));
  1520. end;
  1521. {$ENDIF}
  1522. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1523. function ArcCotH(X: Extended): Extended;
  1524. begin
  1525. ArcCotH:=0.5*ln((x + 1.0)/(x - 1.0));
  1526. end;
  1527. {$ENDIF}
  1528. { hypot function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  1529. function hypot(x,y : float) : float;
  1530. begin
  1531. x:=abs(x);
  1532. y:=abs(y);
  1533. if (x>y) then
  1534. hypot:=x*sqrt(1.0+sqr(y/x))
  1535. else if (x>0.0) then
  1536. hypot:=y*sqrt(1.0+sqr(x/y))
  1537. else
  1538. hypot:=y;
  1539. end;
  1540. function log10(x : float) : float;
  1541. begin
  1542. log10:=ln(x)*0.43429448190325182765; { 1/ln(10) }
  1543. end;
  1544. {$ifndef FPC_MATH_HAS_LOG2}
  1545. function log2(x : float) : float;
  1546. begin
  1547. log2:=ln(x)*1.4426950408889634079; { 1/ln(2) }
  1548. end;
  1549. {$endif FPC_MATH_HAS_LOG2}
  1550. function logn(n,x : float) : float;
  1551. begin
  1552. logn:=ln(x)/ln(n);
  1553. end;
  1554. { lnxp1 function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  1555. function lnxp1(x : float) : float;
  1556. var
  1557. y: float;
  1558. begin
  1559. if (x>=4.0) then
  1560. lnxp1:=ln(1.0+x)
  1561. else
  1562. begin
  1563. y:=1.0+x;
  1564. if (y=1.0) then
  1565. lnxp1:=x
  1566. else
  1567. begin
  1568. lnxp1:=ln(y); { lnxp1(-1) = ln(0) = -Inf }
  1569. if y>0.0 then
  1570. lnxp1:=lnxp1+(x-(y-1.0))/y;
  1571. end;
  1572. end;
  1573. end;
  1574. function power(base,exponent : float) : float;
  1575. begin
  1576. if Exponent=0.0 then
  1577. result:=1.0
  1578. else if (base=0.0) and (exponent>0.0) then
  1579. result:=0.0
  1580. else if (frac(exponent)=0.0) and (abs(exponent)<=maxint) then
  1581. result:=intpower(base,trunc(exponent))
  1582. else
  1583. result:=exp(exponent * ln (base));
  1584. end;
  1585. function intpower(base : float;exponent : longint) : float;
  1586. begin
  1587. if exponent<0 then
  1588. begin
  1589. base:=1.0/base;
  1590. exponent:=-exponent;
  1591. end;
  1592. intpower:=1.0;
  1593. while exponent<>0 do
  1594. begin
  1595. if exponent and 1<>0 then
  1596. intpower:=intpower*base;
  1597. exponent:=exponent shr 1;
  1598. base:=sqr(base);
  1599. end;
  1600. end;
  1601. operator ** (base,exponent : float) e: float; inline;
  1602. begin
  1603. e:=power(base,exponent);
  1604. end;
  1605. operator ** (base,exponent : int64) res: int64;
  1606. begin
  1607. if exponent<0 then
  1608. begin
  1609. if base<=0 then
  1610. raise EInvalidArgument.Create('Non-positive base with negative exponent in **');
  1611. if base=1 then
  1612. res:=1
  1613. else
  1614. res:=0;
  1615. exit;
  1616. end;
  1617. res:=1;
  1618. while exponent<>0 do
  1619. begin
  1620. if exponent and 1<>0 then
  1621. res:=res*base;
  1622. exponent:=exponent shr 1;
  1623. base:=base*base;
  1624. end;
  1625. end;
  1626. function ceil(x : float) : integer;
  1627. begin
  1628. Result:=Trunc(x)+ord(Frac(x)>0);
  1629. end;
  1630. function ceil64(x: float): Int64;
  1631. begin
  1632. Result:=Trunc(x)+ord(Frac(x)>0);
  1633. end;
  1634. function floor(x : float) : integer;
  1635. begin
  1636. Result:=Trunc(x)-ord(Frac(x)<0);
  1637. end;
  1638. function floor64(x: float): Int64;
  1639. begin
  1640. Result:=Trunc(x)-ord(Frac(x)<0);
  1641. end;
  1642. // Correction for "rounding to nearest, ties to even".
  1643. // RoundToNearestTieToEven(QWE.RTYUIOP) = QWE + TieToEven(ER, TYUIOP <> 0).
  1644. function TieToEven(AB: cardinal; somethingAfter: boolean): cardinal;
  1645. begin
  1646. result := AB and 1;
  1647. if (result <> 0) and not somethingAfter then
  1648. result := AB shr 1;
  1649. end;
  1650. {$ifdef FPC_HAS_TYPE_SINGLE}
  1651. procedure Frexp(X: single; out Mantissa: single; out Exponent: integer);
  1652. var
  1653. M: uint32;
  1654. E, ExtraE: int32;
  1655. begin
  1656. Mantissa := X;
  1657. E := TSingleRec(X).Exp;
  1658. if (E > 0) and (E < 2 * TSingleRec.Bias + 1) then
  1659. begin
  1660. // Normal.
  1661. TSingleRec(Mantissa).Exp := TSingleRec.Bias - 1;
  1662. Exponent := E - (TSingleRec.Bias - 1);
  1663. exit;
  1664. end;
  1665. if E = 0 then
  1666. begin
  1667. M := TSingleRec(X).Frac;
  1668. if M <> 0 then
  1669. begin
  1670. // Subnormal.
  1671. ExtraE := 23 - BsrDWord(M);
  1672. TSingleRec(Mantissa).Frac := M shl ExtraE; // "and (1 shl 23 - 1)" required to remove starting 1, but .SetFrac already does it.
  1673. TSingleRec(Mantissa).Exp := TSingleRec.Bias - 1;
  1674. Exponent := -TSingleRec.Bias + 2 - ExtraE;
  1675. exit;
  1676. end;
  1677. end;
  1678. // ±0, ±Inf, NaN.
  1679. Exponent := 0;
  1680. end;
  1681. function Ldexp(X: single; p: integer): single;
  1682. var
  1683. M, E: uint32;
  1684. xp, sh: integer;
  1685. begin
  1686. E := TSingleRec(X).Exp;
  1687. if (E = 0) and (TSingleRec(X).Frac = 0) or (E = 2 * TSingleRec.Bias + 1) then
  1688. // ±0, ±Inf, NaN.
  1689. exit(X);
  1690. Frexp(X, result, xp);
  1691. inc(xp, p);
  1692. if (xp >= -TSingleRec.Bias + 2) and (xp <= TSingleRec.Bias + 1) then
  1693. // Normalized.
  1694. TSingleRec(result).Exp := xp + (TSingleRec.Bias - 1)
  1695. else if xp > TSingleRec.Bias + 1 then
  1696. begin
  1697. // Overflow.
  1698. TSingleRec(result).Exp := 2 * TSingleRec.Bias + 1;
  1699. TSingleRec(result).Frac := 0;
  1700. end else
  1701. begin
  1702. TSingleRec(result).Exp := 0;
  1703. if xp >= -TSingleRec.Bias + 2 - 23 then
  1704. begin
  1705. // Denormalized.
  1706. M := TSingleRec(result).Frac or uint32(1) shl 23;
  1707. sh := -TSingleRec.Bias + 1 - xp;
  1708. TSingleRec(result).Frac := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint32(1) shl sh - 1) <> 0);
  1709. end else
  1710. // Underflow.
  1711. TSingleRec(result).Frac := 0;
  1712. end;
  1713. end;
  1714. {$endif}
  1715. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1716. procedure Frexp(X: double; out Mantissa: double; out Exponent: integer);
  1717. var
  1718. M: uint64;
  1719. E, ExtraE: int32;
  1720. begin
  1721. Mantissa := X;
  1722. E := TDoubleRec(X).Exp;
  1723. if (E > 0) and (E < 2 * TDoubleRec.Bias + 1) then
  1724. begin
  1725. // Normal.
  1726. TDoubleRec(Mantissa).Exp := TDoubleRec.Bias - 1;
  1727. Exponent := E - (TDoubleRec.Bias - 1);
  1728. exit;
  1729. end;
  1730. if E = 0 then
  1731. begin
  1732. M := TDoubleRec(X).Frac;
  1733. if M <> 0 then
  1734. begin
  1735. // Subnormal.
  1736. ExtraE := 52 - BsrQWord(M);
  1737. TDoubleRec(Mantissa).Frac := M shl ExtraE; // "and (1 shl 52 - 1)" required to remove starting 1, but .SetFrac already does it.
  1738. TDoubleRec(Mantissa).Exp := TDoubleRec.Bias - 1;
  1739. Exponent := -TDoubleRec.Bias + 2 - ExtraE;
  1740. exit;
  1741. end;
  1742. end;
  1743. // ±0, ±Inf, NaN.
  1744. Exponent := 0;
  1745. end;
  1746. function Ldexp(X: double; p: integer): double;
  1747. var
  1748. M: uint64;
  1749. E: uint32;
  1750. xp, sh: integer;
  1751. begin
  1752. E := TDoubleRec(X).Exp;
  1753. if (E = 0) and (TDoubleRec(X).Frac = 0) or (E = 2 * TDoubleRec.Bias + 1) then
  1754. // ±0, ±Inf, NaN.
  1755. exit(X);
  1756. Frexp(X, result, xp);
  1757. inc(xp, p);
  1758. if (xp >= -TDoubleRec.Bias + 2) and (xp <= TDoubleRec.Bias + 1) then
  1759. // Normalized.
  1760. TDoubleRec(result).Exp := xp + (TDoubleRec.Bias - 1)
  1761. else if xp > TDoubleRec.Bias + 1 then
  1762. begin
  1763. // Overflow.
  1764. TDoubleRec(result).Exp := 2 * TDoubleRec.Bias + 1;
  1765. TDoubleRec(result).Frac := 0;
  1766. end else
  1767. begin
  1768. TDoubleRec(result).Exp := 0;
  1769. if xp >= -TDoubleRec.Bias + 2 - 52 then
  1770. begin
  1771. // Denormalized.
  1772. M := TDoubleRec(result).Frac or uint64(1) shl 52;
  1773. sh := -TSingleRec.Bias + 1 - xp;
  1774. TDoubleRec(result).Frac := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint64(1) shl sh - 1) <> 0);
  1775. end else
  1776. // Underflow.
  1777. TDoubleRec(result).Frac := 0;
  1778. end;
  1779. end;
  1780. {$endif}
  1781. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1782. procedure Frexp(X: extended; out Mantissa: extended; out Exponent: integer);
  1783. var
  1784. M: uint64;
  1785. E, ExtraE: int32;
  1786. begin
  1787. Mantissa := X;
  1788. E := TExtended80Rec(X).Exp;
  1789. if (E > 0) and (E < 2 * TExtended80Rec.Bias + 1) then
  1790. begin
  1791. // Normal.
  1792. TExtended80Rec(Mantissa).Exp := TExtended80Rec.Bias - 1;
  1793. Exponent := E - (TExtended80Rec.Bias - 1);
  1794. exit;
  1795. end;
  1796. if E = 0 then
  1797. begin
  1798. M := TExtended80Rec(X).Frac;
  1799. if M <> 0 then
  1800. begin
  1801. // Subnormal. Extended has explicit starting 1.
  1802. ExtraE := 63 - BsrQWord(M);
  1803. TExtended80Rec(Mantissa).Frac := M shl ExtraE;
  1804. TExtended80Rec(Mantissa).Exp := TExtended80Rec.Bias - 1;
  1805. Exponent := -TExtended80Rec.Bias + 2 - ExtraE;
  1806. exit;
  1807. end;
  1808. end;
  1809. // ±0, ±Inf, NaN.
  1810. Exponent := 0;
  1811. end;
  1812. function Ldexp(X: extended; p: integer): extended;
  1813. var
  1814. M: uint64;
  1815. E: uint32;
  1816. xp, sh: integer;
  1817. begin
  1818. E := TExtended80Rec(X).Exp;
  1819. if (E = 0) and (TExtended80Rec(X).Frac = 0) or (E = 2 * TExtended80Rec.Bias + 1) then
  1820. // ±0, ±Inf, NaN.
  1821. exit(X);
  1822. Frexp(X, result, xp);
  1823. inc(xp, p);
  1824. if (xp >= -TExtended80Rec.Bias + 2) and (xp <= TExtended80Rec.Bias + 1) then
  1825. // Normalized.
  1826. TExtended80Rec(result).Exp := xp + (TExtended80Rec.Bias - 1)
  1827. else if xp > TExtended80Rec.Bias + 1 then
  1828. begin
  1829. // Overflow.
  1830. TExtended80Rec(result).Exp := 2 * TExtended80Rec.Bias + 1;
  1831. TExtended80Rec(result).Frac := uint64(1) shl 63;
  1832. end
  1833. else if xp >= -TExtended80Rec.Bias + 2 - 63 then
  1834. begin
  1835. // Denormalized... usually.
  1836. // Mantissa of subnormal 'extended' (Exp = 0) must always start with 0.
  1837. // If the calculated mantissa starts with 1, extended instead becomes normalized with Exp = 1.
  1838. M := TExtended80Rec(result).Frac;
  1839. sh := -TExtended80Rec.Bias + 1 - xp;
  1840. M := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint64(1) shl sh - 1) <> 0);
  1841. TExtended80Rec(result).Exp := M shr 63;
  1842. TExtended80Rec(result).Frac := M;
  1843. end else
  1844. begin
  1845. // Underflow.
  1846. TExtended80Rec(result).Exp := 0;
  1847. TExtended80Rec(result).Frac := 0;
  1848. end;
  1849. end;
  1850. {$endif}
  1851. const
  1852. { Cutoff for https://en.wikipedia.org/wiki/Pairwise_summation; sums of at least this many elements are split in two halves. }
  1853. RecursiveSumThreshold=12;
  1854. {$ifdef FPC_HAS_TYPE_SINGLE}
  1855. function mean(const data : array of Single) : float;
  1856. begin
  1857. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  1858. end;
  1859. function mean(const data : PSingle; Const N : longint) : float;
  1860. begin
  1861. mean:=sum(Data,N);
  1862. mean:=mean/N;
  1863. end;
  1864. function sum(const data : array of Single) : float;inline;
  1865. begin
  1866. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  1867. end;
  1868. function sum(const data : PSingle;Const N : longint) : float;
  1869. var
  1870. i : SizeInt;
  1871. begin
  1872. if N>=RecursiveSumThreshold then
  1873. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1874. else
  1875. begin
  1876. result:=0;
  1877. for i:=0 to N-1 do
  1878. result:=result+data[i];
  1879. end;
  1880. end;
  1881. {$endif FPC_HAS_TYPE_SINGLE}
  1882. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1883. function mean(const data : array of Double) : float; inline;
  1884. begin
  1885. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  1886. end;
  1887. function mean(const data : PDouble; Const N : longint) : float;
  1888. begin
  1889. mean:=sum(Data,N);
  1890. mean:=mean/N;
  1891. end;
  1892. function sum(const data : array of Double) : float; inline;
  1893. begin
  1894. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  1895. end;
  1896. function sum(const data : PDouble;Const N : longint) : float;
  1897. var
  1898. i : SizeInt;
  1899. begin
  1900. if N>=RecursiveSumThreshold then
  1901. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1902. else
  1903. begin
  1904. result:=0;
  1905. for i:=0 to N-1 do
  1906. result:=result+data[i];
  1907. end;
  1908. end;
  1909. {$endif FPC_HAS_TYPE_DOUBLE}
  1910. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1911. function mean(const data : array of Extended) : float;
  1912. begin
  1913. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  1914. end;
  1915. function mean(const data : PExtended; Const N : longint) : float;
  1916. begin
  1917. mean:=sum(Data,N);
  1918. mean:=mean/N;
  1919. end;
  1920. function sum(const data : array of Extended) : float; inline;
  1921. begin
  1922. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  1923. end;
  1924. function sum(const data : PExtended;Const N : longint) : float;
  1925. var
  1926. i : SizeInt;
  1927. begin
  1928. if N>=RecursiveSumThreshold then
  1929. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1930. else
  1931. begin
  1932. result:=0;
  1933. for i:=0 to N-1 do
  1934. result:=result+data[i];
  1935. end;
  1936. end;
  1937. {$endif FPC_HAS_TYPE_EXTENDED}
  1938. function sumInt(const data : PInt64;Const N : longint) : Int64;
  1939. var
  1940. i : SizeInt;
  1941. begin
  1942. sumInt:=0;
  1943. for i:=0 to N-1 do
  1944. sumInt:=sumInt+data[i];
  1945. end;
  1946. function sumInt(const data : array of Int64) : Int64; inline;
  1947. begin
  1948. Result:=SumInt(PInt64(@Data[0]),High(Data)+1);
  1949. end;
  1950. function mean(const data : PInt64; const N : Longint):Float;
  1951. begin
  1952. mean:=sumInt(Data,N);
  1953. mean:=mean/N;
  1954. end;
  1955. function mean(const data: array of Int64):Float;
  1956. begin
  1957. mean:=mean(PInt64(@data[0]),High(Data)+1);
  1958. end;
  1959. function sumInt(const data : PInteger; Const N : longint) : Int64;
  1960. var
  1961. i : SizeInt;
  1962. begin
  1963. sumInt:=0;
  1964. for i:=0 to N-1 do
  1965. sumInt:=sumInt+data[i];
  1966. end;
  1967. function sumInt(const data : array of Integer) : Int64;inline;
  1968. begin
  1969. Result:=sumInt(PInteger(@Data[0]),High(Data)+1);
  1970. end;
  1971. function mean(const data : PInteger; const N : Longint):Float;
  1972. begin
  1973. mean:=sumInt(Data,N);
  1974. mean:=mean/N;
  1975. end;
  1976. function mean(const data: array of Integer):Float;
  1977. begin
  1978. mean:=mean(PInteger(@data[0]),High(Data)+1);
  1979. end;
  1980. {$ifdef FPC_HAS_TYPE_SINGLE}
  1981. function sumofsquares(const data : array of Single) : float; inline;
  1982. begin
  1983. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  1984. end;
  1985. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  1986. var
  1987. i : SizeInt;
  1988. begin
  1989. if N>=RecursiveSumThreshold then
  1990. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1991. else
  1992. begin
  1993. result:=0;
  1994. for i:=0 to N-1 do
  1995. result:=result+sqr(data[i]);
  1996. end;
  1997. end;
  1998. procedure sumsandsquares(const data : array of Single;
  1999. var sum,sumofsquares : float); inline;
  2000. begin
  2001. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  2002. end;
  2003. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  2004. var sum,sumofsquares : float);
  2005. var
  2006. i : SizeInt;
  2007. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  2008. begin
  2009. if N>=RecursiveSumThreshold then
  2010. begin
  2011. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  2012. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  2013. sum:=sum0+sum1;
  2014. sumofsquares:=sumofsquares0+sumofsquares1;
  2015. end
  2016. else
  2017. begin
  2018. tsum:=0;
  2019. tsumofsquares:=0;
  2020. for i:=0 to N-1 do
  2021. begin
  2022. temp:=data[i];
  2023. tsum:=tsum+temp;
  2024. tsumofsquares:=tsumofsquares+sqr(temp);
  2025. end;
  2026. sum:=tsum;
  2027. sumofsquares:=tsumofsquares;
  2028. end;
  2029. end;
  2030. {$endif FPC_HAS_TYPE_SINGLE}
  2031. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2032. function sumofsquares(const data : array of Double) : float; inline;
  2033. begin
  2034. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  2035. end;
  2036. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  2037. var
  2038. i : SizeInt;
  2039. begin
  2040. if N>=RecursiveSumThreshold then
  2041. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  2042. else
  2043. begin
  2044. result:=0;
  2045. for i:=0 to N-1 do
  2046. result:=result+sqr(data[i]);
  2047. end;
  2048. end;
  2049. procedure sumsandsquares(const data : array of Double;
  2050. var sum,sumofsquares : float); inline;
  2051. begin
  2052. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  2053. end;
  2054. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  2055. var sum,sumofsquares : float);
  2056. var
  2057. i : SizeInt;
  2058. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  2059. begin
  2060. if N>=RecursiveSumThreshold then
  2061. begin
  2062. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  2063. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  2064. sum:=sum0+sum1;
  2065. sumofsquares:=sumofsquares0+sumofsquares1;
  2066. end
  2067. else
  2068. begin
  2069. tsum:=0;
  2070. tsumofsquares:=0;
  2071. for i:=0 to N-1 do
  2072. begin
  2073. temp:=data[i];
  2074. tsum:=tsum+temp;
  2075. tsumofsquares:=tsumofsquares+sqr(temp);
  2076. end;
  2077. sum:=tsum;
  2078. sumofsquares:=tsumofsquares;
  2079. end;
  2080. end;
  2081. {$endif FPC_HAS_TYPE_DOUBLE}
  2082. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2083. function sumofsquares(const data : array of Extended) : float; inline;
  2084. begin
  2085. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  2086. end;
  2087. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  2088. var
  2089. i : SizeInt;
  2090. begin
  2091. if N>=RecursiveSumThreshold then
  2092. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  2093. else
  2094. begin
  2095. result:=0;
  2096. for i:=0 to N-1 do
  2097. result:=result+sqr(data[i]);
  2098. end;
  2099. end;
  2100. procedure sumsandsquares(const data : array of Extended;
  2101. var sum,sumofsquares : float); inline;
  2102. begin
  2103. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  2104. end;
  2105. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  2106. var sum,sumofsquares : float);
  2107. var
  2108. i : SizeInt;
  2109. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  2110. begin
  2111. if N>=RecursiveSumThreshold then
  2112. begin
  2113. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  2114. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  2115. sum:=sum0+sum1;
  2116. sumofsquares:=sumofsquares0+sumofsquares1;
  2117. end
  2118. else
  2119. begin
  2120. tsum:=0;
  2121. tsumofsquares:=0;
  2122. for i:=0 to N-1 do
  2123. begin
  2124. temp:=data[i];
  2125. tsum:=tsum+temp;
  2126. tsumofsquares:=tsumofsquares+sqr(temp);
  2127. end;
  2128. sum:=tsum;
  2129. sumofsquares:=tsumofsquares;
  2130. end;
  2131. end;
  2132. {$endif FPC_HAS_TYPE_EXTENDED}
  2133. function randg(mean,stddev : float) : float;
  2134. Var U1,S2 : Float;
  2135. begin
  2136. repeat
  2137. u1:= 2*random-1;
  2138. S2:=Sqr(U1)+sqr(2*random-1);
  2139. until s2<1;
  2140. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  2141. end;
  2142. function RandomRange(const aFrom, aTo: Integer): Integer;
  2143. begin
  2144. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  2145. end;
  2146. function RandomRange(const aFrom, aTo: Int64): Int64;
  2147. begin
  2148. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  2149. end;
  2150. {$ifdef FPC_HAS_TYPE_SINGLE}
  2151. procedure MeanAndTotalVariance
  2152. (const data: PSingle; N: LongInt; var mu, variance: float);
  2153. function CalcVariance(data: PSingle; N: SizeInt; mu: float): float;
  2154. var
  2155. i: SizeInt;
  2156. begin
  2157. if N>=RecursiveSumThreshold then
  2158. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  2159. else
  2160. begin
  2161. result:=0;
  2162. for i:=0 to N-1 do
  2163. result:=result+Sqr(data[i]-mu);
  2164. end;
  2165. end;
  2166. begin
  2167. mu := Mean( data, N );
  2168. variance := CalcVariance( data, N, mu );
  2169. end;
  2170. function stddev(const data : array of Single) : float; inline;
  2171. begin
  2172. Result:=Stddev(PSingle(@Data[0]),High(Data)+1);
  2173. end;
  2174. function stddev(const data : PSingle; Const N : Integer) : float;
  2175. begin
  2176. StdDev:=Sqrt(Variance(Data,N));
  2177. end;
  2178. procedure meanandstddev(const data : array of Single;
  2179. var mean,stddev : float); inline;
  2180. begin
  2181. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  2182. end;
  2183. procedure meanandstddev
  2184. ( const data: PSingle;
  2185. const N: Longint;
  2186. var mean,
  2187. stdDev: Float
  2188. );
  2189. var totalVariance: float;
  2190. begin
  2191. MeanAndTotalVariance( data, N, mean, totalVariance );
  2192. if N < 2 then stdDev := 0
  2193. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  2194. end;
  2195. function variance(const data : array of Single) : float; inline;
  2196. begin
  2197. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  2198. end;
  2199. function variance(const data : PSingle; Const N : Integer) : float;
  2200. begin
  2201. If N=1 then
  2202. Result:=0
  2203. else
  2204. Result:=TotalVariance(Data,N)/(N-1);
  2205. end;
  2206. function totalvariance(const data : array of Single) : float; inline;
  2207. begin
  2208. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  2209. end;
  2210. function totalvariance(const data : PSingle; const N : Integer) : float;
  2211. var mu: float;
  2212. begin
  2213. MeanAndTotalVariance( data, N, mu, result );
  2214. end;
  2215. function popnstddev(const data : array of Single) : float;
  2216. begin
  2217. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  2218. end;
  2219. function popnstddev(const data : PSingle; Const N : Integer) : float;
  2220. begin
  2221. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  2222. end;
  2223. function popnvariance(const data : array of Single) : float; inline;
  2224. begin
  2225. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  2226. end;
  2227. function popnvariance(const data : PSingle; Const N : Integer) : float;
  2228. begin
  2229. PopnVariance:=TotalVariance(Data,N)/N;
  2230. end;
  2231. procedure momentskewkurtosis(const data : array of single;
  2232. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  2233. begin
  2234. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  2235. end;
  2236. type
  2237. TMoments2to4 = array[2 .. 4] of float;
  2238. procedure momentskewkurtosis(
  2239. const data: pSingle;
  2240. Const N: integer;
  2241. out m1: float;
  2242. out m2: float;
  2243. out m3: float;
  2244. out m4: float;
  2245. out skew: float;
  2246. out kurtosis: float
  2247. );
  2248. procedure CalcDevSums2to4(data: PSingle; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  2249. var
  2250. tm2, tm3, tm4, dev, dev2: float;
  2251. i: SizeInt;
  2252. m2to4Part0, m2to4Part1: TMoments2to4;
  2253. begin
  2254. if N >= RecursiveSumThreshold then
  2255. begin
  2256. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  2257. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  2258. for i := Low(TMoments2to4) to High(TMoments2to4) do
  2259. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  2260. end
  2261. else
  2262. begin
  2263. tm2 := 0;
  2264. tm3 := 0;
  2265. tm4 := 0;
  2266. for i := 0 to N - 1 do
  2267. begin
  2268. dev := data[i] - m1;
  2269. dev2 := sqr(dev);
  2270. tm2 := tm2 + dev2;
  2271. tm3 := tm3 + dev2 * dev;
  2272. tm4 := tm4 + sqr(dev2);
  2273. end;
  2274. m2to4[2] := tm2;
  2275. m2to4[3] := tm3;
  2276. m2to4[4] := tm4;
  2277. end;
  2278. end;
  2279. var
  2280. reciprocalN: float;
  2281. m2to4: TMoments2to4;
  2282. begin
  2283. m1 := 0;
  2284. reciprocalN := 1/N;
  2285. m1 := reciprocalN * sum(data, N);
  2286. CalcDevSums2to4(data, N, m1, m2to4);
  2287. m2 := reciprocalN * m2to4[2];
  2288. m3 := reciprocalN * m2to4[3];
  2289. m4 := reciprocalN * m2to4[4];
  2290. skew := m3 / (sqrt(m2)*m2);
  2291. kurtosis := m4 / (m2 * m2);
  2292. end;
  2293. function norm(const data : array of Single) : float; inline;
  2294. begin
  2295. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  2296. end;
  2297. function norm(const data : PSingle; Const N : Integer) : float;
  2298. begin
  2299. norm:=sqrt(sumofsquares(data,N));
  2300. end;
  2301. {$endif FPC_HAS_TYPE_SINGLE}
  2302. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2303. procedure MeanAndTotalVariance
  2304. (const data: PDouble; N: LongInt; var mu, variance: float);
  2305. function CalcVariance(data: PDouble; N: SizeInt; mu: float): float;
  2306. var
  2307. i: SizeInt;
  2308. begin
  2309. if N>=RecursiveSumThreshold then
  2310. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  2311. else
  2312. begin
  2313. result:=0;
  2314. for i:=0 to N-1 do
  2315. result:=result+Sqr(data[i]-mu);
  2316. end;
  2317. end;
  2318. begin
  2319. mu := Mean( data, N );
  2320. variance := CalcVariance( data, N, mu );
  2321. end;
  2322. function stddev(const data : array of Double) : float; inline;
  2323. begin
  2324. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  2325. end;
  2326. function stddev(const data : PDouble; Const N : Integer) : float;
  2327. begin
  2328. StdDev:=Sqrt(Variance(Data,N));
  2329. end;
  2330. procedure meanandstddev(const data : array of Double;
  2331. var mean,stddev : float);
  2332. begin
  2333. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  2334. end;
  2335. procedure meanandstddev
  2336. ( const data: PDouble;
  2337. const N: Longint;
  2338. var mean,
  2339. stdDev: Float
  2340. );
  2341. var totalVariance: float;
  2342. begin
  2343. MeanAndTotalVariance( data, N, mean, totalVariance );
  2344. if N < 2 then stdDev := 0
  2345. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  2346. end;
  2347. function variance(const data : array of Double) : float; inline;
  2348. begin
  2349. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  2350. end;
  2351. function variance(const data : PDouble; Const N : Integer) : float;
  2352. begin
  2353. If N=1 then
  2354. Result:=0
  2355. else
  2356. Result:=TotalVariance(Data,N)/(N-1);
  2357. end;
  2358. function totalvariance(const data : array of Double) : float; inline;
  2359. begin
  2360. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  2361. end;
  2362. function totalvariance(const data : PDouble; const N : Integer) : float;
  2363. var mu: float;
  2364. begin
  2365. MeanAndTotalVariance( data, N, mu, result );
  2366. end;
  2367. function popnstddev(const data : array of Double) : float;
  2368. begin
  2369. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  2370. end;
  2371. function popnstddev(const data : PDouble; Const N : Integer) : float;
  2372. begin
  2373. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  2374. end;
  2375. function popnvariance(const data : array of Double) : float; inline;
  2376. begin
  2377. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  2378. end;
  2379. function popnvariance(const data : PDouble; Const N : Integer) : float;
  2380. begin
  2381. PopnVariance:=TotalVariance(Data,N)/N;
  2382. end;
  2383. procedure momentskewkurtosis(const data : array of Double;
  2384. out m1,m2,m3,m4,skew,kurtosis : float);
  2385. begin
  2386. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  2387. end;
  2388. procedure momentskewkurtosis(
  2389. const data: pdouble;
  2390. Const N: integer;
  2391. out m1: float;
  2392. out m2: float;
  2393. out m3: float;
  2394. out m4: float;
  2395. out skew: float;
  2396. out kurtosis: float
  2397. );
  2398. procedure CalcDevSums2to4(data: PDouble; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  2399. var
  2400. tm2, tm3, tm4, dev, dev2: float;
  2401. i: SizeInt;
  2402. m2to4Part0, m2to4Part1: TMoments2to4;
  2403. begin
  2404. if N >= RecursiveSumThreshold then
  2405. begin
  2406. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  2407. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  2408. for i := Low(TMoments2to4) to High(TMoments2to4) do
  2409. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  2410. end
  2411. else
  2412. begin
  2413. tm2 := 0;
  2414. tm3 := 0;
  2415. tm4 := 0;
  2416. for i := 0 to N - 1 do
  2417. begin
  2418. dev := data[i] - m1;
  2419. dev2 := sqr(dev);
  2420. tm2 := tm2 + dev2;
  2421. tm3 := tm3 + dev2 * dev;
  2422. tm4 := tm4 + sqr(dev2);
  2423. end;
  2424. m2to4[2] := tm2;
  2425. m2to4[3] := tm3;
  2426. m2to4[4] := tm4;
  2427. end;
  2428. end;
  2429. var
  2430. reciprocalN: float;
  2431. m2to4: TMoments2to4;
  2432. begin
  2433. m1 := 0;
  2434. reciprocalN := 1/N;
  2435. m1 := reciprocalN * sum(data, N);
  2436. CalcDevSums2to4(data, N, m1, m2to4);
  2437. m2 := reciprocalN * m2to4[2];
  2438. m3 := reciprocalN * m2to4[3];
  2439. m4 := reciprocalN * m2to4[4];
  2440. skew := m3 / (sqrt(m2)*m2);
  2441. kurtosis := m4 / (m2 * m2);
  2442. end;
  2443. function norm(const data : array of Double) : float; inline;
  2444. begin
  2445. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  2446. end;
  2447. function norm(const data : PDouble; Const N : Integer) : float;
  2448. begin
  2449. norm:=sqrt(sumofsquares(data,N));
  2450. end;
  2451. {$endif FPC_HAS_TYPE_DOUBLE}
  2452. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2453. procedure MeanAndTotalVariance
  2454. (const data: PExtended; N: LongInt; var mu, variance: float);
  2455. function CalcVariance(data: PExtended; N: SizeInt; mu: float): float;
  2456. var
  2457. i: SizeInt;
  2458. begin
  2459. if N>=RecursiveSumThreshold then
  2460. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  2461. else
  2462. begin
  2463. result:=0;
  2464. for i:=0 to N-1 do
  2465. result:=result+Sqr(data[i]-mu);
  2466. end;
  2467. end;
  2468. begin
  2469. mu := Mean( data, N );
  2470. variance := CalcVariance( data, N, mu );
  2471. end;
  2472. function stddev(const data : array of Extended) : float; inline;
  2473. begin
  2474. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  2475. end;
  2476. function stddev(const data : PExtended; Const N : Integer) : float;
  2477. begin
  2478. StdDev:=Sqrt(Variance(Data,N));
  2479. end;
  2480. procedure meanandstddev(const data : array of Extended;
  2481. var mean,stddev : float); inline;
  2482. begin
  2483. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  2484. end;
  2485. procedure meanandstddev
  2486. ( const data: PExtended;
  2487. const N: Longint;
  2488. var mean,
  2489. stdDev: Float
  2490. );
  2491. var totalVariance: float;
  2492. begin
  2493. MeanAndTotalVariance( data, N, mean, totalVariance );
  2494. if N < 2 then stdDev := 0
  2495. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  2496. end;
  2497. function variance(const data : array of Extended) : float; inline;
  2498. begin
  2499. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  2500. end;
  2501. function variance(const data : PExtended; Const N : Integer) : float;
  2502. begin
  2503. If N=1 then
  2504. Result:=0
  2505. else
  2506. Result:=TotalVariance(Data,N)/(N-1);
  2507. end;
  2508. function totalvariance(const data : array of Extended) : float; inline;
  2509. begin
  2510. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  2511. end;
  2512. function totalvariance(const data : PExtended;Const N : Integer) : float;
  2513. var mu: float;
  2514. begin
  2515. MeanAndTotalVariance( data, N, mu, result );
  2516. end;
  2517. function popnstddev(const data : array of Extended) : float;
  2518. begin
  2519. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  2520. end;
  2521. function popnstddev(const data : PExtended; Const N : Integer) : float;
  2522. begin
  2523. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  2524. end;
  2525. function popnvariance(const data : array of Extended) : float; inline;
  2526. begin
  2527. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  2528. end;
  2529. function popnvariance(const data : PExtended; Const N : Integer) : float;
  2530. begin
  2531. PopnVariance:=TotalVariance(Data,N)/N;
  2532. end;
  2533. procedure momentskewkurtosis(const data : array of Extended;
  2534. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  2535. begin
  2536. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  2537. end;
  2538. procedure momentskewkurtosis(
  2539. const data: pExtended;
  2540. Const N: Integer;
  2541. out m1: float;
  2542. out m2: float;
  2543. out m3: float;
  2544. out m4: float;
  2545. out skew: float;
  2546. out kurtosis: float
  2547. );
  2548. procedure CalcDevSums2to4(data: PExtended; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  2549. var
  2550. tm2, tm3, tm4, dev, dev2: float;
  2551. i: SizeInt;
  2552. m2to4Part0, m2to4Part1: TMoments2to4;
  2553. begin
  2554. if N >= RecursiveSumThreshold then
  2555. begin
  2556. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  2557. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  2558. for i := Low(TMoments2to4) to High(TMoments2to4) do
  2559. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  2560. end
  2561. else
  2562. begin
  2563. tm2 := 0;
  2564. tm3 := 0;
  2565. tm4 := 0;
  2566. for i := 0 to N - 1 do
  2567. begin
  2568. dev := data[i] - m1;
  2569. dev2 := sqr(dev);
  2570. tm2 := tm2 + dev2;
  2571. tm3 := tm3 + dev2 * dev;
  2572. tm4 := tm4 + sqr(dev2);
  2573. end;
  2574. m2to4[2] := tm2;
  2575. m2to4[3] := tm3;
  2576. m2to4[4] := tm4;
  2577. end;
  2578. end;
  2579. var
  2580. reciprocalN: float;
  2581. m2to4: TMoments2to4;
  2582. begin
  2583. m1 := 0;
  2584. reciprocalN := 1/N;
  2585. m1 := reciprocalN * sum(data, N);
  2586. CalcDevSums2to4(data, N, m1, m2to4);
  2587. m2 := reciprocalN * m2to4[2];
  2588. m3 := reciprocalN * m2to4[3];
  2589. m4 := reciprocalN * m2to4[4];
  2590. skew := m3 / (sqrt(m2)*m2);
  2591. kurtosis := m4 / (m2 * m2);
  2592. end;
  2593. function norm(const data : array of Extended) : float; inline;
  2594. begin
  2595. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  2596. end;
  2597. function norm(const data : PExtended; Const N : Integer) : float;
  2598. begin
  2599. norm:=sqrt(sumofsquares(data,N));
  2600. end;
  2601. {$endif FPC_HAS_TYPE_EXTENDED}
  2602. function MinIntValue(const Data: array of Integer): Integer;
  2603. var
  2604. I: SizeInt;
  2605. begin
  2606. Result := Data[Low(Data)];
  2607. For I := Succ(Low(Data)) To High(Data) Do
  2608. If Data[I] < Result Then Result := Data[I];
  2609. end;
  2610. function MaxIntValue(const Data: array of Integer): Integer;
  2611. var
  2612. I: SizeInt;
  2613. begin
  2614. Result := Data[Low(Data)];
  2615. For I := Succ(Low(Data)) To High(Data) Do
  2616. If Data[I] > Result Then Result := Data[I];
  2617. end;
  2618. function MinValue(const Data: array of Integer): Integer; inline;
  2619. begin
  2620. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  2621. end;
  2622. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  2623. var
  2624. I: SizeInt;
  2625. begin
  2626. Result := Data[0];
  2627. For I := 1 To N-1 do
  2628. If Data[I] < Result Then Result := Data[I];
  2629. end;
  2630. function MaxValue(const Data: array of Integer): Integer; inline;
  2631. begin
  2632. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  2633. end;
  2634. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  2635. var
  2636. i : SizeInt;
  2637. begin
  2638. { get an initial value }
  2639. maxvalue:=data[0];
  2640. for i:=1 to N-1 do
  2641. if data[i]>maxvalue then
  2642. maxvalue:=data[i];
  2643. end;
  2644. {$ifdef FPC_HAS_TYPE_SINGLE}
  2645. function minvalue(const data : array of Single) : Single; inline;
  2646. begin
  2647. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  2648. end;
  2649. function minvalue(const data : PSingle; Const N : Integer) : Single;
  2650. var
  2651. i : SizeInt;
  2652. begin
  2653. { get an initial value }
  2654. minvalue:=data[0];
  2655. for i:=1 to N-1 do
  2656. if data[i]<minvalue then
  2657. minvalue:=data[i];
  2658. end;
  2659. function maxvalue(const data : array of Single) : Single; inline;
  2660. begin
  2661. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  2662. end;
  2663. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  2664. var
  2665. i : SizeInt;
  2666. begin
  2667. { get an initial value }
  2668. maxvalue:=data[0];
  2669. for i:=1 to N-1 do
  2670. if data[i]>maxvalue then
  2671. maxvalue:=data[i];
  2672. end;
  2673. {$endif FPC_HAS_TYPE_SINGLE}
  2674. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2675. function minvalue(const data : array of Double) : Double; inline;
  2676. begin
  2677. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  2678. end;
  2679. function minvalue(const data : PDouble; Const N : Integer) : Double;
  2680. var
  2681. i : SizeInt;
  2682. begin
  2683. { get an initial value }
  2684. minvalue:=data[0];
  2685. for i:=1 to N-1 do
  2686. if data[i]<minvalue then
  2687. minvalue:=data[i];
  2688. end;
  2689. function maxvalue(const data : array of Double) : Double; inline;
  2690. begin
  2691. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  2692. end;
  2693. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  2694. var
  2695. i : SizeInt;
  2696. begin
  2697. { get an initial value }
  2698. maxvalue:=data[0];
  2699. for i:=1 to N-1 do
  2700. if data[i]>maxvalue then
  2701. maxvalue:=data[i];
  2702. end;
  2703. {$endif FPC_HAS_TYPE_DOUBLE}
  2704. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2705. function minvalue(const data : array of Extended) : Extended; inline;
  2706. begin
  2707. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  2708. end;
  2709. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  2710. var
  2711. i : SizeInt;
  2712. begin
  2713. { get an initial value }
  2714. minvalue:=data[0];
  2715. for i:=1 to N-1 do
  2716. if data[i]<minvalue then
  2717. minvalue:=data[i];
  2718. end;
  2719. function maxvalue(const data : array of Extended) : Extended; inline;
  2720. begin
  2721. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  2722. end;
  2723. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  2724. var
  2725. i : SizeInt;
  2726. begin
  2727. { get an initial value }
  2728. maxvalue:=data[0];
  2729. for i:=1 to N-1 do
  2730. if data[i]>maxvalue then
  2731. maxvalue:=data[i];
  2732. end;
  2733. {$endif FPC_HAS_TYPE_EXTENDED}
  2734. function Min(a, b: Integer): Integer;inline;
  2735. begin
  2736. if a < b then
  2737. Result := a
  2738. else
  2739. Result := b;
  2740. end;
  2741. function Max(a, b: Integer): Integer;inline;
  2742. begin
  2743. if a > b then
  2744. Result := a
  2745. else
  2746. Result := b;
  2747. end;
  2748. {
  2749. function Min(a, b: Cardinal): Cardinal;inline;
  2750. begin
  2751. if a < b then
  2752. Result := a
  2753. else
  2754. Result := b;
  2755. end;
  2756. function Max(a, b: Cardinal): Cardinal;inline;
  2757. begin
  2758. if a > b then
  2759. Result := a
  2760. else
  2761. Result := b;
  2762. end;
  2763. }
  2764. function Min(a, b: Int64): Int64;inline;
  2765. begin
  2766. if a < b then
  2767. Result := a
  2768. else
  2769. Result := b;
  2770. end;
  2771. function Max(a, b: Int64): Int64;inline;
  2772. begin
  2773. if a > b then
  2774. Result := a
  2775. else
  2776. Result := b;
  2777. end;
  2778. function Min(a, b: QWord): QWord; inline;
  2779. begin
  2780. if a < b then
  2781. Result := a
  2782. else
  2783. Result := b;
  2784. end;
  2785. function Max(a, b: QWord): Qword;inline;
  2786. begin
  2787. if a > b then
  2788. Result := a
  2789. else
  2790. Result := b;
  2791. end;
  2792. {$ifdef FPC_HAS_TYPE_SINGLE}
  2793. function Min(a, b: Single): Single;inline;
  2794. begin
  2795. if a < b then
  2796. Result := a
  2797. else
  2798. Result := b;
  2799. end;
  2800. function Max(a, b: Single): Single;inline;
  2801. begin
  2802. if a > b then
  2803. Result := a
  2804. else
  2805. Result := b;
  2806. end;
  2807. {$endif FPC_HAS_TYPE_SINGLE}
  2808. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2809. function Min(a, b: Double): Double;inline;
  2810. begin
  2811. if a < b then
  2812. Result := a
  2813. else
  2814. Result := b;
  2815. end;
  2816. function Max(a, b: Double): Double;inline;
  2817. begin
  2818. if a > b then
  2819. Result := a
  2820. else
  2821. Result := b;
  2822. end;
  2823. {$endif FPC_HAS_TYPE_DOUBLE}
  2824. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2825. function Min(a, b: Extended): Extended;inline;
  2826. begin
  2827. if a < b then
  2828. Result := a
  2829. else
  2830. Result := b;
  2831. end;
  2832. function Max(a, b: Extended): Extended;inline;
  2833. begin
  2834. if a > b then
  2835. Result := a
  2836. else
  2837. Result := b;
  2838. end;
  2839. {$endif FPC_HAS_TYPE_EXTENDED}
  2840. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  2841. begin
  2842. Result:=(AValue>=AMin) and (AValue<=AMax);
  2843. end;
  2844. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  2845. begin
  2846. Result:=(AValue>=AMin) and (AValue<=AMax);
  2847. end;
  2848. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2849. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  2850. begin
  2851. Result:=(AValue>=AMin) and (AValue<=AMax);
  2852. end;
  2853. {$endif FPC_HAS_TYPE_DOUBLE}
  2854. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  2855. begin
  2856. Result:=AValue;
  2857. If Result<AMin then
  2858. Result:=AMin;
  2859. if Result>AMax then
  2860. Result:=AMax;
  2861. end;
  2862. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  2863. begin
  2864. Result:=AValue;
  2865. If Result<AMin then
  2866. Result:=AMin;
  2867. if Result>AMax then
  2868. Result:=AMax;
  2869. end;
  2870. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2871. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  2872. begin
  2873. Result:=AValue;
  2874. If Result<AMin then
  2875. Result:=AMin;
  2876. if Result>AMax then
  2877. Result:=AMax;
  2878. end;
  2879. {$endif FPC_HAS_TYPE_DOUBLE}
  2880. Const
  2881. EZeroResolution = Extended(1E-16);
  2882. DZeroResolution = Double(1E-12);
  2883. SZeroResolution = Single(1E-4);
  2884. function IsZero(const A: Single; Epsilon: Single): Boolean;
  2885. begin
  2886. if (Epsilon=0) then
  2887. Epsilon:=SZeroResolution;
  2888. Result:=Abs(A)<=Epsilon;
  2889. end;
  2890. function IsZero(const A: Single): Boolean;inline;
  2891. begin
  2892. Result:=IsZero(A,single(SZeroResolution));
  2893. end;
  2894. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2895. function IsZero(const A: Double; Epsilon: Double): Boolean;
  2896. begin
  2897. if (Epsilon=0) then
  2898. Epsilon:=DZeroResolution;
  2899. Result:=Abs(A)<=Epsilon;
  2900. end;
  2901. function IsZero(const A: Double): Boolean;inline;
  2902. begin
  2903. Result:=IsZero(A,DZeroResolution);
  2904. end;
  2905. {$endif FPC_HAS_TYPE_DOUBLE}
  2906. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2907. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  2908. begin
  2909. if (Epsilon=0) then
  2910. Epsilon:=EZeroResolution;
  2911. Result:=Abs(A)<=Epsilon;
  2912. end;
  2913. function IsZero(const A: Extended): Boolean;inline;
  2914. begin
  2915. Result:=IsZero(A,EZeroResolution);
  2916. end;
  2917. {$endif FPC_HAS_TYPE_EXTENDED}
  2918. type
  2919. TSplitDouble = packed record
  2920. cards: Array[0..1] of cardinal;
  2921. end;
  2922. TSplitExtended = packed record
  2923. cards: Array[0..1] of cardinal;
  2924. w: word;
  2925. end;
  2926. function IsNan(const d : Single): Boolean; overload;
  2927. begin
  2928. result:=(longword(d) and $7fffffff)>$7f800000;
  2929. end;
  2930. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2931. function IsNan(const d : Double): Boolean;
  2932. var
  2933. fraczero, expMaximal: boolean;
  2934. begin
  2935. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2936. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  2937. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  2938. (TSplitDouble(d).cards[1] = 0);
  2939. {$else FPC_BIG_ENDIAN}
  2940. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  2941. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  2942. (TSplitDouble(d).cards[0] = 0);
  2943. {$endif FPC_BIG_ENDIAN}
  2944. Result:=expMaximal and not(fraczero);
  2945. end;
  2946. {$endif FPC_HAS_TYPE_DOUBLE}
  2947. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2948. function IsNan(const d : Extended): Boolean; overload;
  2949. var
  2950. fraczero, expMaximal: boolean;
  2951. begin
  2952. {$ifdef FPC_BIG_ENDIAN}
  2953. {$error no support for big endian extended type yet}
  2954. {$else FPC_BIG_ENDIAN}
  2955. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  2956. fraczero := (TSplitExtended(d).cards[0] = 0) and
  2957. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  2958. {$endif FPC_BIG_ENDIAN}
  2959. Result:=expMaximal and not(fraczero);
  2960. end;
  2961. {$endif FPC_HAS_TYPE_EXTENDED}
  2962. function IsInfinite(const d : Single): Boolean; overload;
  2963. begin
  2964. result:=(longword(d) and $7fffffff)=$7f800000;
  2965. end;
  2966. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2967. function IsInfinite(const d : Double): Boolean; overload;
  2968. var
  2969. fraczero, expMaximal: boolean;
  2970. begin
  2971. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2972. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  2973. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  2974. (TSplitDouble(d).cards[1] = 0);
  2975. {$else FPC_BIG_ENDIAN}
  2976. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  2977. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  2978. (TSplitDouble(d).cards[0] = 0);
  2979. {$endif FPC_BIG_ENDIAN}
  2980. Result:=expMaximal and fraczero;
  2981. end;
  2982. {$endif FPC_HAS_TYPE_DOUBLE}
  2983. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2984. function IsInfinite(const d : Extended): Boolean; overload;
  2985. var
  2986. fraczero, expMaximal: boolean;
  2987. begin
  2988. {$ifdef FPC_BIG_ENDIAN}
  2989. {$error no support for big endian extended type yet}
  2990. {$else FPC_BIG_ENDIAN}
  2991. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  2992. fraczero := (TSplitExtended(d).cards[0] = 0) and
  2993. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  2994. {$endif FPC_BIG_ENDIAN}
  2995. Result:=expMaximal and fraczero;
  2996. end;
  2997. {$endif FPC_HAS_TYPE_EXTENDED}
  2998. function copysign(x,y: float): float;
  2999. begin
  3000. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  3001. {$error copysign not yet implemented for float128}
  3002. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  3003. TSplitExtended(x).w:=(TSplitExtended(x).w and $7fff) or (TSplitExtended(y).w and $8000);
  3004. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  3005. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  3006. TSplitDouble(x).cards[0]:=(TSplitDouble(x).cards[0] and $7fffffff) or (TSplitDouble(y).cards[0] and longword($80000000));
  3007. {$else}
  3008. TSplitDouble(x).cards[1]:=(TSplitDouble(x).cards[1] and $7fffffff) or (TSplitDouble(y).cards[1] and longword($80000000));
  3009. {$endif}
  3010. {$else}
  3011. longword(x):=longword(x and $7fffffff) or (longword(y) and longword($80000000));
  3012. {$endif}
  3013. result:=x;
  3014. end;
  3015. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3016. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  3017. begin
  3018. if (Epsilon=0) then
  3019. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  3020. if (A>B) then
  3021. Result:=((A-B)<=Epsilon)
  3022. else
  3023. Result:=((B-A)<=Epsilon);
  3024. end;
  3025. function SameValue(const A, B: Extended): Boolean;inline;
  3026. begin
  3027. Result:=SameValue(A,B,0.0);
  3028. end;
  3029. {$endif FPC_HAS_TYPE_EXTENDED}
  3030. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3031. function SameValue(const A, B: Double): Boolean;inline;
  3032. begin
  3033. Result:=SameValue(A,B,0.0);
  3034. end;
  3035. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  3036. begin
  3037. if (Epsilon=0) then
  3038. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  3039. if (A>B) then
  3040. Result:=((A-B)<=Epsilon)
  3041. else
  3042. Result:=((B-A)<=Epsilon);
  3043. end;
  3044. {$endif FPC_HAS_TYPE_DOUBLE}
  3045. function SameValue(const A, B: Single): Boolean;inline;
  3046. begin
  3047. Result:=SameValue(A,B,0);
  3048. end;
  3049. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  3050. begin
  3051. if (Epsilon=0) then
  3052. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  3053. if (A>B) then
  3054. Result:=((A-B)<=Epsilon)
  3055. else
  3056. Result:=((B-A)<=Epsilon);
  3057. end;
  3058. // Some CPUs probably allow a faster way of doing this in a single operation...
  3059. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  3060. {$ifndef FPC_MATH_HAS_DIVMOD}
  3061. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  3062. begin
  3063. if Dividend < 0 then
  3064. begin
  3065. { Use DivMod with >=0 dividend }
  3066. Dividend:=-Dividend;
  3067. { The documented behavior of Pascal's div/mod operators and DivMod
  3068. on negative dividends is to return Result closer to zero and
  3069. a negative Remainder. Which means that we can just negate both
  3070. Result and Remainder, and all it's Ok. }
  3071. Result:=-(Dividend Div Divisor);
  3072. Remainder:=-(Dividend+(Result*Divisor));
  3073. end
  3074. else
  3075. begin
  3076. Result:=Dividend Div Divisor;
  3077. Remainder:=Dividend-(Result*Divisor);
  3078. end;
  3079. end;
  3080. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  3081. begin
  3082. if Dividend < 0 then
  3083. begin
  3084. { Use DivMod with >=0 dividend }
  3085. Dividend:=-Dividend;
  3086. { The documented behavior of Pascal's div/mod operators and DivMod
  3087. on negative dividends is to return Result closer to zero and
  3088. a negative Remainder. Which means that we can just negate both
  3089. Result and Remainder, and all it's Ok. }
  3090. Result:=-(Dividend Div Divisor);
  3091. Remainder:=-(Dividend+(Result*Divisor));
  3092. end
  3093. else
  3094. begin
  3095. Result:=Dividend Div Divisor;
  3096. Remainder:=Dividend-(Result*Divisor);
  3097. end;
  3098. end;
  3099. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  3100. begin
  3101. Result:=Dividend Div Divisor;
  3102. Remainder:=Dividend-(Result*Divisor);
  3103. end;
  3104. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  3105. begin
  3106. if Dividend < 0 then
  3107. begin
  3108. { Use DivMod with >=0 dividend }
  3109. Dividend:=-Dividend;
  3110. { The documented behavior of Pascal's div/mod operators and DivMod
  3111. on negative dividends is to return Result closer to zero and
  3112. a negative Remainder. Which means that we can just negate both
  3113. Result and Remainder, and all it's Ok. }
  3114. Result:=-(Dividend Div Divisor);
  3115. Remainder:=-(Dividend+(Result*Divisor));
  3116. end
  3117. else
  3118. begin
  3119. Result:=Dividend Div Divisor;
  3120. Remainder:=Dividend-(Result*Divisor);
  3121. end;
  3122. end;
  3123. {$endif FPC_MATH_HAS_DIVMOD}
  3124. { Floating point modulo}
  3125. {$ifdef FPC_HAS_TYPE_SINGLE}
  3126. function FMod(const a, b: Single): Single;inline;overload;
  3127. begin
  3128. result:= a-b * Int(a/b);
  3129. end;
  3130. {$endif FPC_HAS_TYPE_SINGLE}
  3131. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3132. function FMod(const a, b: Double): Double;inline;overload;
  3133. begin
  3134. result:= a-b * Int(a/b);
  3135. end;
  3136. {$endif FPC_HAS_TYPE_DOUBLE}
  3137. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3138. function FMod(const a, b: Extended): Extended;inline;overload;
  3139. begin
  3140. result:= a-b * Int(a/b);
  3141. end;
  3142. {$endif FPC_HAS_TYPE_EXTENDED}
  3143. operator mod(const a,b:float) c:float;inline;
  3144. begin
  3145. c:= a-b * Int(a/b);
  3146. if SameValue(abs(c),abs(b)) then
  3147. c:=0.0;
  3148. end;
  3149. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  3150. begin
  3151. if val then result:=iftrue else result:=iffalse;
  3152. end;
  3153. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  3154. begin
  3155. if val then result:=iftrue else result:=iffalse;
  3156. end;
  3157. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  3158. begin
  3159. if val then result:=iftrue else result:=iffalse;
  3160. end;
  3161. // dilemma here. asm can do the two comparisons in one go?
  3162. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  3163. function CompareValue(const A, B : Integer): TValueRelationship;
  3164. begin
  3165. result:=GreaterThanValue;
  3166. if a=b then
  3167. result:=EqualsValue
  3168. else
  3169. if a<b then
  3170. result:=LessThanValue;
  3171. end;
  3172. function CompareValue(const A, B: Int64): TValueRelationship;
  3173. begin
  3174. result:=GreaterThanValue;
  3175. if a=b then
  3176. result:=EqualsValue
  3177. else
  3178. if a<b then
  3179. result:=LessThanValue;
  3180. end;
  3181. function CompareValue(const A, B: QWord): TValueRelationship;
  3182. begin
  3183. result:=GreaterThanValue;
  3184. if a=b then
  3185. result:=EqualsValue
  3186. else
  3187. if a<b then
  3188. result:=LessThanValue;
  3189. end;
  3190. {$ifdef FPC_HAS_TYPE_SINGLE}
  3191. function CompareValue(const A, B: Single; delta: Single = 0.0): TValueRelationship;
  3192. begin
  3193. result:=GreaterThanValue;
  3194. if abs(a-b)<=delta then
  3195. result:=EqualsValue
  3196. else
  3197. if a<b then
  3198. result:=LessThanValue;
  3199. end;
  3200. {$endif}
  3201. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3202. function CompareValue(const A, B: Double; delta: Double = 0.0): TValueRelationship;
  3203. begin
  3204. result:=GreaterThanValue;
  3205. if abs(a-b)<=delta then
  3206. result:=EqualsValue
  3207. else
  3208. if a<b then
  3209. result:=LessThanValue;
  3210. end;
  3211. {$endif}
  3212. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3213. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  3214. begin
  3215. result:=GreaterThanValue;
  3216. if abs(a-b)<=delta then
  3217. result:=EqualsValue
  3218. else
  3219. if a<b then
  3220. result:=LessThanValue;
  3221. end;
  3222. {$endif}
  3223. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3224. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  3225. var
  3226. RV : Double;
  3227. begin
  3228. RV:=IntPower(10,Digits);
  3229. Result:=Round(AValue/RV)*RV;
  3230. end;
  3231. {$endif}
  3232. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3233. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  3234. var
  3235. RV : Extended;
  3236. begin
  3237. RV:=IntPower(10,Digits);
  3238. Result:=Round(AValue/RV)*RV;
  3239. end;
  3240. {$endif}
  3241. {$ifdef FPC_HAS_TYPE_SINGLE}
  3242. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  3243. var
  3244. RV : Single;
  3245. begin
  3246. RV:=IntPower(10,Digits);
  3247. Result:=Round(AValue/RV)*RV;
  3248. end;
  3249. {$endif}
  3250. {$ifdef FPC_HAS_TYPE_SINGLE}
  3251. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  3252. var
  3253. RV : Single;
  3254. begin
  3255. RV := IntPower(10, -Digits);
  3256. if AValue < 0 then
  3257. Result := Int((AValue*RV) - 0.5)/RV
  3258. else
  3259. Result := Int((AValue*RV) + 0.5)/RV;
  3260. end;
  3261. {$endif}
  3262. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3263. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  3264. var
  3265. RV : Double;
  3266. begin
  3267. RV := IntPower(10, -Digits);
  3268. if AValue < 0 then
  3269. Result := Int((AValue*RV) - 0.5)/RV
  3270. else
  3271. Result := Int((AValue*RV) + 0.5)/RV;
  3272. end;
  3273. {$endif}
  3274. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3275. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  3276. var
  3277. RV : Extended;
  3278. begin
  3279. RV := IntPower(10, -Digits);
  3280. if AValue < 0 then
  3281. Result := Int((AValue*RV) - 0.5)/RV
  3282. else
  3283. Result := Int((AValue*RV) + 0.5)/RV;
  3284. end;
  3285. {$endif}
  3286. function RandomFrom(const AValues: array of Double): Double; overload;
  3287. begin
  3288. result:=AValues[random(High(AValues)+1)];
  3289. end;
  3290. function RandomFrom(const AValues: array of Integer): Integer; overload;
  3291. begin
  3292. result:=AValues[random(High(AValues)+1)];
  3293. end;
  3294. function RandomFrom(const AValues: array of Int64): Int64; overload;
  3295. begin
  3296. result:=AValues[random(High(AValues)+1)];
  3297. end;
  3298. {$if FPC_FULLVERSION >=30101}
  3299. generic function RandomFrom<T>(const AValues:array of T):T;
  3300. begin
  3301. result:=AValues[random(High(AValues)+1)];
  3302. end;
  3303. {$endif}
  3304. function FutureValue(ARate: Float; NPeriods: Integer;
  3305. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  3306. var
  3307. q, qn, factor: Float;
  3308. begin
  3309. if ARate = 0 then
  3310. Result := -APresentValue - APayment * NPeriods
  3311. else begin
  3312. q := 1.0 + ARate;
  3313. qn := power(q, NPeriods);
  3314. factor := (qn - 1) / (q - 1);
  3315. if APaymentTime = ptStartOfPeriod then
  3316. factor := factor * q;
  3317. Result := -(APresentValue * qn + APayment*factor);
  3318. end;
  3319. end;
  3320. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  3321. APaymentTime: TPaymentTime): Float;
  3322. { The interest rate cannot be calculated analytically. We solve the equation
  3323. numerically by means of the Newton method:
  3324. - guess value for the interest reate
  3325. - calculate at which interest rate the tangent of the curve fv(rate)
  3326. (straight line!) has the requested future vale.
  3327. - use this rate for the next iteration. }
  3328. const
  3329. DELTA = 0.001;
  3330. EPS = 1E-9; // required precision of interest rate (after typ. 6 iterations)
  3331. MAXIT = 20; // max iteration count to protect agains non-convergence
  3332. var
  3333. r1, r2, dr: Float;
  3334. fv1, fv2: Float;
  3335. iteration: Integer;
  3336. begin
  3337. iteration := 0;
  3338. r1 := 0.05; // inital guess
  3339. repeat
  3340. r2 := r1 + DELTA;
  3341. fv1 := FutureValue(r1, NPeriods, APayment, APresentValue, APaymentTime);
  3342. fv2 := FutureValue(r2, NPeriods, APayment, APresentValue, APaymentTime);
  3343. dr := (AFutureValue - fv1) / (fv2 - fv1) * delta; // tangent at fv(r)
  3344. r1 := r1 + dr; // next guess
  3345. inc(iteration);
  3346. until (abs(dr) < EPS) or (iteration >= MAXIT);
  3347. Result := r1;
  3348. end;
  3349. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  3350. APaymentTime: TPaymentTime): Float;
  3351. { Solve the cash flow equation (1) for q^n and take the logarithm }
  3352. var
  3353. q, x1, x2: Float;
  3354. begin
  3355. if ARate = 0 then
  3356. Result := -(APresentValue + AFutureValue) / APayment
  3357. else begin
  3358. q := 1.0 + ARate;
  3359. if APaymentTime = ptStartOfPeriod then
  3360. APayment := APayment * q;
  3361. x1 := APayment - AFutureValue * ARate;
  3362. x2 := APayment + APresentValue * ARate;
  3363. if (x2 = 0) // we have to divide by x2
  3364. or (sign(x1) * sign(x2) < 0) // the argument of the log is negative
  3365. then
  3366. Result := Infinity
  3367. else begin
  3368. Result := ln(x1/x2) / ln(q);
  3369. end;
  3370. end;
  3371. end;
  3372. function Payment(ARate: Float; NPeriods: Integer;
  3373. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  3374. var
  3375. q, qn, factor: Float;
  3376. begin
  3377. if ARate = 0 then
  3378. Result := -(AFutureValue + APresentValue) / NPeriods
  3379. else begin
  3380. q := 1.0 + ARate;
  3381. qn := power(q, NPeriods);
  3382. factor := (qn - 1) / (q - 1);
  3383. if APaymentTime = ptStartOfPeriod then
  3384. factor := factor * q;
  3385. Result := -(AFutureValue + APresentValue * qn) / factor;
  3386. end;
  3387. end;
  3388. function PresentValue(ARate: Float; NPeriods: Integer;
  3389. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  3390. var
  3391. q, qn, factor: Float;
  3392. begin
  3393. if ARate = 0.0 then
  3394. Result := -AFutureValue - APayment * NPeriods
  3395. else begin
  3396. q := 1.0 + ARate;
  3397. qn := power(q, NPeriods);
  3398. factor := (qn - 1) / (q - 1);
  3399. if APaymentTime = ptStartOfPeriod then
  3400. factor := factor * q;
  3401. Result := -(AFutureValue + APayment*factor) / qn;
  3402. end;
  3403. end;
  3404. {$else}
  3405. implementation
  3406. {$endif FPUNONE}
  3407. end.