math.pp 101 KB

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