math.pp 93 KB

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