math.pp 96 KB

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