math.pp 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694
  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. if abs(x)=1.0 then
  997. if x<0.0 then
  998. arccos:=Pi
  999. else
  1000. arccos:=0
  1001. else
  1002. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  1003. end;
  1004. {$ENDIF}
  1005. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1006. function Arccos(x : Double) : Double;
  1007. begin
  1008. if abs(x)=1.0 then
  1009. if x<0.0 then
  1010. arccos:=Pi
  1011. else
  1012. arccos:=0
  1013. else
  1014. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  1015. end;
  1016. {$ENDIF}
  1017. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1018. function Arccos(x : Extended) : Extended;
  1019. begin
  1020. if abs(x)=1.0 then
  1021. if x<0.0 then
  1022. arccos:=Pi
  1023. else
  1024. arccos:=0
  1025. else
  1026. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  1027. end;
  1028. {$ENDIF}
  1029. {$ifndef FPC_MATH_HAS_ARCTAN2}
  1030. function arctan2(y,x : float) : float;
  1031. begin
  1032. if x=0 then
  1033. begin
  1034. if y=0 then
  1035. result:=0.0
  1036. else if y>0 then
  1037. result:=pi/2
  1038. else
  1039. result:=-pi/2;
  1040. end
  1041. else
  1042. begin
  1043. result:=ArcTan(y/x);
  1044. if x<0 then
  1045. if y<0 then
  1046. result:=result-pi
  1047. else
  1048. result:=result+pi;
  1049. end;
  1050. end;
  1051. {$endif FPC_MATH_HAS_ARCTAN2}
  1052. function cosh(x : float) : float;
  1053. var
  1054. temp : float;
  1055. begin
  1056. temp:=exp(x);
  1057. cosh:=0.5*(temp+1.0/temp);
  1058. end;
  1059. function sinh(x : float) : float;
  1060. var
  1061. temp : float;
  1062. begin
  1063. temp:=exp(x);
  1064. { copysign ensures that sinh(-0.0)=-0.0 }
  1065. sinh:=copysign(0.5*(temp-1.0/temp),x);
  1066. end;
  1067. function tanh(x : float) : float;
  1068. var
  1069. tmp:float;
  1070. begin
  1071. if x < 0 then begin
  1072. tmp:=exp(2*x);
  1073. result:=(tmp-1)/(1+tmp)
  1074. end
  1075. else begin
  1076. tmp:=exp(-2*x);
  1077. result:=(1-tmp)/(1+tmp)
  1078. end;
  1079. end;
  1080. {$ifdef FPC_HAS_TYPE_SINGLE}
  1081. function SecH(const X: Single): Single;
  1082. var
  1083. Ex: ValReal;
  1084. begin
  1085. //https://en.wikipedia.org/wiki/Hyperbolic_functions#Definitions
  1086. //SecH = 2 / (e^X + e^-X)
  1087. Ex:=Exp(X);
  1088. SecH:=2/(Ex+1/Ex);
  1089. end;
  1090. {$ENDIF}
  1091. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1092. function SecH(const X: Double): Double;
  1093. var
  1094. Ex: ValReal;
  1095. begin
  1096. Ex:=Exp(X);
  1097. SecH:=2/(Ex+1/Ex);
  1098. end;
  1099. {$ENDIF}
  1100. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1101. function SecH(const X: Extended): Extended;
  1102. var
  1103. Ex: Extended;
  1104. begin
  1105. Ex:=Exp(X);
  1106. SecH:=2/(Ex+1/Ex);
  1107. end;
  1108. {$ENDIF}
  1109. {$ifdef FPC_HAS_TYPE_SINGLE}
  1110. function CscH(const X: Single): Single;
  1111. var
  1112. Ex: ValReal;
  1113. begin
  1114. //CscH = 2 / (e^X - e^-X)
  1115. Ex:=Exp(X);
  1116. CscH:=2/(Ex-1/Ex);
  1117. end;
  1118. {$ENDIF}
  1119. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1120. function CscH(const X: Double): Double;
  1121. var
  1122. Ex: ValReal;
  1123. begin
  1124. Ex:=Exp(X);
  1125. CscH:=2/(Ex-1/Ex);
  1126. end;
  1127. {$ENDIF}
  1128. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1129. function CscH(const X: Extended): Extended;
  1130. var
  1131. Ex: Extended;
  1132. begin
  1133. Ex:=Exp(X);
  1134. CscH:=2/(Ex-1/Ex);
  1135. end;
  1136. {$ENDIF}
  1137. {$ifdef FPC_HAS_TYPE_SINGLE}
  1138. function CotH(const X: Single): Single;
  1139. var
  1140. Ex, Emx: ValReal;
  1141. begin
  1142. //CotH = (e^X + e^-X) / (e^X - e^-X)
  1143. Ex:=Exp(X);
  1144. Emx:=1/Ex;
  1145. CotH:=(Ex+Emx)/(Ex-Emx);
  1146. end;
  1147. {$ENDIF}
  1148. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1149. function CotH(const X: Double): Double;
  1150. var
  1151. Ex, Emx: ValReal;
  1152. begin
  1153. Ex:=Exp(X);
  1154. Emx:=1/Ex;
  1155. CotH:=(Ex+Emx)/(Ex-Emx);
  1156. end;
  1157. {$ENDIF}
  1158. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1159. function CotH(const X: Extended): Extended;
  1160. var
  1161. Ex, Emx: Extended;
  1162. begin
  1163. Ex:=Exp(X);
  1164. Emx:=1/Ex;
  1165. CotH:=(Ex+Emx)/(Ex-Emx);
  1166. end;
  1167. {$ENDIF}
  1168. function arccosh(x : float) : float; inline;
  1169. begin
  1170. arccosh:=arcosh(x);
  1171. end;
  1172. function arcsinh(x : float) : float;inline;
  1173. begin
  1174. arcsinh:=arsinh(x);
  1175. end;
  1176. function arctanh(x : float) : float;inline;
  1177. begin
  1178. arctanh:=artanh(x);
  1179. end;
  1180. function arcosh(x : float) : float;
  1181. begin
  1182. { Provides accuracy about 4*eps near 1.0 }
  1183. arcosh:=Ln(x+Sqrt((x-1.0)*(x+1.0)));
  1184. end;
  1185. function arsinh(x : float) : float;
  1186. var
  1187. z: float;
  1188. begin
  1189. z:=abs(x);
  1190. z:=Ln(z+Sqrt(1+z*z));
  1191. { copysign ensures that arsinh(-Inf)=-Inf and arsinh(-0.0)=-0.0 }
  1192. arsinh:=copysign(z,x);
  1193. end;
  1194. function artanh(x : float) : float;
  1195. begin
  1196. artanh:=(lnxp1(x)-lnxp1(-x))*0.5;
  1197. end;
  1198. {$ifdef FPC_HAS_TYPE_SINGLE}
  1199. function ArcSec(X: Single): Single;
  1200. begin
  1201. ArcSec:=ArcCos(1/X);
  1202. end;
  1203. {$ENDIF}
  1204. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1205. function ArcSec(X: Double): Double;
  1206. begin
  1207. ArcSec:=ArcCos(1/X);
  1208. end;
  1209. {$ENDIF}
  1210. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1211. function ArcSec(X: Extended): Extended;
  1212. begin
  1213. ArcSec:=ArcCos(1/X);
  1214. end;
  1215. {$ENDIF}
  1216. {$ifdef FPC_HAS_TYPE_SINGLE}
  1217. function ArcCsc(X: Single): Single;
  1218. begin
  1219. ArcCsc:=ArcSin(1/X);
  1220. end;
  1221. {$ENDIF}
  1222. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1223. function ArcCsc(X: Double): Double;
  1224. begin
  1225. ArcCsc:=ArcSin(1/X);
  1226. end;
  1227. {$ENDIF}
  1228. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1229. function ArcCsc(X: Extended): Extended;
  1230. begin
  1231. ArcCsc:=ArcSin(1/X);
  1232. end;
  1233. {$ENDIF}
  1234. {$ifdef FPC_HAS_TYPE_SINGLE}
  1235. function ArcCot(X: Single): Single;
  1236. begin
  1237. if x=0 then
  1238. ArcCot:=0.5*pi
  1239. else
  1240. ArcCot:=ArcTan(1/X);
  1241. end;
  1242. {$ENDIF}
  1243. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1244. function ArcCot(X: Double): Double;
  1245. begin
  1246. begin
  1247. if x=0 then
  1248. ArcCot:=0.5*pi
  1249. else
  1250. ArcCot:=ArcTan(1/X);
  1251. end;
  1252. end;
  1253. {$ENDIF}
  1254. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1255. function ArcCot(X: Extended): Extended;
  1256. begin
  1257. begin
  1258. if x=0 then
  1259. ArcCot:=0.5*pi
  1260. else
  1261. ArcCot:=ArcTan(1/X);
  1262. end;
  1263. end;
  1264. {$ENDIF}
  1265. {$ifdef FPC_HAS_TYPE_SINGLE}
  1266. function ArcSecH(X : Single): Single;
  1267. begin
  1268. ArcSecH:=ln((1+(sqrt(1.0-sqr(X))))/X); //replacing division inside ln() by subtracting 2 ln()'s seems to be slower
  1269. end;
  1270. {$ENDIF}
  1271. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1272. function ArcSecH(X : Double): Double;
  1273. begin
  1274. ArcSecH:=ln((1+(sqrt(1.0-sqr(X))))/X);
  1275. end;
  1276. {$ENDIF}
  1277. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1278. function ArcSecH(X : Extended): Extended;
  1279. begin
  1280. ArcSecH:=ln((1+(sqrt(1.0-sqr(X))))/X);
  1281. end;
  1282. {$ENDIF}
  1283. {$ifdef FPC_HAS_TYPE_SINGLE}
  1284. function ArcCscH(X: Single): Single;
  1285. begin
  1286. ArcCscH:=ln((1.0/X)+sqrt(1.0/(sqr(x))+1.0));
  1287. end;
  1288. {$ENDIF}
  1289. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1290. function ArcCscH(X: Double): Double;
  1291. begin
  1292. ArcCscH:=ln((1.0/X)+sqrt(1.0/(sqr(x))+1.0));
  1293. end;
  1294. {$ENDIF}
  1295. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1296. function ArcCscH(X: Extended): Extended;
  1297. begin
  1298. ArcCscH:=ln((1.0/X)+sqrt(1.0/(sqr(x))+1.0));
  1299. end;
  1300. {$ENDIF}
  1301. {$ifdef FPC_HAS_TYPE_SINGLE}
  1302. function ArcCotH(X: Single): Single;
  1303. begin
  1304. ArcCotH:=0.5*ln((x + 1.0)/(x - 1.0));
  1305. end;
  1306. {$ENDIF}
  1307. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1308. function ArcCotH(X: Double): Double;
  1309. begin
  1310. ArcCotH:=0.5*ln((x + 1.0)/(x - 1.0));
  1311. end;
  1312. {$ENDIF}
  1313. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1314. function ArcCotH(X: Extended): Extended;
  1315. begin
  1316. ArcCotH:=0.5*ln((x + 1.0)/(x - 1.0));
  1317. end;
  1318. {$ENDIF}
  1319. { hypot function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  1320. function hypot(x,y : float) : float;
  1321. begin
  1322. x:=abs(x);
  1323. y:=abs(y);
  1324. if (x>y) then
  1325. hypot:=x*sqrt(1.0+sqr(y/x))
  1326. else if (x>0.0) then
  1327. hypot:=y*sqrt(1.0+sqr(x/y))
  1328. else
  1329. hypot:=y;
  1330. end;
  1331. function log10(x : float) : float;
  1332. begin
  1333. log10:=ln(x)*0.43429448190325182765; { 1/ln(10) }
  1334. end;
  1335. {$ifndef FPC_MATH_HAS_LOG2}
  1336. function log2(x : float) : float;
  1337. begin
  1338. log2:=ln(x)*1.4426950408889634079; { 1/ln(2) }
  1339. end;
  1340. {$endif FPC_MATH_HAS_LOG2}
  1341. function logn(n,x : float) : float;
  1342. begin
  1343. logn:=ln(x)/ln(n);
  1344. end;
  1345. { lnxp1 function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  1346. function lnxp1(x : float) : float;
  1347. var
  1348. y: float;
  1349. begin
  1350. if (x>=4.0) then
  1351. lnxp1:=ln(1.0+x)
  1352. else
  1353. begin
  1354. y:=1.0+x;
  1355. if (y=1.0) then
  1356. lnxp1:=x
  1357. else
  1358. begin
  1359. lnxp1:=ln(y); { lnxp1(-1) = ln(0) = -Inf }
  1360. if y>0.0 then
  1361. lnxp1:=lnxp1+(x-(y-1.0))/y;
  1362. end;
  1363. end;
  1364. end;
  1365. function power(base,exponent : float) : float;
  1366. begin
  1367. if Exponent=0.0 then
  1368. result:=1.0
  1369. else if (base=0.0) and (exponent>0.0) then
  1370. result:=0.0
  1371. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  1372. result:=intpower(base,trunc(exponent))
  1373. else
  1374. result:=exp(exponent * ln (base));
  1375. end;
  1376. function intpower(base : float;exponent : longint) : float;
  1377. begin
  1378. if exponent<0 then
  1379. begin
  1380. base:=1.0/base;
  1381. exponent:=-exponent;
  1382. end;
  1383. intpower:=1.0;
  1384. while exponent<>0 do
  1385. begin
  1386. if exponent and 1<>0 then
  1387. intpower:=intpower*base;
  1388. exponent:=exponent shr 1;
  1389. base:=sqr(base);
  1390. end;
  1391. end;
  1392. operator ** (base,exponent : float) e: float; inline;
  1393. begin
  1394. e:=power(base,exponent);
  1395. end;
  1396. operator ** (base,exponent : int64) res: int64;
  1397. begin
  1398. if exponent<0 then
  1399. begin
  1400. if base<=0 then
  1401. raise EInvalidArgument.Create('Non-positive base with negative exponent in **');
  1402. if base=1 then
  1403. res:=1
  1404. else
  1405. res:=0;
  1406. exit;
  1407. end;
  1408. res:=1;
  1409. while exponent<>0 do
  1410. begin
  1411. if exponent and 1<>0 then
  1412. res:=res*base;
  1413. exponent:=exponent shr 1;
  1414. base:=base*base;
  1415. end;
  1416. end;
  1417. function ceil(x : float) : integer;
  1418. begin
  1419. Result:=Trunc(x)+ord(Frac(x)>0);
  1420. end;
  1421. function ceil64(x: float): Int64;
  1422. begin
  1423. Result:=Trunc(x)+ord(Frac(x)>0);
  1424. end;
  1425. function floor(x : float) : integer;
  1426. begin
  1427. Result:=Trunc(x)-ord(Frac(x)<0);
  1428. end;
  1429. function floor64(x: float): Int64;
  1430. begin
  1431. Result:=Trunc(x)-ord(Frac(x)<0);
  1432. end;
  1433. // Correction for "rounding to nearest, ties to even".
  1434. // RoundToNearestTieToEven(QWE.RTYUIOP) = QWE + TieToEven(ER, TYUIOP <> 0).
  1435. function TieToEven(AB: cardinal; somethingAfter: boolean): cardinal;
  1436. begin
  1437. result := AB and 1;
  1438. if (result <> 0) and not somethingAfter then
  1439. result := AB shr 1;
  1440. end;
  1441. {$ifdef FPC_HAS_TYPE_SINGLE}
  1442. procedure Frexp(X: single; out Mantissa: single; out Exponent: integer);
  1443. var
  1444. M: uint32;
  1445. E, ExtraE: int32;
  1446. begin
  1447. Mantissa := X;
  1448. E := TSingleRec(X).Exp;
  1449. if (E > 0) and (E < 2 * TSingleRec.Bias + 1) then
  1450. begin
  1451. // Normal.
  1452. TSingleRec(Mantissa).Exp := TSingleRec.Bias - 1;
  1453. Exponent := E - (TSingleRec.Bias - 1);
  1454. exit;
  1455. end;
  1456. if E = 0 then
  1457. begin
  1458. M := TSingleRec(X).Frac;
  1459. if M <> 0 then
  1460. begin
  1461. // Subnormal.
  1462. ExtraE := 23 - BsrDWord(M);
  1463. TSingleRec(Mantissa).Frac := M shl ExtraE; // "and (1 shl 23 - 1)" required to remove starting 1, but .SetFrac already does it.
  1464. TSingleRec(Mantissa).Exp := TSingleRec.Bias - 1;
  1465. Exponent := -TSingleRec.Bias + 2 - ExtraE;
  1466. exit;
  1467. end;
  1468. end;
  1469. // ±0, ±Inf, NaN.
  1470. Exponent := 0;
  1471. end;
  1472. function Ldexp(X: single; p: integer): single;
  1473. var
  1474. M, E: uint32;
  1475. xp, sh: integer;
  1476. begin
  1477. E := TSingleRec(X).Exp;
  1478. if (E = 0) and (TSingleRec(X).Frac = 0) or (E = 2 * TSingleRec.Bias + 1) then
  1479. // ±0, ±Inf, NaN.
  1480. exit(X);
  1481. Frexp(X, result, xp);
  1482. inc(xp, p);
  1483. if (xp >= -TSingleRec.Bias + 2) and (xp <= TSingleRec.Bias + 1) then
  1484. // Normalized.
  1485. TSingleRec(result).Exp := xp + (TSingleRec.Bias - 1)
  1486. else if xp > TSingleRec.Bias + 1 then
  1487. begin
  1488. // Overflow.
  1489. TSingleRec(result).Exp := 2 * TSingleRec.Bias + 1;
  1490. TSingleRec(result).Frac := 0;
  1491. end else
  1492. begin
  1493. TSingleRec(result).Exp := 0;
  1494. if xp >= -TSingleRec.Bias + 2 - 23 then
  1495. begin
  1496. // Denormalized.
  1497. M := TSingleRec(result).Frac or uint32(1) shl 23;
  1498. sh := -TSingleRec.Bias + 1 - xp;
  1499. TSingleRec(result).Frac := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint32(1) shl sh - 1) <> 0);
  1500. end else
  1501. // Underflow.
  1502. TSingleRec(result).Frac := 0;
  1503. end;
  1504. end;
  1505. {$endif}
  1506. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1507. procedure Frexp(X: double; out Mantissa: double; out Exponent: integer);
  1508. var
  1509. M: uint64;
  1510. E, ExtraE: int32;
  1511. begin
  1512. Mantissa := X;
  1513. E := TDoubleRec(X).Exp;
  1514. if (E > 0) and (E < 2 * TDoubleRec.Bias + 1) then
  1515. begin
  1516. // Normal.
  1517. TDoubleRec(Mantissa).Exp := TDoubleRec.Bias - 1;
  1518. Exponent := E - (TDoubleRec.Bias - 1);
  1519. exit;
  1520. end;
  1521. if E = 0 then
  1522. begin
  1523. M := TDoubleRec(X).Frac;
  1524. if M <> 0 then
  1525. begin
  1526. // Subnormal.
  1527. ExtraE := 52 - BsrQWord(M);
  1528. TDoubleRec(Mantissa).Frac := M shl ExtraE; // "and (1 shl 52 - 1)" required to remove starting 1, but .SetFrac already does it.
  1529. TDoubleRec(Mantissa).Exp := TDoubleRec.Bias - 1;
  1530. Exponent := -TDoubleRec.Bias + 2 - ExtraE;
  1531. exit;
  1532. end;
  1533. end;
  1534. // ±0, ±Inf, NaN.
  1535. Exponent := 0;
  1536. end;
  1537. function Ldexp(X: double; p: integer): double;
  1538. var
  1539. M: uint64;
  1540. E: uint32;
  1541. xp, sh: integer;
  1542. begin
  1543. E := TDoubleRec(X).Exp;
  1544. if (E = 0) and (TDoubleRec(X).Frac = 0) or (E = 2 * TDoubleRec.Bias + 1) then
  1545. // ±0, ±Inf, NaN.
  1546. exit(X);
  1547. Frexp(X, result, xp);
  1548. inc(xp, p);
  1549. if (xp >= -TDoubleRec.Bias + 2) and (xp <= TDoubleRec.Bias + 1) then
  1550. // Normalized.
  1551. TDoubleRec(result).Exp := xp + (TDoubleRec.Bias - 1)
  1552. else if xp > TDoubleRec.Bias + 1 then
  1553. begin
  1554. // Overflow.
  1555. TDoubleRec(result).Exp := 2 * TDoubleRec.Bias + 1;
  1556. TDoubleRec(result).Frac := 0;
  1557. end else
  1558. begin
  1559. TDoubleRec(result).Exp := 0;
  1560. if xp >= -TDoubleRec.Bias + 2 - 52 then
  1561. begin
  1562. // Denormalized.
  1563. M := TDoubleRec(result).Frac or uint64(1) shl 52;
  1564. sh := -TSingleRec.Bias + 1 - xp;
  1565. TDoubleRec(result).Frac := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint64(1) shl sh - 1) <> 0);
  1566. end else
  1567. // Underflow.
  1568. TDoubleRec(result).Frac := 0;
  1569. end;
  1570. end;
  1571. {$endif}
  1572. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1573. procedure Frexp(X: extended; out Mantissa: extended; out Exponent: integer);
  1574. var
  1575. M: uint64;
  1576. E, ExtraE: int32;
  1577. begin
  1578. Mantissa := X;
  1579. E := TExtended80Rec(X).Exp;
  1580. if (E > 0) and (E < 2 * TExtended80Rec.Bias + 1) then
  1581. begin
  1582. // Normal.
  1583. TExtended80Rec(Mantissa).Exp := TExtended80Rec.Bias - 1;
  1584. Exponent := E - (TExtended80Rec.Bias - 1);
  1585. exit;
  1586. end;
  1587. if E = 0 then
  1588. begin
  1589. M := TExtended80Rec(X).Frac;
  1590. if M <> 0 then
  1591. begin
  1592. // Subnormal. Extended has explicit starting 1.
  1593. ExtraE := 63 - BsrQWord(M);
  1594. TExtended80Rec(Mantissa).Frac := M shl ExtraE;
  1595. TExtended80Rec(Mantissa).Exp := TExtended80Rec.Bias - 1;
  1596. Exponent := -TExtended80Rec.Bias + 2 - ExtraE;
  1597. exit;
  1598. end;
  1599. end;
  1600. // ±0, ±Inf, NaN.
  1601. Exponent := 0;
  1602. end;
  1603. function Ldexp(X: extended; p: integer): extended;
  1604. var
  1605. M: uint64;
  1606. E: uint32;
  1607. xp, sh: integer;
  1608. begin
  1609. E := TExtended80Rec(X).Exp;
  1610. if (E = 0) and (TExtended80Rec(X).Frac = 0) or (E = 2 * TExtended80Rec.Bias + 1) then
  1611. // ±0, ±Inf, NaN.
  1612. exit(X);
  1613. Frexp(X, result, xp);
  1614. inc(xp, p);
  1615. if (xp >= -TExtended80Rec.Bias + 2) and (xp <= TExtended80Rec.Bias + 1) then
  1616. // Normalized.
  1617. TExtended80Rec(result).Exp := xp + (TExtended80Rec.Bias - 1)
  1618. else if xp > TExtended80Rec.Bias + 1 then
  1619. begin
  1620. // Overflow.
  1621. TExtended80Rec(result).Exp := 2 * TExtended80Rec.Bias + 1;
  1622. TExtended80Rec(result).Frac := uint64(1) shl 63;
  1623. end
  1624. else if xp >= -TExtended80Rec.Bias + 2 - 63 then
  1625. begin
  1626. // Denormalized... usually.
  1627. // Mantissa of subnormal 'extended' (Exp = 0) must always start with 0.
  1628. // If the calculated mantissa starts with 1, extended instead becomes normalized with Exp = 1.
  1629. M := TExtended80Rec(result).Frac;
  1630. sh := -TExtended80Rec.Bias + 1 - xp;
  1631. M := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint64(1) shl sh - 1) <> 0);
  1632. TExtended80Rec(result).Exp := M shr 63;
  1633. TExtended80Rec(result).Frac := M;
  1634. end else
  1635. begin
  1636. // Underflow.
  1637. TExtended80Rec(result).Exp := 0;
  1638. TExtended80Rec(result).Frac := 0;
  1639. end;
  1640. end;
  1641. {$endif}
  1642. const
  1643. { Cutoff for https://en.wikipedia.org/wiki/Pairwise_summation; sums of at least this many elements are split in two halves. }
  1644. RecursiveSumThreshold=12;
  1645. {$ifdef FPC_HAS_TYPE_SINGLE}
  1646. function mean(const data : array of Single) : float;
  1647. begin
  1648. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  1649. end;
  1650. function mean(const data : PSingle; Const N : longint) : float;
  1651. begin
  1652. mean:=sum(Data,N);
  1653. mean:=mean/N;
  1654. end;
  1655. function sum(const data : array of Single) : float;inline;
  1656. begin
  1657. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  1658. end;
  1659. function sum(const data : PSingle;Const N : longint) : float;
  1660. var
  1661. i : SizeInt;
  1662. begin
  1663. if N>=RecursiveSumThreshold then
  1664. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1665. else
  1666. begin
  1667. result:=0;
  1668. for i:=0 to N-1 do
  1669. result:=result+data[i];
  1670. end;
  1671. end;
  1672. {$endif FPC_HAS_TYPE_SINGLE}
  1673. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1674. function mean(const data : array of Double) : float; inline;
  1675. begin
  1676. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  1677. end;
  1678. function mean(const data : PDouble; Const N : longint) : float;
  1679. begin
  1680. mean:=sum(Data,N);
  1681. mean:=mean/N;
  1682. end;
  1683. function sum(const data : array of Double) : float; inline;
  1684. begin
  1685. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  1686. end;
  1687. function sum(const data : PDouble;Const N : longint) : float;
  1688. var
  1689. i : SizeInt;
  1690. begin
  1691. if N>=RecursiveSumThreshold then
  1692. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1693. else
  1694. begin
  1695. result:=0;
  1696. for i:=0 to N-1 do
  1697. result:=result+data[i];
  1698. end;
  1699. end;
  1700. {$endif FPC_HAS_TYPE_DOUBLE}
  1701. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1702. function mean(const data : array of Extended) : float;
  1703. begin
  1704. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  1705. end;
  1706. function mean(const data : PExtended; Const N : longint) : float;
  1707. begin
  1708. mean:=sum(Data,N);
  1709. mean:=mean/N;
  1710. end;
  1711. function sum(const data : array of Extended) : float; inline;
  1712. begin
  1713. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  1714. end;
  1715. function sum(const data : PExtended;Const N : longint) : float;
  1716. var
  1717. i : SizeInt;
  1718. begin
  1719. if N>=RecursiveSumThreshold then
  1720. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1721. else
  1722. begin
  1723. result:=0;
  1724. for i:=0 to N-1 do
  1725. result:=result+data[i];
  1726. end;
  1727. end;
  1728. {$endif FPC_HAS_TYPE_EXTENDED}
  1729. function sumInt(const data : PInt64;Const N : longint) : Int64;
  1730. var
  1731. i : SizeInt;
  1732. begin
  1733. sumInt:=0;
  1734. for i:=0 to N-1 do
  1735. sumInt:=sumInt+data[i];
  1736. end;
  1737. function sumInt(const data : array of Int64) : Int64; inline;
  1738. begin
  1739. Result:=SumInt(PInt64(@Data[0]),High(Data)+1);
  1740. end;
  1741. function mean(const data : PInt64; const N : Longint):Float;
  1742. begin
  1743. mean:=sumInt(Data,N);
  1744. mean:=mean/N;
  1745. end;
  1746. function mean(const data: array of Int64):Float;
  1747. begin
  1748. mean:=mean(PInt64(@data[0]),High(Data)+1);
  1749. end;
  1750. function sumInt(const data : PInteger; Const N : longint) : Int64;
  1751. var
  1752. i : SizeInt;
  1753. begin
  1754. sumInt:=0;
  1755. for i:=0 to N-1 do
  1756. sumInt:=sumInt+data[i];
  1757. end;
  1758. function sumInt(const data : array of Integer) : Int64;inline;
  1759. begin
  1760. Result:=sumInt(PInteger(@Data[0]),High(Data)+1);
  1761. end;
  1762. function mean(const data : PInteger; const N : Longint):Float;
  1763. begin
  1764. mean:=sumInt(Data,N);
  1765. mean:=mean/N;
  1766. end;
  1767. function mean(const data: array of Integer):Float;
  1768. begin
  1769. mean:=mean(PInteger(@data[0]),High(Data)+1);
  1770. end;
  1771. {$ifdef FPC_HAS_TYPE_SINGLE}
  1772. function sumofsquares(const data : array of Single) : float; inline;
  1773. begin
  1774. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  1775. end;
  1776. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  1777. var
  1778. i : SizeInt;
  1779. begin
  1780. if N>=RecursiveSumThreshold then
  1781. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1782. else
  1783. begin
  1784. result:=0;
  1785. for i:=0 to N-1 do
  1786. result:=result+sqr(data[i]);
  1787. end;
  1788. end;
  1789. procedure sumsandsquares(const data : array of Single;
  1790. var sum,sumofsquares : float); inline;
  1791. begin
  1792. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1793. end;
  1794. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  1795. var sum,sumofsquares : float);
  1796. var
  1797. i : SizeInt;
  1798. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  1799. begin
  1800. if N>=RecursiveSumThreshold then
  1801. begin
  1802. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  1803. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  1804. sum:=sum0+sum1;
  1805. sumofsquares:=sumofsquares0+sumofsquares1;
  1806. end
  1807. else
  1808. begin
  1809. tsum:=0;
  1810. tsumofsquares:=0;
  1811. for i:=0 to N-1 do
  1812. begin
  1813. temp:=data[i];
  1814. tsum:=tsum+temp;
  1815. tsumofsquares:=tsumofsquares+sqr(temp);
  1816. end;
  1817. sum:=tsum;
  1818. sumofsquares:=tsumofsquares;
  1819. end;
  1820. end;
  1821. {$endif FPC_HAS_TYPE_SINGLE}
  1822. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1823. function sumofsquares(const data : array of Double) : float; inline;
  1824. begin
  1825. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  1826. end;
  1827. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  1828. var
  1829. i : SizeInt;
  1830. begin
  1831. if N>=RecursiveSumThreshold then
  1832. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1833. else
  1834. begin
  1835. result:=0;
  1836. for i:=0 to N-1 do
  1837. result:=result+sqr(data[i]);
  1838. end;
  1839. end;
  1840. procedure sumsandsquares(const data : array of Double;
  1841. var sum,sumofsquares : float);
  1842. begin
  1843. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1844. end;
  1845. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  1846. var sum,sumofsquares : float);
  1847. var
  1848. i : SizeInt;
  1849. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  1850. begin
  1851. if N>=RecursiveSumThreshold then
  1852. begin
  1853. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  1854. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  1855. sum:=sum0+sum1;
  1856. sumofsquares:=sumofsquares0+sumofsquares1;
  1857. end
  1858. else
  1859. begin
  1860. tsum:=0;
  1861. tsumofsquares:=0;
  1862. for i:=0 to N-1 do
  1863. begin
  1864. temp:=data[i];
  1865. tsum:=tsum+temp;
  1866. tsumofsquares:=tsumofsquares+sqr(temp);
  1867. end;
  1868. sum:=tsum;
  1869. sumofsquares:=tsumofsquares;
  1870. end;
  1871. end;
  1872. {$endif FPC_HAS_TYPE_DOUBLE}
  1873. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1874. function sumofsquares(const data : array of Extended) : float; inline;
  1875. begin
  1876. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  1877. end;
  1878. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  1879. var
  1880. i : SizeInt;
  1881. begin
  1882. if N>=RecursiveSumThreshold then
  1883. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1884. else
  1885. begin
  1886. result:=0;
  1887. for i:=0 to N-1 do
  1888. result:=result+sqr(data[i]);
  1889. end;
  1890. end;
  1891. procedure sumsandsquares(const data : array of Extended;
  1892. var sum,sumofsquares : float); inline;
  1893. begin
  1894. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1895. end;
  1896. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  1897. var sum,sumofsquares : float);
  1898. var
  1899. i : SizeInt;
  1900. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  1901. begin
  1902. if N>=RecursiveSumThreshold then
  1903. begin
  1904. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  1905. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  1906. sum:=sum0+sum1;
  1907. sumofsquares:=sumofsquares0+sumofsquares1;
  1908. end
  1909. else
  1910. begin
  1911. tsum:=0;
  1912. tsumofsquares:=0;
  1913. for i:=0 to N-1 do
  1914. begin
  1915. temp:=data[i];
  1916. tsum:=tsum+temp;
  1917. tsumofsquares:=tsumofsquares+sqr(temp);
  1918. end;
  1919. sum:=tsum;
  1920. sumofsquares:=tsumofsquares;
  1921. end;
  1922. end;
  1923. {$endif FPC_HAS_TYPE_EXTENDED}
  1924. function randg(mean,stddev : float) : float;
  1925. Var U1,S2 : Float;
  1926. begin
  1927. repeat
  1928. u1:= 2*random-1;
  1929. S2:=Sqr(U1)+sqr(2*random-1);
  1930. until s2<1;
  1931. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  1932. end;
  1933. function RandomRange(const aFrom, aTo: Integer): Integer;
  1934. begin
  1935. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1936. end;
  1937. function RandomRange(const aFrom, aTo: Int64): Int64;
  1938. begin
  1939. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  1940. end;
  1941. {$ifdef FPC_HAS_TYPE_SINGLE}
  1942. procedure MeanAndTotalVariance
  1943. (const data: PSingle; N: LongInt; var mu, variance: float);
  1944. function CalcVariance(data: PSingle; N: SizeInt; mu: float): float;
  1945. var
  1946. i: SizeInt;
  1947. begin
  1948. if N>=RecursiveSumThreshold then
  1949. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  1950. else
  1951. begin
  1952. result:=0;
  1953. for i:=0 to N-1 do
  1954. result:=result+Sqr(data[i]-mu);
  1955. end;
  1956. end;
  1957. begin
  1958. mu := Mean( data, N );
  1959. variance := CalcVariance( data, N, mu );
  1960. end;
  1961. function stddev(const data : array of Single) : float; inline;
  1962. begin
  1963. Result:=Stddev(PSingle(@Data[0]),High(Data)+1);
  1964. end;
  1965. function stddev(const data : PSingle; Const N : Integer) : float;
  1966. begin
  1967. StdDev:=Sqrt(Variance(Data,N));
  1968. end;
  1969. procedure meanandstddev(const data : array of Single;
  1970. var mean,stddev : float); inline;
  1971. begin
  1972. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  1973. end;
  1974. procedure meanandstddev
  1975. ( const data: PSingle;
  1976. const N: Longint;
  1977. var mean,
  1978. stdDev: Float
  1979. );
  1980. var totalVariance: float;
  1981. begin
  1982. MeanAndTotalVariance( data, N, mean, totalVariance );
  1983. if N < 2 then stdDev := 0
  1984. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  1985. end;
  1986. function variance(const data : array of Single) : float; inline;
  1987. begin
  1988. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  1989. end;
  1990. function variance(const data : PSingle; Const N : Integer) : float;
  1991. begin
  1992. If N=1 then
  1993. Result:=0
  1994. else
  1995. Result:=TotalVariance(Data,N)/(N-1);
  1996. end;
  1997. function totalvariance(const data : array of Single) : float; inline;
  1998. begin
  1999. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  2000. end;
  2001. function totalvariance(const data : PSingle; const N : Integer) : float;
  2002. var mu: float;
  2003. begin
  2004. MeanAndTotalVariance( data, N, mu, result );
  2005. end;
  2006. function popnstddev(const data : array of Single) : float;
  2007. begin
  2008. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  2009. end;
  2010. function popnstddev(const data : PSingle; Const N : Integer) : float;
  2011. begin
  2012. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  2013. end;
  2014. function popnvariance(const data : array of Single) : float; inline;
  2015. begin
  2016. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  2017. end;
  2018. function popnvariance(const data : PSingle; Const N : Integer) : float;
  2019. begin
  2020. PopnVariance:=TotalVariance(Data,N)/N;
  2021. end;
  2022. procedure momentskewkurtosis(const data : array of single;
  2023. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  2024. begin
  2025. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  2026. end;
  2027. type
  2028. TMoments2to4 = array[2 .. 4] of float;
  2029. procedure momentskewkurtosis(
  2030. const data: pSingle;
  2031. Const N: integer;
  2032. out m1: float;
  2033. out m2: float;
  2034. out m3: float;
  2035. out m4: float;
  2036. out skew: float;
  2037. out kurtosis: float
  2038. );
  2039. procedure CalcDevSums2to4(data: PSingle; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  2040. var
  2041. tm2, tm3, tm4, dev, dev2: float;
  2042. i: SizeInt;
  2043. m2to4Part0, m2to4Part1: TMoments2to4;
  2044. begin
  2045. if N >= RecursiveSumThreshold then
  2046. begin
  2047. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  2048. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  2049. for i := Low(TMoments2to4) to High(TMoments2to4) do
  2050. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  2051. end
  2052. else
  2053. begin
  2054. tm2 := 0;
  2055. tm3 := 0;
  2056. tm4 := 0;
  2057. for i := 0 to N - 1 do
  2058. begin
  2059. dev := data[i] - m1;
  2060. dev2 := sqr(dev);
  2061. tm2 := tm2 + dev2;
  2062. tm3 := tm3 + dev2 * dev;
  2063. tm4 := tm4 + sqr(dev2);
  2064. end;
  2065. m2to4[2] := tm2;
  2066. m2to4[3] := tm3;
  2067. m2to4[4] := tm4;
  2068. end;
  2069. end;
  2070. var
  2071. reciprocalN: float;
  2072. m2to4: TMoments2to4;
  2073. begin
  2074. m1 := 0;
  2075. reciprocalN := 1/N;
  2076. m1 := reciprocalN * sum(data, N);
  2077. CalcDevSums2to4(data, N, m1, m2to4);
  2078. m2 := reciprocalN * m2to4[2];
  2079. m3 := reciprocalN * m2to4[3];
  2080. m4 := reciprocalN * m2to4[4];
  2081. skew := m3 / (sqrt(m2)*m2);
  2082. kurtosis := m4 / (m2 * m2);
  2083. end;
  2084. function norm(const data : array of Single) : float; inline;
  2085. begin
  2086. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  2087. end;
  2088. function norm(const data : PSingle; Const N : Integer) : float;
  2089. begin
  2090. norm:=sqrt(sumofsquares(data,N));
  2091. end;
  2092. {$endif FPC_HAS_TYPE_SINGLE}
  2093. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2094. procedure MeanAndTotalVariance
  2095. (const data: PDouble; N: LongInt; var mu, variance: float);
  2096. function CalcVariance(data: PDouble; N: SizeInt; mu: float): float;
  2097. var
  2098. i: SizeInt;
  2099. begin
  2100. if N>=RecursiveSumThreshold then
  2101. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  2102. else
  2103. begin
  2104. result:=0;
  2105. for i:=0 to N-1 do
  2106. result:=result+Sqr(data[i]-mu);
  2107. end;
  2108. end;
  2109. begin
  2110. mu := Mean( data, N );
  2111. variance := CalcVariance( data, N, mu );
  2112. end;
  2113. function stddev(const data : array of Double) : float; inline;
  2114. begin
  2115. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  2116. end;
  2117. function stddev(const data : PDouble; Const N : Integer) : float;
  2118. begin
  2119. StdDev:=Sqrt(Variance(Data,N));
  2120. end;
  2121. procedure meanandstddev(const data : array of Double;
  2122. var mean,stddev : float);
  2123. begin
  2124. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  2125. end;
  2126. procedure meanandstddev
  2127. ( const data: PDouble;
  2128. const N: Longint;
  2129. var mean,
  2130. stdDev: Float
  2131. );
  2132. var totalVariance: float;
  2133. begin
  2134. MeanAndTotalVariance( data, N, mean, totalVariance );
  2135. if N < 2 then stdDev := 0
  2136. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  2137. end;
  2138. function variance(const data : array of Double) : float; inline;
  2139. begin
  2140. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  2141. end;
  2142. function variance(const data : PDouble; Const N : Integer) : float;
  2143. begin
  2144. If N=1 then
  2145. Result:=0
  2146. else
  2147. Result:=TotalVariance(Data,N)/(N-1);
  2148. end;
  2149. function totalvariance(const data : array of Double) : float; inline;
  2150. begin
  2151. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  2152. end;
  2153. function totalvariance(const data : PDouble; const N : Integer) : float;
  2154. var mu: float;
  2155. begin
  2156. MeanAndTotalVariance( data, N, mu, result );
  2157. end;
  2158. function popnstddev(const data : array of Double) : float;
  2159. begin
  2160. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  2161. end;
  2162. function popnstddev(const data : PDouble; Const N : Integer) : float;
  2163. begin
  2164. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  2165. end;
  2166. function popnvariance(const data : array of Double) : float; inline;
  2167. begin
  2168. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  2169. end;
  2170. function popnvariance(const data : PDouble; Const N : Integer) : float;
  2171. begin
  2172. PopnVariance:=TotalVariance(Data,N)/N;
  2173. end;
  2174. procedure momentskewkurtosis(const data : array of Double;
  2175. out m1,m2,m3,m4,skew,kurtosis : float);
  2176. begin
  2177. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  2178. end;
  2179. procedure momentskewkurtosis(
  2180. const data: pdouble;
  2181. Const N: integer;
  2182. out m1: float;
  2183. out m2: float;
  2184. out m3: float;
  2185. out m4: float;
  2186. out skew: float;
  2187. out kurtosis: float
  2188. );
  2189. procedure CalcDevSums2to4(data: PDouble; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  2190. var
  2191. tm2, tm3, tm4, dev, dev2: float;
  2192. i: SizeInt;
  2193. m2to4Part0, m2to4Part1: TMoments2to4;
  2194. begin
  2195. if N >= RecursiveSumThreshold then
  2196. begin
  2197. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  2198. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  2199. for i := Low(TMoments2to4) to High(TMoments2to4) do
  2200. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  2201. end
  2202. else
  2203. begin
  2204. tm2 := 0;
  2205. tm3 := 0;
  2206. tm4 := 0;
  2207. for i := 0 to N - 1 do
  2208. begin
  2209. dev := data[i] - m1;
  2210. dev2 := sqr(dev);
  2211. tm2 := tm2 + dev2;
  2212. tm3 := tm3 + dev2 * dev;
  2213. tm4 := tm4 + sqr(dev2);
  2214. end;
  2215. m2to4[2] := tm2;
  2216. m2to4[3] := tm3;
  2217. m2to4[4] := tm4;
  2218. end;
  2219. end;
  2220. var
  2221. reciprocalN: float;
  2222. m2to4: TMoments2to4;
  2223. begin
  2224. m1 := 0;
  2225. reciprocalN := 1/N;
  2226. m1 := reciprocalN * sum(data, N);
  2227. CalcDevSums2to4(data, N, m1, m2to4);
  2228. m2 := reciprocalN * m2to4[2];
  2229. m3 := reciprocalN * m2to4[3];
  2230. m4 := reciprocalN * m2to4[4];
  2231. skew := m3 / (sqrt(m2)*m2);
  2232. kurtosis := m4 / (m2 * m2);
  2233. end;
  2234. function norm(const data : array of Double) : float; inline;
  2235. begin
  2236. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  2237. end;
  2238. function norm(const data : PDouble; Const N : Integer) : float;
  2239. begin
  2240. norm:=sqrt(sumofsquares(data,N));
  2241. end;
  2242. {$endif FPC_HAS_TYPE_DOUBLE}
  2243. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2244. procedure MeanAndTotalVariance
  2245. (const data: PExtended; N: LongInt; var mu, variance: float);
  2246. function CalcVariance(data: PExtended; N: SizeInt; mu: float): float;
  2247. var
  2248. i: SizeInt;
  2249. begin
  2250. if N>=RecursiveSumThreshold then
  2251. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  2252. else
  2253. begin
  2254. result:=0;
  2255. for i:=0 to N-1 do
  2256. result:=result+Sqr(data[i]-mu);
  2257. end;
  2258. end;
  2259. begin
  2260. mu := Mean( data, N );
  2261. variance := CalcVariance( data, N, mu );
  2262. end;
  2263. function stddev(const data : array of Extended) : float; inline;
  2264. begin
  2265. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  2266. end;
  2267. function stddev(const data : PExtended; Const N : Integer) : float;
  2268. begin
  2269. StdDev:=Sqrt(Variance(Data,N));
  2270. end;
  2271. procedure meanandstddev(const data : array of Extended;
  2272. var mean,stddev : float); inline;
  2273. begin
  2274. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  2275. end;
  2276. procedure meanandstddev
  2277. ( const data: PExtended;
  2278. const N: Longint;
  2279. var mean,
  2280. stdDev: Float
  2281. );
  2282. var totalVariance: float;
  2283. begin
  2284. MeanAndTotalVariance( data, N, mean, totalVariance );
  2285. if N < 2 then stdDev := 0
  2286. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  2287. end;
  2288. function variance(const data : array of Extended) : float; inline;
  2289. begin
  2290. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  2291. end;
  2292. function variance(const data : PExtended; Const N : Integer) : float;
  2293. begin
  2294. If N=1 then
  2295. Result:=0
  2296. else
  2297. Result:=TotalVariance(Data,N)/(N-1);
  2298. end;
  2299. function totalvariance(const data : array of Extended) : float; inline;
  2300. begin
  2301. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  2302. end;
  2303. function totalvariance(const data : PExtended;Const N : Integer) : float;
  2304. var mu: float;
  2305. begin
  2306. MeanAndTotalVariance( data, N, mu, result );
  2307. end;
  2308. function popnstddev(const data : array of Extended) : float;
  2309. begin
  2310. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  2311. end;
  2312. function popnstddev(const data : PExtended; Const N : Integer) : float;
  2313. begin
  2314. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  2315. end;
  2316. function popnvariance(const data : array of Extended) : float; inline;
  2317. begin
  2318. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  2319. end;
  2320. function popnvariance(const data : PExtended; Const N : Integer) : float;
  2321. begin
  2322. PopnVariance:=TotalVariance(Data,N)/N;
  2323. end;
  2324. procedure momentskewkurtosis(const data : array of Extended;
  2325. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  2326. begin
  2327. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  2328. end;
  2329. procedure momentskewkurtosis(
  2330. const data: pExtended;
  2331. Const N: Integer;
  2332. out m1: float;
  2333. out m2: float;
  2334. out m3: float;
  2335. out m4: float;
  2336. out skew: float;
  2337. out kurtosis: float
  2338. );
  2339. procedure CalcDevSums2to4(data: PExtended; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  2340. var
  2341. tm2, tm3, tm4, dev, dev2: float;
  2342. i: SizeInt;
  2343. m2to4Part0, m2to4Part1: TMoments2to4;
  2344. begin
  2345. if N >= RecursiveSumThreshold then
  2346. begin
  2347. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  2348. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  2349. for i := Low(TMoments2to4) to High(TMoments2to4) do
  2350. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  2351. end
  2352. else
  2353. begin
  2354. tm2 := 0;
  2355. tm3 := 0;
  2356. tm4 := 0;
  2357. for i := 0 to N - 1 do
  2358. begin
  2359. dev := data[i] - m1;
  2360. dev2 := sqr(dev);
  2361. tm2 := tm2 + dev2;
  2362. tm3 := tm3 + dev2 * dev;
  2363. tm4 := tm4 + sqr(dev2);
  2364. end;
  2365. m2to4[2] := tm2;
  2366. m2to4[3] := tm3;
  2367. m2to4[4] := tm4;
  2368. end;
  2369. end;
  2370. var
  2371. reciprocalN: float;
  2372. m2to4: TMoments2to4;
  2373. begin
  2374. m1 := 0;
  2375. reciprocalN := 1/N;
  2376. m1 := reciprocalN * sum(data, N);
  2377. CalcDevSums2to4(data, N, m1, m2to4);
  2378. m2 := reciprocalN * m2to4[2];
  2379. m3 := reciprocalN * m2to4[3];
  2380. m4 := reciprocalN * m2to4[4];
  2381. skew := m3 / (sqrt(m2)*m2);
  2382. kurtosis := m4 / (m2 * m2);
  2383. end;
  2384. function norm(const data : array of Extended) : float; inline;
  2385. begin
  2386. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  2387. end;
  2388. function norm(const data : PExtended; Const N : Integer) : float;
  2389. begin
  2390. norm:=sqrt(sumofsquares(data,N));
  2391. end;
  2392. {$endif FPC_HAS_TYPE_EXTENDED}
  2393. function MinIntValue(const Data: array of Integer): Integer;
  2394. var
  2395. I: SizeInt;
  2396. begin
  2397. Result := Data[Low(Data)];
  2398. For I := Succ(Low(Data)) To High(Data) Do
  2399. If Data[I] < Result Then Result := Data[I];
  2400. end;
  2401. function MaxIntValue(const Data: array of Integer): Integer;
  2402. var
  2403. I: SizeInt;
  2404. begin
  2405. Result := Data[Low(Data)];
  2406. For I := Succ(Low(Data)) To High(Data) Do
  2407. If Data[I] > Result Then Result := Data[I];
  2408. end;
  2409. function MinValue(const Data: array of Integer): Integer; inline;
  2410. begin
  2411. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  2412. end;
  2413. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  2414. var
  2415. I: SizeInt;
  2416. begin
  2417. Result := Data[0];
  2418. For I := 1 To N-1 do
  2419. If Data[I] < Result Then Result := Data[I];
  2420. end;
  2421. function MaxValue(const Data: array of Integer): Integer; inline;
  2422. begin
  2423. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  2424. end;
  2425. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  2426. var
  2427. i : SizeInt;
  2428. begin
  2429. { get an initial value }
  2430. maxvalue:=data[0];
  2431. for i:=1 to N-1 do
  2432. if data[i]>maxvalue then
  2433. maxvalue:=data[i];
  2434. end;
  2435. {$ifdef FPC_HAS_TYPE_SINGLE}
  2436. function minvalue(const data : array of Single) : Single; inline;
  2437. begin
  2438. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  2439. end;
  2440. function minvalue(const data : PSingle; Const N : Integer) : Single;
  2441. var
  2442. i : SizeInt;
  2443. begin
  2444. { get an initial value }
  2445. minvalue:=data[0];
  2446. for i:=1 to N-1 do
  2447. if data[i]<minvalue then
  2448. minvalue:=data[i];
  2449. end;
  2450. function maxvalue(const data : array of Single) : Single; inline;
  2451. begin
  2452. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  2453. end;
  2454. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  2455. var
  2456. i : SizeInt;
  2457. begin
  2458. { get an initial value }
  2459. maxvalue:=data[0];
  2460. for i:=1 to N-1 do
  2461. if data[i]>maxvalue then
  2462. maxvalue:=data[i];
  2463. end;
  2464. {$endif FPC_HAS_TYPE_SINGLE}
  2465. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2466. function minvalue(const data : array of Double) : Double; inline;
  2467. begin
  2468. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  2469. end;
  2470. function minvalue(const data : PDouble; Const N : Integer) : Double;
  2471. var
  2472. i : SizeInt;
  2473. begin
  2474. { get an initial value }
  2475. minvalue:=data[0];
  2476. for i:=1 to N-1 do
  2477. if data[i]<minvalue then
  2478. minvalue:=data[i];
  2479. end;
  2480. function maxvalue(const data : array of Double) : Double; inline;
  2481. begin
  2482. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  2483. end;
  2484. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  2485. var
  2486. i : SizeInt;
  2487. begin
  2488. { get an initial value }
  2489. maxvalue:=data[0];
  2490. for i:=1 to N-1 do
  2491. if data[i]>maxvalue then
  2492. maxvalue:=data[i];
  2493. end;
  2494. {$endif FPC_HAS_TYPE_DOUBLE}
  2495. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2496. function minvalue(const data : array of Extended) : Extended; inline;
  2497. begin
  2498. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  2499. end;
  2500. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  2501. var
  2502. i : SizeInt;
  2503. begin
  2504. { get an initial value }
  2505. minvalue:=data[0];
  2506. for i:=1 to N-1 do
  2507. if data[i]<minvalue then
  2508. minvalue:=data[i];
  2509. end;
  2510. function maxvalue(const data : array of Extended) : Extended; inline;
  2511. begin
  2512. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  2513. end;
  2514. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  2515. var
  2516. i : SizeInt;
  2517. begin
  2518. { get an initial value }
  2519. maxvalue:=data[0];
  2520. for i:=1 to N-1 do
  2521. if data[i]>maxvalue then
  2522. maxvalue:=data[i];
  2523. end;
  2524. {$endif FPC_HAS_TYPE_EXTENDED}
  2525. function Min(a, b: Integer): Integer;inline;
  2526. begin
  2527. if a < b then
  2528. Result := a
  2529. else
  2530. Result := b;
  2531. end;
  2532. function Max(a, b: Integer): Integer;inline;
  2533. begin
  2534. if a > b then
  2535. Result := a
  2536. else
  2537. Result := b;
  2538. end;
  2539. {
  2540. function Min(a, b: Cardinal): Cardinal;inline;
  2541. begin
  2542. if a < b then
  2543. Result := a
  2544. else
  2545. Result := b;
  2546. end;
  2547. function Max(a, b: Cardinal): Cardinal;inline;
  2548. begin
  2549. if a > b then
  2550. Result := a
  2551. else
  2552. Result := b;
  2553. end;
  2554. }
  2555. function Min(a, b: Int64): Int64;inline;
  2556. begin
  2557. if a < b then
  2558. Result := a
  2559. else
  2560. Result := b;
  2561. end;
  2562. function Max(a, b: Int64): Int64;inline;
  2563. begin
  2564. if a > b then
  2565. Result := a
  2566. else
  2567. Result := b;
  2568. end;
  2569. function Min(a, b: QWord): QWord; inline;
  2570. begin
  2571. if a < b then
  2572. Result := a
  2573. else
  2574. Result := b;
  2575. end;
  2576. function Max(a, b: QWord): Qword;inline;
  2577. begin
  2578. if a > b then
  2579. Result := a
  2580. else
  2581. Result := b;
  2582. end;
  2583. {$ifdef FPC_HAS_TYPE_SINGLE}
  2584. function Min(a, b: Single): Single;inline;
  2585. begin
  2586. if a < b then
  2587. Result := a
  2588. else
  2589. Result := b;
  2590. end;
  2591. function Max(a, b: Single): Single;inline;
  2592. begin
  2593. if a > b then
  2594. Result := a
  2595. else
  2596. Result := b;
  2597. end;
  2598. {$endif FPC_HAS_TYPE_SINGLE}
  2599. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2600. function Min(a, b: Double): Double;inline;
  2601. begin
  2602. if a < b then
  2603. Result := a
  2604. else
  2605. Result := b;
  2606. end;
  2607. function Max(a, b: Double): Double;inline;
  2608. begin
  2609. if a > b then
  2610. Result := a
  2611. else
  2612. Result := b;
  2613. end;
  2614. {$endif FPC_HAS_TYPE_DOUBLE}
  2615. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2616. function Min(a, b: Extended): Extended;inline;
  2617. begin
  2618. if a < b then
  2619. Result := a
  2620. else
  2621. Result := b;
  2622. end;
  2623. function Max(a, b: Extended): Extended;inline;
  2624. begin
  2625. if a > b then
  2626. Result := a
  2627. else
  2628. Result := b;
  2629. end;
  2630. {$endif FPC_HAS_TYPE_EXTENDED}
  2631. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  2632. begin
  2633. Result:=(AValue>=AMin) and (AValue<=AMax);
  2634. end;
  2635. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  2636. begin
  2637. Result:=(AValue>=AMin) and (AValue<=AMax);
  2638. end;
  2639. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2640. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  2641. begin
  2642. Result:=(AValue>=AMin) and (AValue<=AMax);
  2643. end;
  2644. {$endif FPC_HAS_TYPE_DOUBLE}
  2645. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  2646. begin
  2647. Result:=AValue;
  2648. If Result<AMin then
  2649. Result:=AMin;
  2650. if Result>AMax then
  2651. Result:=AMax;
  2652. end;
  2653. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  2654. begin
  2655. Result:=AValue;
  2656. If Result<AMin then
  2657. Result:=AMin;
  2658. if Result>AMax then
  2659. Result:=AMax;
  2660. end;
  2661. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2662. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  2663. begin
  2664. Result:=AValue;
  2665. If Result<AMin then
  2666. Result:=AMin;
  2667. if Result>AMax then
  2668. Result:=AMax;
  2669. end;
  2670. {$endif FPC_HAS_TYPE_DOUBLE}
  2671. Const
  2672. EZeroResolution = Extended(1E-16);
  2673. DZeroResolution = Double(1E-12);
  2674. SZeroResolution = Single(1E-4);
  2675. function IsZero(const A: Single; Epsilon: Single): Boolean;
  2676. begin
  2677. if (Epsilon=0) then
  2678. Epsilon:=SZeroResolution;
  2679. Result:=Abs(A)<=Epsilon;
  2680. end;
  2681. function IsZero(const A: Single): Boolean;inline;
  2682. begin
  2683. Result:=IsZero(A,single(SZeroResolution));
  2684. end;
  2685. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2686. function IsZero(const A: Double; Epsilon: Double): Boolean;
  2687. begin
  2688. if (Epsilon=0) then
  2689. Epsilon:=DZeroResolution;
  2690. Result:=Abs(A)<=Epsilon;
  2691. end;
  2692. function IsZero(const A: Double): Boolean;inline;
  2693. begin
  2694. Result:=IsZero(A,DZeroResolution);
  2695. end;
  2696. {$endif FPC_HAS_TYPE_DOUBLE}
  2697. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2698. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  2699. begin
  2700. if (Epsilon=0) then
  2701. Epsilon:=EZeroResolution;
  2702. Result:=Abs(A)<=Epsilon;
  2703. end;
  2704. function IsZero(const A: Extended): Boolean;inline;
  2705. begin
  2706. Result:=IsZero(A,EZeroResolution);
  2707. end;
  2708. {$endif FPC_HAS_TYPE_EXTENDED}
  2709. type
  2710. TSplitDouble = packed record
  2711. cards: Array[0..1] of cardinal;
  2712. end;
  2713. TSplitExtended = packed record
  2714. cards: Array[0..1] of cardinal;
  2715. w: word;
  2716. end;
  2717. function IsNan(const d : Single): Boolean; overload;
  2718. begin
  2719. result:=(longword(d) and $7fffffff)>$7f800000;
  2720. end;
  2721. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2722. function IsNan(const d : Double): Boolean;
  2723. var
  2724. fraczero, expMaximal: boolean;
  2725. begin
  2726. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2727. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  2728. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  2729. (TSplitDouble(d).cards[1] = 0);
  2730. {$else FPC_BIG_ENDIAN}
  2731. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  2732. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  2733. (TSplitDouble(d).cards[0] = 0);
  2734. {$endif FPC_BIG_ENDIAN}
  2735. Result:=expMaximal and not(fraczero);
  2736. end;
  2737. {$endif FPC_HAS_TYPE_DOUBLE}
  2738. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2739. function IsNan(const d : Extended): Boolean; overload;
  2740. var
  2741. fraczero, expMaximal: boolean;
  2742. begin
  2743. {$ifdef FPC_BIG_ENDIAN}
  2744. {$error no support for big endian extended type yet}
  2745. {$else FPC_BIG_ENDIAN}
  2746. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  2747. fraczero := (TSplitExtended(d).cards[0] = 0) and
  2748. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  2749. {$endif FPC_BIG_ENDIAN}
  2750. Result:=expMaximal and not(fraczero);
  2751. end;
  2752. {$endif FPC_HAS_TYPE_EXTENDED}
  2753. function IsInfinite(const d : Single): Boolean; overload;
  2754. begin
  2755. result:=(longword(d) and $7fffffff)=$7f800000;
  2756. end;
  2757. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2758. function IsInfinite(const d : Double): Boolean; overload;
  2759. var
  2760. fraczero, expMaximal: boolean;
  2761. begin
  2762. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2763. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  2764. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  2765. (TSplitDouble(d).cards[1] = 0);
  2766. {$else FPC_BIG_ENDIAN}
  2767. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  2768. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  2769. (TSplitDouble(d).cards[0] = 0);
  2770. {$endif FPC_BIG_ENDIAN}
  2771. Result:=expMaximal and fraczero;
  2772. end;
  2773. {$endif FPC_HAS_TYPE_DOUBLE}
  2774. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2775. function IsInfinite(const d : Extended): Boolean; overload;
  2776. var
  2777. fraczero, expMaximal: boolean;
  2778. begin
  2779. {$ifdef FPC_BIG_ENDIAN}
  2780. {$error no support for big endian extended type yet}
  2781. {$else FPC_BIG_ENDIAN}
  2782. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  2783. fraczero := (TSplitExtended(d).cards[0] = 0) and
  2784. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  2785. {$endif FPC_BIG_ENDIAN}
  2786. Result:=expMaximal and fraczero;
  2787. end;
  2788. {$endif FPC_HAS_TYPE_EXTENDED}
  2789. function copysign(x,y: float): float;
  2790. begin
  2791. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  2792. {$error copysign not yet implemented for float128}
  2793. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  2794. TSplitExtended(x).w:=(TSplitExtended(x).w and $7fff) or (TSplitExtended(y).w and $8000);
  2795. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  2796. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2797. TSplitDouble(x).cards[0]:=(TSplitDouble(x).cards[0] and $7fffffff) or (TSplitDouble(y).cards[0] and longword($80000000));
  2798. {$else}
  2799. TSplitDouble(x).cards[1]:=(TSplitDouble(x).cards[1] and $7fffffff) or (TSplitDouble(y).cards[1] and longword($80000000));
  2800. {$endif}
  2801. {$else}
  2802. longword(x):=longword(x and $7fffffff) or (longword(y) and longword($80000000));
  2803. {$endif}
  2804. result:=x;
  2805. end;
  2806. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2807. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  2808. begin
  2809. if (Epsilon=0) then
  2810. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  2811. if (A>B) then
  2812. Result:=((A-B)<=Epsilon)
  2813. else
  2814. Result:=((B-A)<=Epsilon);
  2815. end;
  2816. function SameValue(const A, B: Extended): Boolean;inline;
  2817. begin
  2818. Result:=SameValue(A,B,0.0);
  2819. end;
  2820. {$endif FPC_HAS_TYPE_EXTENDED}
  2821. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2822. function SameValue(const A, B: Double): Boolean;inline;
  2823. begin
  2824. Result:=SameValue(A,B,0.0);
  2825. end;
  2826. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  2827. begin
  2828. if (Epsilon=0) then
  2829. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  2830. if (A>B) then
  2831. Result:=((A-B)<=Epsilon)
  2832. else
  2833. Result:=((B-A)<=Epsilon);
  2834. end;
  2835. {$endif FPC_HAS_TYPE_DOUBLE}
  2836. function SameValue(const A, B: Single): Boolean;inline;
  2837. begin
  2838. Result:=SameValue(A,B,0);
  2839. end;
  2840. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  2841. begin
  2842. if (Epsilon=0) then
  2843. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  2844. if (A>B) then
  2845. Result:=((A-B)<=Epsilon)
  2846. else
  2847. Result:=((B-A)<=Epsilon);
  2848. end;
  2849. // Some CPUs probably allow a faster way of doing this in a single operation...
  2850. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  2851. {$ifndef FPC_MATH_HAS_DIVMOD}
  2852. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  2853. begin
  2854. if Dividend < 0 then
  2855. begin
  2856. { Use DivMod with >=0 dividend }
  2857. Dividend:=-Dividend;
  2858. { The documented behavior of Pascal's div/mod operators and DivMod
  2859. on negative dividends is to return Result closer to zero and
  2860. a negative Remainder. Which means that we can just negate both
  2861. Result and Remainder, and all it's Ok. }
  2862. Result:=-(Dividend Div Divisor);
  2863. Remainder:=-(Dividend+(Result*Divisor));
  2864. end
  2865. else
  2866. begin
  2867. Result:=Dividend Div Divisor;
  2868. Remainder:=Dividend-(Result*Divisor);
  2869. end;
  2870. end;
  2871. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  2872. begin
  2873. if Dividend < 0 then
  2874. begin
  2875. { Use DivMod with >=0 dividend }
  2876. Dividend:=-Dividend;
  2877. { The documented behavior of Pascal's div/mod operators and DivMod
  2878. on negative dividends is to return Result closer to zero and
  2879. a negative Remainder. Which means that we can just negate both
  2880. Result and Remainder, and all it's Ok. }
  2881. Result:=-(Dividend Div Divisor);
  2882. Remainder:=-(Dividend+(Result*Divisor));
  2883. end
  2884. else
  2885. begin
  2886. Result:=Dividend Div Divisor;
  2887. Remainder:=Dividend-(Result*Divisor);
  2888. end;
  2889. end;
  2890. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  2891. begin
  2892. Result:=Dividend Div Divisor;
  2893. Remainder:=Dividend-(Result*Divisor);
  2894. end;
  2895. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  2896. begin
  2897. if Dividend < 0 then
  2898. begin
  2899. { Use DivMod with >=0 dividend }
  2900. Dividend:=-Dividend;
  2901. { The documented behavior of Pascal's div/mod operators and DivMod
  2902. on negative dividends is to return Result closer to zero and
  2903. a negative Remainder. Which means that we can just negate both
  2904. Result and Remainder, and all it's Ok. }
  2905. Result:=-(Dividend Div Divisor);
  2906. Remainder:=-(Dividend+(Result*Divisor));
  2907. end
  2908. else
  2909. begin
  2910. Result:=Dividend Div Divisor;
  2911. Remainder:=Dividend-(Result*Divisor);
  2912. end;
  2913. end;
  2914. {$endif FPC_MATH_HAS_DIVMOD}
  2915. { Floating point modulo}
  2916. {$ifdef FPC_HAS_TYPE_SINGLE}
  2917. function FMod(const a, b: Single): Single;inline;overload;
  2918. begin
  2919. result:= a-b * Int(a/b);
  2920. end;
  2921. {$endif FPC_HAS_TYPE_SINGLE}
  2922. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2923. function FMod(const a, b: Double): Double;inline;overload;
  2924. begin
  2925. result:= a-b * Int(a/b);
  2926. end;
  2927. {$endif FPC_HAS_TYPE_DOUBLE}
  2928. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2929. function FMod(const a, b: Extended): Extended;inline;overload;
  2930. begin
  2931. result:= a-b * Int(a/b);
  2932. end;
  2933. {$endif FPC_HAS_TYPE_EXTENDED}
  2934. operator mod(const a,b:float) c:float;inline;
  2935. begin
  2936. c:= a-b * Int(a/b);
  2937. if SameValue(abs(c),abs(b)) then
  2938. c:=0.0;
  2939. end;
  2940. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  2941. begin
  2942. if val then result:=iftrue else result:=iffalse;
  2943. end;
  2944. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  2945. begin
  2946. if val then result:=iftrue else result:=iffalse;
  2947. end;
  2948. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  2949. begin
  2950. if val then result:=iftrue else result:=iffalse;
  2951. end;
  2952. // dilemma here. asm can do the two comparisons in one go?
  2953. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  2954. function CompareValue(const A, B : Integer): 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. function CompareValue(const A, B: Int64): TValueRelationship;
  2964. begin
  2965. result:=GreaterThanValue;
  2966. if a=b then
  2967. result:=EqualsValue
  2968. else
  2969. if a<b then
  2970. result:=LessThanValue;
  2971. end;
  2972. function CompareValue(const A, B: QWord): TValueRelationship;
  2973. begin
  2974. result:=GreaterThanValue;
  2975. if a=b then
  2976. result:=EqualsValue
  2977. else
  2978. if a<b then
  2979. result:=LessThanValue;
  2980. end;
  2981. {$ifdef FPC_HAS_TYPE_SINGLE}
  2982. function CompareValue(const A, B: Single; delta: Single = 0.0): TValueRelationship;
  2983. begin
  2984. result:=GreaterThanValue;
  2985. if abs(a-b)<=delta then
  2986. result:=EqualsValue
  2987. else
  2988. if a<b then
  2989. result:=LessThanValue;
  2990. end;
  2991. {$endif}
  2992. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2993. function CompareValue(const A, B: Double; delta: Double = 0.0): TValueRelationship;
  2994. begin
  2995. result:=GreaterThanValue;
  2996. if abs(a-b)<=delta then
  2997. result:=EqualsValue
  2998. else
  2999. if a<b then
  3000. result:=LessThanValue;
  3001. end;
  3002. {$endif}
  3003. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3004. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  3005. begin
  3006. result:=GreaterThanValue;
  3007. if abs(a-b)<=delta then
  3008. result:=EqualsValue
  3009. else
  3010. if a<b then
  3011. result:=LessThanValue;
  3012. end;
  3013. {$endif}
  3014. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3015. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  3016. var
  3017. RV : Double;
  3018. begin
  3019. RV:=IntPower(10,Digits);
  3020. Result:=Round(AValue/RV)*RV;
  3021. end;
  3022. {$endif}
  3023. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3024. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  3025. var
  3026. RV : Extended;
  3027. begin
  3028. RV:=IntPower(10,Digits);
  3029. Result:=Round(AValue/RV)*RV;
  3030. end;
  3031. {$endif}
  3032. {$ifdef FPC_HAS_TYPE_SINGLE}
  3033. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  3034. var
  3035. RV : Single;
  3036. begin
  3037. RV:=IntPower(10,Digits);
  3038. Result:=Round(AValue/RV)*RV;
  3039. end;
  3040. {$endif}
  3041. {$ifdef FPC_HAS_TYPE_SINGLE}
  3042. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  3043. var
  3044. RV : Single;
  3045. begin
  3046. RV := IntPower(10, -Digits);
  3047. if AValue < 0 then
  3048. Result := Int((AValue*RV) - 0.5)/RV
  3049. else
  3050. Result := Int((AValue*RV) + 0.5)/RV;
  3051. end;
  3052. {$endif}
  3053. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3054. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  3055. var
  3056. RV : Double;
  3057. begin
  3058. RV := IntPower(10, -Digits);
  3059. if AValue < 0 then
  3060. Result := Int((AValue*RV) - 0.5)/RV
  3061. else
  3062. Result := Int((AValue*RV) + 0.5)/RV;
  3063. end;
  3064. {$endif}
  3065. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3066. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  3067. var
  3068. RV : Extended;
  3069. begin
  3070. RV := IntPower(10, -Digits);
  3071. if AValue < 0 then
  3072. Result := Int((AValue*RV) - 0.5)/RV
  3073. else
  3074. Result := Int((AValue*RV) + 0.5)/RV;
  3075. end;
  3076. {$endif}
  3077. function RandomFrom(const AValues: array of Double): Double; overload;
  3078. begin
  3079. result:=AValues[random(High(AValues)+1)];
  3080. end;
  3081. function RandomFrom(const AValues: array of Integer): Integer; overload;
  3082. begin
  3083. result:=AValues[random(High(AValues)+1)];
  3084. end;
  3085. function RandomFrom(const AValues: array of Int64): Int64; overload;
  3086. begin
  3087. result:=AValues[random(High(AValues)+1)];
  3088. end;
  3089. {$if FPC_FULLVERSION >=30101}
  3090. generic function RandomFrom<T>(const AValues:array of T):T;
  3091. begin
  3092. result:=AValues[random(High(AValues)+1)];
  3093. end;
  3094. {$endif}
  3095. function FutureValue(ARate: Float; NPeriods: Integer;
  3096. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  3097. var
  3098. q, qn, factor: Float;
  3099. begin
  3100. if ARate = 0 then
  3101. Result := -APresentValue - APayment * NPeriods
  3102. else begin
  3103. q := 1.0 + ARate;
  3104. qn := power(q, NPeriods);
  3105. factor := (qn - 1) / (q - 1);
  3106. if APaymentTime = ptStartOfPeriod then
  3107. factor := factor * q;
  3108. Result := -(APresentValue * qn + APayment*factor);
  3109. end;
  3110. end;
  3111. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  3112. APaymentTime: TPaymentTime): Float;
  3113. { The interest rate cannot be calculated analytically. We solve the equation
  3114. numerically by means of the Newton method:
  3115. - guess value for the interest reate
  3116. - calculate at which interest rate the tangent of the curve fv(rate)
  3117. (straight line!) has the requested future vale.
  3118. - use this rate for the next iteration. }
  3119. const
  3120. DELTA = 0.001;
  3121. EPS = 1E-9; // required precision of interest rate (after typ. 6 iterations)
  3122. MAXIT = 20; // max iteration count to protect agains non-convergence
  3123. var
  3124. r1, r2, dr: Float;
  3125. fv1, fv2: Float;
  3126. iteration: Integer;
  3127. begin
  3128. iteration := 0;
  3129. r1 := 0.05; // inital guess
  3130. repeat
  3131. r2 := r1 + DELTA;
  3132. fv1 := FutureValue(r1, NPeriods, APayment, APresentValue, APaymentTime);
  3133. fv2 := FutureValue(r2, NPeriods, APayment, APresentValue, APaymentTime);
  3134. dr := (AFutureValue - fv1) / (fv2 - fv1) * delta; // tangent at fv(r)
  3135. r1 := r1 + dr; // next guess
  3136. inc(iteration);
  3137. until (abs(dr) < EPS) or (iteration >= MAXIT);
  3138. Result := r1;
  3139. end;
  3140. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  3141. APaymentTime: TPaymentTime): Float;
  3142. { Solve the cash flow equation (1) for q^n and take the logarithm }
  3143. var
  3144. q, x1, x2: Float;
  3145. begin
  3146. if ARate = 0 then
  3147. Result := -(APresentValue + AFutureValue) / APayment
  3148. else begin
  3149. q := 1.0 + ARate;
  3150. if APaymentTime = ptStartOfPeriod then
  3151. APayment := APayment * q;
  3152. x1 := APayment - AFutureValue * ARate;
  3153. x2 := APayment + APresentValue * ARate;
  3154. if (x2 = 0) // we have to divide by x2
  3155. or (sign(x1) * sign(x2) < 0) // the argument of the log is negative
  3156. then
  3157. Result := Infinity
  3158. else begin
  3159. Result := ln(x1/x2) / ln(q);
  3160. end;
  3161. end;
  3162. end;
  3163. function Payment(ARate: Float; NPeriods: Integer;
  3164. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  3165. var
  3166. q, qn, factor: Float;
  3167. begin
  3168. if ARate = 0 then
  3169. Result := -(AFutureValue + APresentValue) / NPeriods
  3170. else begin
  3171. q := 1.0 + ARate;
  3172. qn := power(q, NPeriods);
  3173. factor := (qn - 1) / (q - 1);
  3174. if APaymentTime = ptStartOfPeriod then
  3175. factor := factor * q;
  3176. Result := -(AFutureValue + APresentValue * qn) / factor;
  3177. end;
  3178. end;
  3179. function PresentValue(ARate: Float; NPeriods: Integer;
  3180. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  3181. var
  3182. q, qn, factor: Float;
  3183. begin
  3184. if ARate = 0.0 then
  3185. Result := -AFutureValue - APayment * NPeriods
  3186. else begin
  3187. q := 1.0 + ARate;
  3188. qn := power(q, NPeriods);
  3189. factor := (qn - 1) / (q - 1);
  3190. if APaymentTime = ptStartOfPeriod then
  3191. factor := factor * q;
  3192. Result := -(AFutureValue + APayment*factor) / qn;
  3193. end;
  3194. end;
  3195. {$else}
  3196. implementation
  3197. {$endif FPUNONE}
  3198. end.