index.js 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947
  1. module.exports = (function() {
  2. var __MODS__ = {};
  3. var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
  4. var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
  5. var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
  6. var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
  7. __DEFINE__(1720091880135, function(require, module, exports) {
  8. ;(function (globalScope) {
  9. /*!
  10. * decimal.js v10.4.3
  11. * An arbitrary-precision Decimal type for JavaScript.
  12. * https://github.com/MikeMcl/decimal.js
  13. * Copyright (c) 2022 Michael Mclaughlin <M8ch88l@gmail.com>
  14. * MIT Licence
  15. */
  16. // ----------------------------------- EDITABLE DEFAULTS ------------------------------------ //
  17. // The maximum exponent magnitude.
  18. // The limit on the value of `toExpNeg`, `toExpPos`, `minE` and `maxE`.
  19. var EXP_LIMIT = 9e15, // 0 to 9e15
  20. // The limit on the value of `precision`, and on the value of the first argument to
  21. // `toDecimalPlaces`, `toExponential`, `toFixed`, `toPrecision` and `toSignificantDigits`.
  22. MAX_DIGITS = 1e9, // 0 to 1e9
  23. // Base conversion alphabet.
  24. NUMERALS = '0123456789abcdef',
  25. // The natural logarithm of 10 (1025 digits).
  26. LN10 = '2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058',
  27. // Pi (1025 digits).
  28. PI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789',
  29. // The initial configuration properties of the Decimal constructor.
  30. DEFAULTS = {
  31. // These values must be integers within the stated ranges (inclusive).
  32. // Most of these values can be changed at run-time using the `Decimal.config` method.
  33. // The maximum number of significant digits of the result of a calculation or base conversion.
  34. // E.g. `Decimal.config({ precision: 20 });`
  35. precision: 20, // 1 to MAX_DIGITS
  36. // The rounding mode used when rounding to `precision`.
  37. //
  38. // ROUND_UP 0 Away from zero.
  39. // ROUND_DOWN 1 Towards zero.
  40. // ROUND_CEIL 2 Towards +Infinity.
  41. // ROUND_FLOOR 3 Towards -Infinity.
  42. // ROUND_HALF_UP 4 Towards nearest neighbour. If equidistant, up.
  43. // ROUND_HALF_DOWN 5 Towards nearest neighbour. If equidistant, down.
  44. // ROUND_HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour.
  45. // ROUND_HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity.
  46. // ROUND_HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.
  47. //
  48. // E.g.
  49. // `Decimal.rounding = 4;`
  50. // `Decimal.rounding = Decimal.ROUND_HALF_UP;`
  51. rounding: 4, // 0 to 8
  52. // The modulo mode used when calculating the modulus: a mod n.
  53. // The quotient (q = a / n) is calculated according to the corresponding rounding mode.
  54. // The remainder (r) is calculated as: r = a - n * q.
  55. //
  56. // UP 0 The remainder is positive if the dividend is negative, else is negative.
  57. // DOWN 1 The remainder has the same sign as the dividend (JavaScript %).
  58. // FLOOR 3 The remainder has the same sign as the divisor (Python %).
  59. // HALF_EVEN 6 The IEEE 754 remainder function.
  60. // EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). Always positive.
  61. //
  62. // Truncated division (1), floored division (3), the IEEE 754 remainder (6), and Euclidian
  63. // division (9) are commonly used for the modulus operation. The other rounding modes can also
  64. // be used, but they may not give useful results.
  65. modulo: 1, // 0 to 9
  66. // The exponent value at and beneath which `toString` returns exponential notation.
  67. // JavaScript numbers: -7
  68. toExpNeg: -7, // 0 to -EXP_LIMIT
  69. // The exponent value at and above which `toString` returns exponential notation.
  70. // JavaScript numbers: 21
  71. toExpPos: 21, // 0 to EXP_LIMIT
  72. // The minimum exponent value, beneath which underflow to zero occurs.
  73. // JavaScript numbers: -324 (5e-324)
  74. minE: -EXP_LIMIT, // -1 to -EXP_LIMIT
  75. // The maximum exponent value, above which overflow to Infinity occurs.
  76. // JavaScript numbers: 308 (1.7976931348623157e+308)
  77. maxE: EXP_LIMIT, // 1 to EXP_LIMIT
  78. // Whether to use cryptographically-secure random number generation, if available.
  79. crypto: false // true/false
  80. },
  81. // ----------------------------------- END OF EDITABLE DEFAULTS ------------------------------- //
  82. Decimal, inexact, noConflict, quadrant,
  83. external = true,
  84. decimalError = '[DecimalError] ',
  85. invalidArgument = decimalError + 'Invalid argument: ',
  86. precisionLimitExceeded = decimalError + 'Precision limit exceeded',
  87. cryptoUnavailable = decimalError + 'crypto unavailable',
  88. tag = '[object Decimal]',
  89. mathfloor = Math.floor,
  90. mathpow = Math.pow,
  91. isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,
  92. isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,
  93. isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,
  94. isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,
  95. BASE = 1e7,
  96. LOG_BASE = 7,
  97. MAX_SAFE_INTEGER = 9007199254740991,
  98. LN10_PRECISION = LN10.length - 1,
  99. PI_PRECISION = PI.length - 1,
  100. // Decimal.prototype object
  101. P = { toStringTag: tag };
  102. // Decimal prototype methods
  103. /*
  104. * absoluteValue abs
  105. * ceil
  106. * clampedTo clamp
  107. * comparedTo cmp
  108. * cosine cos
  109. * cubeRoot cbrt
  110. * decimalPlaces dp
  111. * dividedBy div
  112. * dividedToIntegerBy divToInt
  113. * equals eq
  114. * floor
  115. * greaterThan gt
  116. * greaterThanOrEqualTo gte
  117. * hyperbolicCosine cosh
  118. * hyperbolicSine sinh
  119. * hyperbolicTangent tanh
  120. * inverseCosine acos
  121. * inverseHyperbolicCosine acosh
  122. * inverseHyperbolicSine asinh
  123. * inverseHyperbolicTangent atanh
  124. * inverseSine asin
  125. * inverseTangent atan
  126. * isFinite
  127. * isInteger isInt
  128. * isNaN
  129. * isNegative isNeg
  130. * isPositive isPos
  131. * isZero
  132. * lessThan lt
  133. * lessThanOrEqualTo lte
  134. * logarithm log
  135. * [maximum] [max]
  136. * [minimum] [min]
  137. * minus sub
  138. * modulo mod
  139. * naturalExponential exp
  140. * naturalLogarithm ln
  141. * negated neg
  142. * plus add
  143. * precision sd
  144. * round
  145. * sine sin
  146. * squareRoot sqrt
  147. * tangent tan
  148. * times mul
  149. * toBinary
  150. * toDecimalPlaces toDP
  151. * toExponential
  152. * toFixed
  153. * toFraction
  154. * toHexadecimal toHex
  155. * toNearest
  156. * toNumber
  157. * toOctal
  158. * toPower pow
  159. * toPrecision
  160. * toSignificantDigits toSD
  161. * toString
  162. * truncated trunc
  163. * valueOf toJSON
  164. */
  165. /*
  166. * Return a new Decimal whose value is the absolute value of this Decimal.
  167. *
  168. */
  169. P.absoluteValue = P.abs = function () {
  170. var x = new this.constructor(this);
  171. if (x.s < 0) x.s = 1;
  172. return finalise(x);
  173. };
  174. /*
  175. * Return a new Decimal whose value is the value of this Decimal rounded to a whole number in the
  176. * direction of positive Infinity.
  177. *
  178. */
  179. P.ceil = function () {
  180. return finalise(new this.constructor(this), this.e + 1, 2);
  181. };
  182. /*
  183. * Return a new Decimal whose value is the value of this Decimal clamped to the range
  184. * delineated by `min` and `max`.
  185. *
  186. * min {number|string|Decimal}
  187. * max {number|string|Decimal}
  188. *
  189. */
  190. P.clampedTo = P.clamp = function (min, max) {
  191. var k,
  192. x = this,
  193. Ctor = x.constructor;
  194. min = new Ctor(min);
  195. max = new Ctor(max);
  196. if (!min.s || !max.s) return new Ctor(NaN);
  197. if (min.gt(max)) throw Error(invalidArgument + max);
  198. k = x.cmp(min);
  199. return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x);
  200. };
  201. /*
  202. * Return
  203. * 1 if the value of this Decimal is greater than the value of `y`,
  204. * -1 if the value of this Decimal is less than the value of `y`,
  205. * 0 if they have the same value,
  206. * NaN if the value of either Decimal is NaN.
  207. *
  208. */
  209. P.comparedTo = P.cmp = function (y) {
  210. var i, j, xdL, ydL,
  211. x = this,
  212. xd = x.d,
  213. yd = (y = new x.constructor(y)).d,
  214. xs = x.s,
  215. ys = y.s;
  216. // Either NaN or ±Infinity?
  217. if (!xd || !yd) {
  218. return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;
  219. }
  220. // Either zero?
  221. if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0;
  222. // Signs differ?
  223. if (xs !== ys) return xs;
  224. // Compare exponents.
  225. if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1;
  226. xdL = xd.length;
  227. ydL = yd.length;
  228. // Compare digit by digit.
  229. for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) {
  230. if (xd[i] !== yd[i]) return xd[i] > yd[i] ^ xs < 0 ? 1 : -1;
  231. }
  232. // Compare lengths.
  233. return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;
  234. };
  235. /*
  236. * Return a new Decimal whose value is the cosine of the value in radians of this Decimal.
  237. *
  238. * Domain: [-Infinity, Infinity]
  239. * Range: [-1, 1]
  240. *
  241. * cos(0) = 1
  242. * cos(-0) = 1
  243. * cos(Infinity) = NaN
  244. * cos(-Infinity) = NaN
  245. * cos(NaN) = NaN
  246. *
  247. */
  248. P.cosine = P.cos = function () {
  249. var pr, rm,
  250. x = this,
  251. Ctor = x.constructor;
  252. if (!x.d) return new Ctor(NaN);
  253. // cos(0) = cos(-0) = 1
  254. if (!x.d[0]) return new Ctor(1);
  255. pr = Ctor.precision;
  256. rm = Ctor.rounding;
  257. Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
  258. Ctor.rounding = 1;
  259. x = cosine(Ctor, toLessThanHalfPi(Ctor, x));
  260. Ctor.precision = pr;
  261. Ctor.rounding = rm;
  262. return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);
  263. };
  264. /*
  265. *
  266. * Return a new Decimal whose value is the cube root of the value of this Decimal, rounded to
  267. * `precision` significant digits using rounding mode `rounding`.
  268. *
  269. * cbrt(0) = 0
  270. * cbrt(-0) = -0
  271. * cbrt(1) = 1
  272. * cbrt(-1) = -1
  273. * cbrt(N) = N
  274. * cbrt(-I) = -I
  275. * cbrt(I) = I
  276. *
  277. * Math.cbrt(x) = (x < 0 ? -Math.pow(-x, 1/3) : Math.pow(x, 1/3))
  278. *
  279. */
  280. P.cubeRoot = P.cbrt = function () {
  281. var e, m, n, r, rep, s, sd, t, t3, t3plusx,
  282. x = this,
  283. Ctor = x.constructor;
  284. if (!x.isFinite() || x.isZero()) return new Ctor(x);
  285. external = false;
  286. // Initial estimate.
  287. s = x.s * mathpow(x.s * x, 1 / 3);
  288. // Math.cbrt underflow/overflow?
  289. // Pass x to Math.pow as integer, then adjust the exponent of the result.
  290. if (!s || Math.abs(s) == 1 / 0) {
  291. n = digitsToString(x.d);
  292. e = x.e;
  293. // Adjust n exponent so it is a multiple of 3 away from x exponent.
  294. if (s = (e - n.length + 1) % 3) n += (s == 1 || s == -2 ? '0' : '00');
  295. s = mathpow(n, 1 / 3);
  296. // Rarely, e may be one less than the result exponent value.
  297. e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
  298. if (s == 1 / 0) {
  299. n = '5e' + e;
  300. } else {
  301. n = s.toExponential();
  302. n = n.slice(0, n.indexOf('e') + 1) + e;
  303. }
  304. r = new Ctor(n);
  305. r.s = x.s;
  306. } else {
  307. r = new Ctor(s.toString());
  308. }
  309. sd = (e = Ctor.precision) + 3;
  310. // Halley's method.
  311. // TODO? Compare Newton's method.
  312. for (;;) {
  313. t = r;
  314. t3 = t.times(t).times(t);
  315. t3plusx = t3.plus(x);
  316. r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1);
  317. // TODO? Replace with for-loop and checkRoundingDigits.
  318. if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
  319. n = n.slice(sd - 3, sd + 1);
  320. // The 4th rounding digit may be in error by -1 so if the 4 rounding digits are 9999 or 4999
  321. // , i.e. approaching a rounding boundary, continue the iteration.
  322. if (n == '9999' || !rep && n == '4999') {
  323. // On the first iteration only, check to see if rounding up gives the exact result as the
  324. // nines may infinitely repeat.
  325. if (!rep) {
  326. finalise(t, e + 1, 0);
  327. if (t.times(t).times(t).eq(x)) {
  328. r = t;
  329. break;
  330. }
  331. }
  332. sd += 4;
  333. rep = 1;
  334. } else {
  335. // If the rounding digits are null, 0{0,4} or 50{0,3}, check for an exact result.
  336. // If not, then there are further digits and m will be truthy.
  337. if (!+n || !+n.slice(1) && n.charAt(0) == '5') {
  338. // Truncate to the first rounding digit.
  339. finalise(r, e + 1, 1);
  340. m = !r.times(r).times(r).eq(x);
  341. }
  342. break;
  343. }
  344. }
  345. }
  346. external = true;
  347. return finalise(r, e, Ctor.rounding, m);
  348. };
  349. /*
  350. * Return the number of decimal places of the value of this Decimal.
  351. *
  352. */
  353. P.decimalPlaces = P.dp = function () {
  354. var w,
  355. d = this.d,
  356. n = NaN;
  357. if (d) {
  358. w = d.length - 1;
  359. n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE;
  360. // Subtract the number of trailing zeros of the last word.
  361. w = d[w];
  362. if (w) for (; w % 10 == 0; w /= 10) n--;
  363. if (n < 0) n = 0;
  364. }
  365. return n;
  366. };
  367. /*
  368. * n / 0 = I
  369. * n / N = N
  370. * n / I = 0
  371. * 0 / n = 0
  372. * 0 / 0 = N
  373. * 0 / N = N
  374. * 0 / I = 0
  375. * N / n = N
  376. * N / 0 = N
  377. * N / N = N
  378. * N / I = N
  379. * I / n = I
  380. * I / 0 = I
  381. * I / N = N
  382. * I / I = N
  383. *
  384. * Return a new Decimal whose value is the value of this Decimal divided by `y`, rounded to
  385. * `precision` significant digits using rounding mode `rounding`.
  386. *
  387. */
  388. P.dividedBy = P.div = function (y) {
  389. return divide(this, new this.constructor(y));
  390. };
  391. /*
  392. * Return a new Decimal whose value is the integer part of dividing the value of this Decimal
  393. * by the value of `y`, rounded to `precision` significant digits using rounding mode `rounding`.
  394. *
  395. */
  396. P.dividedToIntegerBy = P.divToInt = function (y) {
  397. var x = this,
  398. Ctor = x.constructor;
  399. return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding);
  400. };
  401. /*
  402. * Return true if the value of this Decimal is equal to the value of `y`, otherwise return false.
  403. *
  404. */
  405. P.equals = P.eq = function (y) {
  406. return this.cmp(y) === 0;
  407. };
  408. /*
  409. * Return a new Decimal whose value is the value of this Decimal rounded to a whole number in the
  410. * direction of negative Infinity.
  411. *
  412. */
  413. P.floor = function () {
  414. return finalise(new this.constructor(this), this.e + 1, 3);
  415. };
  416. /*
  417. * Return true if the value of this Decimal is greater than the value of `y`, otherwise return
  418. * false.
  419. *
  420. */
  421. P.greaterThan = P.gt = function (y) {
  422. return this.cmp(y) > 0;
  423. };
  424. /*
  425. * Return true if the value of this Decimal is greater than or equal to the value of `y`,
  426. * otherwise return false.
  427. *
  428. */
  429. P.greaterThanOrEqualTo = P.gte = function (y) {
  430. var k = this.cmp(y);
  431. return k == 1 || k === 0;
  432. };
  433. /*
  434. * Return a new Decimal whose value is the hyperbolic cosine of the value in radians of this
  435. * Decimal.
  436. *
  437. * Domain: [-Infinity, Infinity]
  438. * Range: [1, Infinity]
  439. *
  440. * cosh(x) = 1 + x^2/2! + x^4/4! + x^6/6! + ...
  441. *
  442. * cosh(0) = 1
  443. * cosh(-0) = 1
  444. * cosh(Infinity) = Infinity
  445. * cosh(-Infinity) = Infinity
  446. * cosh(NaN) = NaN
  447. *
  448. * x time taken (ms) result
  449. * 1000 9 9.8503555700852349694e+433
  450. * 10000 25 4.4034091128314607936e+4342
  451. * 100000 171 1.4033316802130615897e+43429
  452. * 1000000 3817 1.5166076984010437725e+434294
  453. * 10000000 abandoned after 2 minute wait
  454. *
  455. * TODO? Compare performance of cosh(x) = 0.5 * (exp(x) + exp(-x))
  456. *
  457. */
  458. P.hyperbolicCosine = P.cosh = function () {
  459. var k, n, pr, rm, len,
  460. x = this,
  461. Ctor = x.constructor,
  462. one = new Ctor(1);
  463. if (!x.isFinite()) return new Ctor(x.s ? 1 / 0 : NaN);
  464. if (x.isZero()) return one;
  465. pr = Ctor.precision;
  466. rm = Ctor.rounding;
  467. Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
  468. Ctor.rounding = 1;
  469. len = x.d.length;
  470. // Argument reduction: cos(4x) = 1 - 8cos^2(x) + 8cos^4(x) + 1
  471. // i.e. cos(x) = 1 - cos^2(x/4)(8 - 8cos^2(x/4))
  472. // Estimate the optimum number of times to use the argument reduction.
  473. // TODO? Estimation reused from cosine() and may not be optimal here.
  474. if (len < 32) {
  475. k = Math.ceil(len / 3);
  476. n = (1 / tinyPow(4, k)).toString();
  477. } else {
  478. k = 16;
  479. n = '2.3283064365386962890625e-10';
  480. }
  481. x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true);
  482. // Reverse argument reduction
  483. var cosh2_x,
  484. i = k,
  485. d8 = new Ctor(8);
  486. for (; i--;) {
  487. cosh2_x = x.times(x);
  488. x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8))));
  489. }
  490. return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true);
  491. };
  492. /*
  493. * Return a new Decimal whose value is the hyperbolic sine of the value in radians of this
  494. * Decimal.
  495. *
  496. * Domain: [-Infinity, Infinity]
  497. * Range: [-Infinity, Infinity]
  498. *
  499. * sinh(x) = x + x^3/3! + x^5/5! + x^7/7! + ...
  500. *
  501. * sinh(0) = 0
  502. * sinh(-0) = -0
  503. * sinh(Infinity) = Infinity
  504. * sinh(-Infinity) = -Infinity
  505. * sinh(NaN) = NaN
  506. *
  507. * x time taken (ms)
  508. * 10 2 ms
  509. * 100 5 ms
  510. * 1000 14 ms
  511. * 10000 82 ms
  512. * 100000 886 ms 1.4033316802130615897e+43429
  513. * 200000 2613 ms
  514. * 300000 5407 ms
  515. * 400000 8824 ms
  516. * 500000 13026 ms 8.7080643612718084129e+217146
  517. * 1000000 48543 ms
  518. *
  519. * TODO? Compare performance of sinh(x) = 0.5 * (exp(x) - exp(-x))
  520. *
  521. */
  522. P.hyperbolicSine = P.sinh = function () {
  523. var k, pr, rm, len,
  524. x = this,
  525. Ctor = x.constructor;
  526. if (!x.isFinite() || x.isZero()) return new Ctor(x);
  527. pr = Ctor.precision;
  528. rm = Ctor.rounding;
  529. Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
  530. Ctor.rounding = 1;
  531. len = x.d.length;
  532. if (len < 3) {
  533. x = taylorSeries(Ctor, 2, x, x, true);
  534. } else {
  535. // Alternative argument reduction: sinh(3x) = sinh(x)(3 + 4sinh^2(x))
  536. // i.e. sinh(x) = sinh(x/3)(3 + 4sinh^2(x/3))
  537. // 3 multiplications and 1 addition
  538. // Argument reduction: sinh(5x) = sinh(x)(5 + sinh^2(x)(20 + 16sinh^2(x)))
  539. // i.e. sinh(x) = sinh(x/5)(5 + sinh^2(x/5)(20 + 16sinh^2(x/5)))
  540. // 4 multiplications and 2 additions
  541. // Estimate the optimum number of times to use the argument reduction.
  542. k = 1.4 * Math.sqrt(len);
  543. k = k > 16 ? 16 : k | 0;
  544. x = x.times(1 / tinyPow(5, k));
  545. x = taylorSeries(Ctor, 2, x, x, true);
  546. // Reverse argument reduction
  547. var sinh2_x,
  548. d5 = new Ctor(5),
  549. d16 = new Ctor(16),
  550. d20 = new Ctor(20);
  551. for (; k--;) {
  552. sinh2_x = x.times(x);
  553. x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20))));
  554. }
  555. }
  556. Ctor.precision = pr;
  557. Ctor.rounding = rm;
  558. return finalise(x, pr, rm, true);
  559. };
  560. /*
  561. * Return a new Decimal whose value is the hyperbolic tangent of the value in radians of this
  562. * Decimal.
  563. *
  564. * Domain: [-Infinity, Infinity]
  565. * Range: [-1, 1]
  566. *
  567. * tanh(x) = sinh(x) / cosh(x)
  568. *
  569. * tanh(0) = 0
  570. * tanh(-0) = -0
  571. * tanh(Infinity) = 1
  572. * tanh(-Infinity) = -1
  573. * tanh(NaN) = NaN
  574. *
  575. */
  576. P.hyperbolicTangent = P.tanh = function () {
  577. var pr, rm,
  578. x = this,
  579. Ctor = x.constructor;
  580. if (!x.isFinite()) return new Ctor(x.s);
  581. if (x.isZero()) return new Ctor(x);
  582. pr = Ctor.precision;
  583. rm = Ctor.rounding;
  584. Ctor.precision = pr + 7;
  585. Ctor.rounding = 1;
  586. return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
  587. };
  588. /*
  589. * Return a new Decimal whose value is the arccosine (inverse cosine) in radians of the value of
  590. * this Decimal.
  591. *
  592. * Domain: [-1, 1]
  593. * Range: [0, pi]
  594. *
  595. * acos(x) = pi/2 - asin(x)
  596. *
  597. * acos(0) = pi/2
  598. * acos(-0) = pi/2
  599. * acos(1) = 0
  600. * acos(-1) = pi
  601. * acos(1/2) = pi/3
  602. * acos(-1/2) = 2*pi/3
  603. * acos(|x| > 1) = NaN
  604. * acos(NaN) = NaN
  605. *
  606. */
  607. P.inverseCosine = P.acos = function () {
  608. var halfPi,
  609. x = this,
  610. Ctor = x.constructor,
  611. k = x.abs().cmp(1),
  612. pr = Ctor.precision,
  613. rm = Ctor.rounding;
  614. if (k !== -1) {
  615. return k === 0
  616. // |x| is 1
  617. ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0)
  618. // |x| > 1 or x is NaN
  619. : new Ctor(NaN);
  620. }
  621. if (x.isZero()) return getPi(Ctor, pr + 4, rm).times(0.5);
  622. // TODO? Special case acos(0.5) = pi/3 and acos(-0.5) = 2*pi/3
  623. Ctor.precision = pr + 6;
  624. Ctor.rounding = 1;
  625. x = x.asin();
  626. halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
  627. Ctor.precision = pr;
  628. Ctor.rounding = rm;
  629. return halfPi.minus(x);
  630. };
  631. /*
  632. * Return a new Decimal whose value is the inverse of the hyperbolic cosine in radians of the
  633. * value of this Decimal.
  634. *
  635. * Domain: [1, Infinity]
  636. * Range: [0, Infinity]
  637. *
  638. * acosh(x) = ln(x + sqrt(x^2 - 1))
  639. *
  640. * acosh(x < 1) = NaN
  641. * acosh(NaN) = NaN
  642. * acosh(Infinity) = Infinity
  643. * acosh(-Infinity) = NaN
  644. * acosh(0) = NaN
  645. * acosh(-0) = NaN
  646. * acosh(1) = 0
  647. * acosh(-1) = NaN
  648. *
  649. */
  650. P.inverseHyperbolicCosine = P.acosh = function () {
  651. var pr, rm,
  652. x = this,
  653. Ctor = x.constructor;
  654. if (x.lte(1)) return new Ctor(x.eq(1) ? 0 : NaN);
  655. if (!x.isFinite()) return new Ctor(x);
  656. pr = Ctor.precision;
  657. rm = Ctor.rounding;
  658. Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4;
  659. Ctor.rounding = 1;
  660. external = false;
  661. x = x.times(x).minus(1).sqrt().plus(x);
  662. external = true;
  663. Ctor.precision = pr;
  664. Ctor.rounding = rm;
  665. return x.ln();
  666. };
  667. /*
  668. * Return a new Decimal whose value is the inverse of the hyperbolic sine in radians of the value
  669. * of this Decimal.
  670. *
  671. * Domain: [-Infinity, Infinity]
  672. * Range: [-Infinity, Infinity]
  673. *
  674. * asinh(x) = ln(x + sqrt(x^2 + 1))
  675. *
  676. * asinh(NaN) = NaN
  677. * asinh(Infinity) = Infinity
  678. * asinh(-Infinity) = -Infinity
  679. * asinh(0) = 0
  680. * asinh(-0) = -0
  681. *
  682. */
  683. P.inverseHyperbolicSine = P.asinh = function () {
  684. var pr, rm,
  685. x = this,
  686. Ctor = x.constructor;
  687. if (!x.isFinite() || x.isZero()) return new Ctor(x);
  688. pr = Ctor.precision;
  689. rm = Ctor.rounding;
  690. Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6;
  691. Ctor.rounding = 1;
  692. external = false;
  693. x = x.times(x).plus(1).sqrt().plus(x);
  694. external = true;
  695. Ctor.precision = pr;
  696. Ctor.rounding = rm;
  697. return x.ln();
  698. };
  699. /*
  700. * Return a new Decimal whose value is the inverse of the hyperbolic tangent in radians of the
  701. * value of this Decimal.
  702. *
  703. * Domain: [-1, 1]
  704. * Range: [-Infinity, Infinity]
  705. *
  706. * atanh(x) = 0.5 * ln((1 + x) / (1 - x))
  707. *
  708. * atanh(|x| > 1) = NaN
  709. * atanh(NaN) = NaN
  710. * atanh(Infinity) = NaN
  711. * atanh(-Infinity) = NaN
  712. * atanh(0) = 0
  713. * atanh(-0) = -0
  714. * atanh(1) = Infinity
  715. * atanh(-1) = -Infinity
  716. *
  717. */
  718. P.inverseHyperbolicTangent = P.atanh = function () {
  719. var pr, rm, wpr, xsd,
  720. x = this,
  721. Ctor = x.constructor;
  722. if (!x.isFinite()) return new Ctor(NaN);
  723. if (x.e >= 0) return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN);
  724. pr = Ctor.precision;
  725. rm = Ctor.rounding;
  726. xsd = x.sd();
  727. if (Math.max(xsd, pr) < 2 * -x.e - 1) return finalise(new Ctor(x), pr, rm, true);
  728. Ctor.precision = wpr = xsd - x.e;
  729. x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1);
  730. Ctor.precision = pr + 4;
  731. Ctor.rounding = 1;
  732. x = x.ln();
  733. Ctor.precision = pr;
  734. Ctor.rounding = rm;
  735. return x.times(0.5);
  736. };
  737. /*
  738. * Return a new Decimal whose value is the arcsine (inverse sine) in radians of the value of this
  739. * Decimal.
  740. *
  741. * Domain: [-Infinity, Infinity]
  742. * Range: [-pi/2, pi/2]
  743. *
  744. * asin(x) = 2*atan(x/(1 + sqrt(1 - x^2)))
  745. *
  746. * asin(0) = 0
  747. * asin(-0) = -0
  748. * asin(1/2) = pi/6
  749. * asin(-1/2) = -pi/6
  750. * asin(1) = pi/2
  751. * asin(-1) = -pi/2
  752. * asin(|x| > 1) = NaN
  753. * asin(NaN) = NaN
  754. *
  755. * TODO? Compare performance of Taylor series.
  756. *
  757. */
  758. P.inverseSine = P.asin = function () {
  759. var halfPi, k,
  760. pr, rm,
  761. x = this,
  762. Ctor = x.constructor;
  763. if (x.isZero()) return new Ctor(x);
  764. k = x.abs().cmp(1);
  765. pr = Ctor.precision;
  766. rm = Ctor.rounding;
  767. if (k !== -1) {
  768. // |x| is 1
  769. if (k === 0) {
  770. halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
  771. halfPi.s = x.s;
  772. return halfPi;
  773. }
  774. // |x| > 1 or x is NaN
  775. return new Ctor(NaN);
  776. }
  777. // TODO? Special case asin(1/2) = pi/6 and asin(-1/2) = -pi/6
  778. Ctor.precision = pr + 6;
  779. Ctor.rounding = 1;
  780. x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan();
  781. Ctor.precision = pr;
  782. Ctor.rounding = rm;
  783. return x.times(2);
  784. };
  785. /*
  786. * Return a new Decimal whose value is the arctangent (inverse tangent) in radians of the value
  787. * of this Decimal.
  788. *
  789. * Domain: [-Infinity, Infinity]
  790. * Range: [-pi/2, pi/2]
  791. *
  792. * atan(x) = x - x^3/3 + x^5/5 - x^7/7 + ...
  793. *
  794. * atan(0) = 0
  795. * atan(-0) = -0
  796. * atan(1) = pi/4
  797. * atan(-1) = -pi/4
  798. * atan(Infinity) = pi/2
  799. * atan(-Infinity) = -pi/2
  800. * atan(NaN) = NaN
  801. *
  802. */
  803. P.inverseTangent = P.atan = function () {
  804. var i, j, k, n, px, t, r, wpr, x2,
  805. x = this,
  806. Ctor = x.constructor,
  807. pr = Ctor.precision,
  808. rm = Ctor.rounding;
  809. if (!x.isFinite()) {
  810. if (!x.s) return new Ctor(NaN);
  811. if (pr + 4 <= PI_PRECISION) {
  812. r = getPi(Ctor, pr + 4, rm).times(0.5);
  813. r.s = x.s;
  814. return r;
  815. }
  816. } else if (x.isZero()) {
  817. return new Ctor(x);
  818. } else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) {
  819. r = getPi(Ctor, pr + 4, rm).times(0.25);
  820. r.s = x.s;
  821. return r;
  822. }
  823. Ctor.precision = wpr = pr + 10;
  824. Ctor.rounding = 1;
  825. // TODO? if (x >= 1 && pr <= PI_PRECISION) atan(x) = halfPi * x.s - atan(1 / x);
  826. // Argument reduction
  827. // Ensure |x| < 0.42
  828. // atan(x) = 2 * atan(x / (1 + sqrt(1 + x^2)))
  829. k = Math.min(28, wpr / LOG_BASE + 2 | 0);
  830. for (i = k; i; --i) x = x.div(x.times(x).plus(1).sqrt().plus(1));
  831. external = false;
  832. j = Math.ceil(wpr / LOG_BASE);
  833. n = 1;
  834. x2 = x.times(x);
  835. r = new Ctor(x);
  836. px = x;
  837. // atan(x) = x - x^3/3 + x^5/5 - x^7/7 + ...
  838. for (; i !== -1;) {
  839. px = px.times(x2);
  840. t = r.minus(px.div(n += 2));
  841. px = px.times(x2);
  842. r = t.plus(px.div(n += 2));
  843. if (r.d[j] !== void 0) for (i = j; r.d[i] === t.d[i] && i--;);
  844. }
  845. if (k) r = r.times(2 << (k - 1));
  846. external = true;
  847. return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true);
  848. };
  849. /*
  850. * Return true if the value of this Decimal is a finite number, otherwise return false.
  851. *
  852. */
  853. P.isFinite = function () {
  854. return !!this.d;
  855. };
  856. /*
  857. * Return true if the value of this Decimal is an integer, otherwise return false.
  858. *
  859. */
  860. P.isInteger = P.isInt = function () {
  861. return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2;
  862. };
  863. /*
  864. * Return true if the value of this Decimal is NaN, otherwise return false.
  865. *
  866. */
  867. P.isNaN = function () {
  868. return !this.s;
  869. };
  870. /*
  871. * Return true if the value of this Decimal is negative, otherwise return false.
  872. *
  873. */
  874. P.isNegative = P.isNeg = function () {
  875. return this.s < 0;
  876. };
  877. /*
  878. * Return true if the value of this Decimal is positive, otherwise return false.
  879. *
  880. */
  881. P.isPositive = P.isPos = function () {
  882. return this.s > 0;
  883. };
  884. /*
  885. * Return true if the value of this Decimal is 0 or -0, otherwise return false.
  886. *
  887. */
  888. P.isZero = function () {
  889. return !!this.d && this.d[0] === 0;
  890. };
  891. /*
  892. * Return true if the value of this Decimal is less than `y`, otherwise return false.
  893. *
  894. */
  895. P.lessThan = P.lt = function (y) {
  896. return this.cmp(y) < 0;
  897. };
  898. /*
  899. * Return true if the value of this Decimal is less than or equal to `y`, otherwise return false.
  900. *
  901. */
  902. P.lessThanOrEqualTo = P.lte = function (y) {
  903. return this.cmp(y) < 1;
  904. };
  905. /*
  906. * Return the logarithm of the value of this Decimal to the specified base, rounded to `precision`
  907. * significant digits using rounding mode `rounding`.
  908. *
  909. * If no base is specified, return log[10](arg).
  910. *
  911. * log[base](arg) = ln(arg) / ln(base)
  912. *
  913. * The result will always be correctly rounded if the base of the log is 10, and 'almost always'
  914. * otherwise:
  915. *
  916. * Depending on the rounding mode, the result may be incorrectly rounded if the first fifteen
  917. * rounding digits are [49]99999999999999 or [50]00000000000000. In that case, the maximum error
  918. * between the result and the correctly rounded result will be one ulp (unit in the last place).
  919. *
  920. * log[-b](a) = NaN
  921. * log[0](a) = NaN
  922. * log[1](a) = NaN
  923. * log[NaN](a) = NaN
  924. * log[Infinity](a) = NaN
  925. * log[b](0) = -Infinity
  926. * log[b](-0) = -Infinity
  927. * log[b](-a) = NaN
  928. * log[b](1) = 0
  929. * log[b](Infinity) = Infinity
  930. * log[b](NaN) = NaN
  931. *
  932. * [base] {number|string|Decimal} The base of the logarithm.
  933. *
  934. */
  935. P.logarithm = P.log = function (base) {
  936. var isBase10, d, denominator, k, inf, num, sd, r,
  937. arg = this,
  938. Ctor = arg.constructor,
  939. pr = Ctor.precision,
  940. rm = Ctor.rounding,
  941. guard = 5;
  942. // Default base is 10.
  943. if (base == null) {
  944. base = new Ctor(10);
  945. isBase10 = true;
  946. } else {
  947. base = new Ctor(base);
  948. d = base.d;
  949. // Return NaN if base is negative, or non-finite, or is 0 or 1.
  950. if (base.s < 0 || !d || !d[0] || base.eq(1)) return new Ctor(NaN);
  951. isBase10 = base.eq(10);
  952. }
  953. d = arg.d;
  954. // Is arg negative, non-finite, 0 or 1?
  955. if (arg.s < 0 || !d || !d[0] || arg.eq(1)) {
  956. return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0);
  957. }
  958. // The result will have a non-terminating decimal expansion if base is 10 and arg is not an
  959. // integer power of 10.
  960. if (isBase10) {
  961. if (d.length > 1) {
  962. inf = true;
  963. } else {
  964. for (k = d[0]; k % 10 === 0;) k /= 10;
  965. inf = k !== 1;
  966. }
  967. }
  968. external = false;
  969. sd = pr + guard;
  970. num = naturalLogarithm(arg, sd);
  971. denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
  972. // The result will have 5 rounding digits.
  973. r = divide(num, denominator, sd, 1);
  974. // If at a rounding boundary, i.e. the result's rounding digits are [49]9999 or [50]0000,
  975. // calculate 10 further digits.
  976. //
  977. // If the result is known to have an infinite decimal expansion, repeat this until it is clear
  978. // that the result is above or below the boundary. Otherwise, if after calculating the 10
  979. // further digits, the last 14 are nines, round up and assume the result is exact.
  980. // Also assume the result is exact if the last 14 are zero.
  981. //
  982. // Example of a result that will be incorrectly rounded:
  983. // log[1048576](4503599627370502) = 2.60000000000000009610279511444746...
  984. // The above result correctly rounded using ROUND_CEIL to 1 decimal place should be 2.7, but it
  985. // will be given as 2.6 as there are 15 zeros immediately after the requested decimal place, so
  986. // the exact result would be assumed to be 2.6, which rounded using ROUND_CEIL to 1 decimal
  987. // place is still 2.6.
  988. if (checkRoundingDigits(r.d, k = pr, rm)) {
  989. do {
  990. sd += 10;
  991. num = naturalLogarithm(arg, sd);
  992. denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
  993. r = divide(num, denominator, sd, 1);
  994. if (!inf) {
  995. // Check for 14 nines from the 2nd rounding digit, as the first may be 4.
  996. if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) {
  997. r = finalise(r, pr + 1, 0);
  998. }
  999. break;
  1000. }
  1001. } while (checkRoundingDigits(r.d, k += 10, rm));
  1002. }
  1003. external = true;
  1004. return finalise(r, pr, rm);
  1005. };
  1006. /*
  1007. * Return a new Decimal whose value is the maximum of the arguments and the value of this Decimal.
  1008. *
  1009. * arguments {number|string|Decimal}
  1010. *
  1011. P.max = function () {
  1012. Array.prototype.push.call(arguments, this);
  1013. return maxOrMin(this.constructor, arguments, 'lt');
  1014. };
  1015. */
  1016. /*
  1017. * Return a new Decimal whose value is the minimum of the arguments and the value of this Decimal.
  1018. *
  1019. * arguments {number|string|Decimal}
  1020. *
  1021. P.min = function () {
  1022. Array.prototype.push.call(arguments, this);
  1023. return maxOrMin(this.constructor, arguments, 'gt');
  1024. };
  1025. */
  1026. /*
  1027. * n - 0 = n
  1028. * n - N = N
  1029. * n - I = -I
  1030. * 0 - n = -n
  1031. * 0 - 0 = 0
  1032. * 0 - N = N
  1033. * 0 - I = -I
  1034. * N - n = N
  1035. * N - 0 = N
  1036. * N - N = N
  1037. * N - I = N
  1038. * I - n = I
  1039. * I - 0 = I
  1040. * I - N = N
  1041. * I - I = N
  1042. *
  1043. * Return a new Decimal whose value is the value of this Decimal minus `y`, rounded to `precision`
  1044. * significant digits using rounding mode `rounding`.
  1045. *
  1046. */
  1047. P.minus = P.sub = function (y) {
  1048. var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd,
  1049. x = this,
  1050. Ctor = x.constructor;
  1051. y = new Ctor(y);
  1052. // If either is not finite...
  1053. if (!x.d || !y.d) {
  1054. // Return NaN if either is NaN.
  1055. if (!x.s || !y.s) y = new Ctor(NaN);
  1056. // Return y negated if x is finite and y is ±Infinity.
  1057. else if (x.d) y.s = -y.s;
  1058. // Return x if y is finite and x is ±Infinity.
  1059. // Return x if both are ±Infinity with different signs.
  1060. // Return NaN if both are ±Infinity with the same sign.
  1061. else y = new Ctor(y.d || x.s !== y.s ? x : NaN);
  1062. return y;
  1063. }
  1064. // If signs differ...
  1065. if (x.s != y.s) {
  1066. y.s = -y.s;
  1067. return x.plus(y);
  1068. }
  1069. xd = x.d;
  1070. yd = y.d;
  1071. pr = Ctor.precision;
  1072. rm = Ctor.rounding;
  1073. // If either is zero...
  1074. if (!xd[0] || !yd[0]) {
  1075. // Return y negated if x is zero and y is non-zero.
  1076. if (yd[0]) y.s = -y.s;
  1077. // Return x if y is zero and x is non-zero.
  1078. else if (xd[0]) y = new Ctor(x);
  1079. // Return zero if both are zero.
  1080. // From IEEE 754 (2008) 6.3: 0 - 0 = -0 - -0 = -0 when rounding to -Infinity.
  1081. else return new Ctor(rm === 3 ? -0 : 0);
  1082. return external ? finalise(y, pr, rm) : y;
  1083. }
  1084. // x and y are finite, non-zero numbers with the same sign.
  1085. // Calculate base 1e7 exponents.
  1086. e = mathfloor(y.e / LOG_BASE);
  1087. xe = mathfloor(x.e / LOG_BASE);
  1088. xd = xd.slice();
  1089. k = xe - e;
  1090. // If base 1e7 exponents differ...
  1091. if (k) {
  1092. xLTy = k < 0;
  1093. if (xLTy) {
  1094. d = xd;
  1095. k = -k;
  1096. len = yd.length;
  1097. } else {
  1098. d = yd;
  1099. e = xe;
  1100. len = xd.length;
  1101. }
  1102. // Numbers with massively different exponents would result in a very high number of
  1103. // zeros needing to be prepended, but this can be avoided while still ensuring correct
  1104. // rounding by limiting the number of zeros to `Math.ceil(pr / LOG_BASE) + 2`.
  1105. i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
  1106. if (k > i) {
  1107. k = i;
  1108. d.length = 1;
  1109. }
  1110. // Prepend zeros to equalise exponents.
  1111. d.reverse();
  1112. for (i = k; i--;) d.push(0);
  1113. d.reverse();
  1114. // Base 1e7 exponents equal.
  1115. } else {
  1116. // Check digits to determine which is the bigger number.
  1117. i = xd.length;
  1118. len = yd.length;
  1119. xLTy = i < len;
  1120. if (xLTy) len = i;
  1121. for (i = 0; i < len; i++) {
  1122. if (xd[i] != yd[i]) {
  1123. xLTy = xd[i] < yd[i];
  1124. break;
  1125. }
  1126. }
  1127. k = 0;
  1128. }
  1129. if (xLTy) {
  1130. d = xd;
  1131. xd = yd;
  1132. yd = d;
  1133. y.s = -y.s;
  1134. }
  1135. len = xd.length;
  1136. // Append zeros to `xd` if shorter.
  1137. // Don't add zeros to `yd` if shorter as subtraction only needs to start at `yd` length.
  1138. for (i = yd.length - len; i > 0; --i) xd[len++] = 0;
  1139. // Subtract yd from xd.
  1140. for (i = yd.length; i > k;) {
  1141. if (xd[--i] < yd[i]) {
  1142. for (j = i; j && xd[--j] === 0;) xd[j] = BASE - 1;
  1143. --xd[j];
  1144. xd[i] += BASE;
  1145. }
  1146. xd[i] -= yd[i];
  1147. }
  1148. // Remove trailing zeros.
  1149. for (; xd[--len] === 0;) xd.pop();
  1150. // Remove leading zeros and adjust exponent accordingly.
  1151. for (; xd[0] === 0; xd.shift()) --e;
  1152. // Zero?
  1153. if (!xd[0]) return new Ctor(rm === 3 ? -0 : 0);
  1154. y.d = xd;
  1155. y.e = getBase10Exponent(xd, e);
  1156. return external ? finalise(y, pr, rm) : y;
  1157. };
  1158. /*
  1159. * n % 0 = N
  1160. * n % N = N
  1161. * n % I = n
  1162. * 0 % n = 0
  1163. * -0 % n = -0
  1164. * 0 % 0 = N
  1165. * 0 % N = N
  1166. * 0 % I = 0
  1167. * N % n = N
  1168. * N % 0 = N
  1169. * N % N = N
  1170. * N % I = N
  1171. * I % n = N
  1172. * I % 0 = N
  1173. * I % N = N
  1174. * I % I = N
  1175. *
  1176. * Return a new Decimal whose value is the value of this Decimal modulo `y`, rounded to
  1177. * `precision` significant digits using rounding mode `rounding`.
  1178. *
  1179. * The result depends on the modulo mode.
  1180. *
  1181. */
  1182. P.modulo = P.mod = function (y) {
  1183. var q,
  1184. x = this,
  1185. Ctor = x.constructor;
  1186. y = new Ctor(y);
  1187. // Return NaN if x is ±Infinity or NaN, or y is NaN or ±0.
  1188. if (!x.d || !y.s || y.d && !y.d[0]) return new Ctor(NaN);
  1189. // Return x if y is ±Infinity or x is ±0.
  1190. if (!y.d || x.d && !x.d[0]) {
  1191. return finalise(new Ctor(x), Ctor.precision, Ctor.rounding);
  1192. }
  1193. // Prevent rounding of intermediate calculations.
  1194. external = false;
  1195. if (Ctor.modulo == 9) {
  1196. // Euclidian division: q = sign(y) * floor(x / abs(y))
  1197. // result = x - q * y where 0 <= result < abs(y)
  1198. q = divide(x, y.abs(), 0, 3, 1);
  1199. q.s *= y.s;
  1200. } else {
  1201. q = divide(x, y, 0, Ctor.modulo, 1);
  1202. }
  1203. q = q.times(y);
  1204. external = true;
  1205. return x.minus(q);
  1206. };
  1207. /*
  1208. * Return a new Decimal whose value is the natural exponential of the value of this Decimal,
  1209. * i.e. the base e raised to the power the value of this Decimal, rounded to `precision`
  1210. * significant digits using rounding mode `rounding`.
  1211. *
  1212. */
  1213. P.naturalExponential = P.exp = function () {
  1214. return naturalExponential(this);
  1215. };
  1216. /*
  1217. * Return a new Decimal whose value is the natural logarithm of the value of this Decimal,
  1218. * rounded to `precision` significant digits using rounding mode `rounding`.
  1219. *
  1220. */
  1221. P.naturalLogarithm = P.ln = function () {
  1222. return naturalLogarithm(this);
  1223. };
  1224. /*
  1225. * Return a new Decimal whose value is the value of this Decimal negated, i.e. as if multiplied by
  1226. * -1.
  1227. *
  1228. */
  1229. P.negated = P.neg = function () {
  1230. var x = new this.constructor(this);
  1231. x.s = -x.s;
  1232. return finalise(x);
  1233. };
  1234. /*
  1235. * n + 0 = n
  1236. * n + N = N
  1237. * n + I = I
  1238. * 0 + n = n
  1239. * 0 + 0 = 0
  1240. * 0 + N = N
  1241. * 0 + I = I
  1242. * N + n = N
  1243. * N + 0 = N
  1244. * N + N = N
  1245. * N + I = N
  1246. * I + n = I
  1247. * I + 0 = I
  1248. * I + N = N
  1249. * I + I = I
  1250. *
  1251. * Return a new Decimal whose value is the value of this Decimal plus `y`, rounded to `precision`
  1252. * significant digits using rounding mode `rounding`.
  1253. *
  1254. */
  1255. P.plus = P.add = function (y) {
  1256. var carry, d, e, i, k, len, pr, rm, xd, yd,
  1257. x = this,
  1258. Ctor = x.constructor;
  1259. y = new Ctor(y);
  1260. // If either is not finite...
  1261. if (!x.d || !y.d) {
  1262. // Return NaN if either is NaN.
  1263. if (!x.s || !y.s) y = new Ctor(NaN);
  1264. // Return x if y is finite and x is ±Infinity.
  1265. // Return x if both are ±Infinity with the same sign.
  1266. // Return NaN if both are ±Infinity with different signs.
  1267. // Return y if x is finite and y is ±Infinity.
  1268. else if (!x.d) y = new Ctor(y.d || x.s === y.s ? x : NaN);
  1269. return y;
  1270. }
  1271. // If signs differ...
  1272. if (x.s != y.s) {
  1273. y.s = -y.s;
  1274. return x.minus(y);
  1275. }
  1276. xd = x.d;
  1277. yd = y.d;
  1278. pr = Ctor.precision;
  1279. rm = Ctor.rounding;
  1280. // If either is zero...
  1281. if (!xd[0] || !yd[0]) {
  1282. // Return x if y is zero.
  1283. // Return y if y is non-zero.
  1284. if (!yd[0]) y = new Ctor(x);
  1285. return external ? finalise(y, pr, rm) : y;
  1286. }
  1287. // x and y are finite, non-zero numbers with the same sign.
  1288. // Calculate base 1e7 exponents.
  1289. k = mathfloor(x.e / LOG_BASE);
  1290. e = mathfloor(y.e / LOG_BASE);
  1291. xd = xd.slice();
  1292. i = k - e;
  1293. // If base 1e7 exponents differ...
  1294. if (i) {
  1295. if (i < 0) {
  1296. d = xd;
  1297. i = -i;
  1298. len = yd.length;
  1299. } else {
  1300. d = yd;
  1301. e = k;
  1302. len = xd.length;
  1303. }
  1304. // Limit number of zeros prepended to max(ceil(pr / LOG_BASE), len) + 1.
  1305. k = Math.ceil(pr / LOG_BASE);
  1306. len = k > len ? k + 1 : len + 1;
  1307. if (i > len) {
  1308. i = len;
  1309. d.length = 1;
  1310. }
  1311. // Prepend zeros to equalise exponents. Note: Faster to use reverse then do unshifts.
  1312. d.reverse();
  1313. for (; i--;) d.push(0);
  1314. d.reverse();
  1315. }
  1316. len = xd.length;
  1317. i = yd.length;
  1318. // If yd is longer than xd, swap xd and yd so xd points to the longer array.
  1319. if (len - i < 0) {
  1320. i = len;
  1321. d = yd;
  1322. yd = xd;
  1323. xd = d;
  1324. }
  1325. // Only start adding at yd.length - 1 as the further digits of xd can be left as they are.
  1326. for (carry = 0; i;) {
  1327. carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
  1328. xd[i] %= BASE;
  1329. }
  1330. if (carry) {
  1331. xd.unshift(carry);
  1332. ++e;
  1333. }
  1334. // Remove trailing zeros.
  1335. // No need to check for zero, as +x + +y != 0 && -x + -y != 0
  1336. for (len = xd.length; xd[--len] == 0;) xd.pop();
  1337. y.d = xd;
  1338. y.e = getBase10Exponent(xd, e);
  1339. return external ? finalise(y, pr, rm) : y;
  1340. };
  1341. /*
  1342. * Return the number of significant digits of the value of this Decimal.
  1343. *
  1344. * [z] {boolean|number} Whether to count integer-part trailing zeros: true, false, 1 or 0.
  1345. *
  1346. */
  1347. P.precision = P.sd = function (z) {
  1348. var k,
  1349. x = this;
  1350. if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) throw Error(invalidArgument + z);
  1351. if (x.d) {
  1352. k = getPrecision(x.d);
  1353. if (z && x.e + 1 > k) k = x.e + 1;
  1354. } else {
  1355. k = NaN;
  1356. }
  1357. return k;
  1358. };
  1359. /*
  1360. * Return a new Decimal whose value is the value of this Decimal rounded to a whole number using
  1361. * rounding mode `rounding`.
  1362. *
  1363. */
  1364. P.round = function () {
  1365. var x = this,
  1366. Ctor = x.constructor;
  1367. return finalise(new Ctor(x), x.e + 1, Ctor.rounding);
  1368. };
  1369. /*
  1370. * Return a new Decimal whose value is the sine of the value in radians of this Decimal.
  1371. *
  1372. * Domain: [-Infinity, Infinity]
  1373. * Range: [-1, 1]
  1374. *
  1375. * sin(x) = x - x^3/3! + x^5/5! - ...
  1376. *
  1377. * sin(0) = 0
  1378. * sin(-0) = -0
  1379. * sin(Infinity) = NaN
  1380. * sin(-Infinity) = NaN
  1381. * sin(NaN) = NaN
  1382. *
  1383. */
  1384. P.sine = P.sin = function () {
  1385. var pr, rm,
  1386. x = this,
  1387. Ctor = x.constructor;
  1388. if (!x.isFinite()) return new Ctor(NaN);
  1389. if (x.isZero()) return new Ctor(x);
  1390. pr = Ctor.precision;
  1391. rm = Ctor.rounding;
  1392. Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
  1393. Ctor.rounding = 1;
  1394. x = sine(Ctor, toLessThanHalfPi(Ctor, x));
  1395. Ctor.precision = pr;
  1396. Ctor.rounding = rm;
  1397. return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true);
  1398. };
  1399. /*
  1400. * Return a new Decimal whose value is the square root of this Decimal, rounded to `precision`
  1401. * significant digits using rounding mode `rounding`.
  1402. *
  1403. * sqrt(-n) = N
  1404. * sqrt(N) = N
  1405. * sqrt(-I) = N
  1406. * sqrt(I) = I
  1407. * sqrt(0) = 0
  1408. * sqrt(-0) = -0
  1409. *
  1410. */
  1411. P.squareRoot = P.sqrt = function () {
  1412. var m, n, sd, r, rep, t,
  1413. x = this,
  1414. d = x.d,
  1415. e = x.e,
  1416. s = x.s,
  1417. Ctor = x.constructor;
  1418. // Negative/NaN/Infinity/zero?
  1419. if (s !== 1 || !d || !d[0]) {
  1420. return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0);
  1421. }
  1422. external = false;
  1423. // Initial estimate.
  1424. s = Math.sqrt(+x);
  1425. // Math.sqrt underflow/overflow?
  1426. // Pass x to Math.sqrt as integer, then adjust the exponent of the result.
  1427. if (s == 0 || s == 1 / 0) {
  1428. n = digitsToString(d);
  1429. if ((n.length + e) % 2 == 0) n += '0';
  1430. s = Math.sqrt(n);
  1431. e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
  1432. if (s == 1 / 0) {
  1433. n = '5e' + e;
  1434. } else {
  1435. n = s.toExponential();
  1436. n = n.slice(0, n.indexOf('e') + 1) + e;
  1437. }
  1438. r = new Ctor(n);
  1439. } else {
  1440. r = new Ctor(s.toString());
  1441. }
  1442. sd = (e = Ctor.precision) + 3;
  1443. // Newton-Raphson iteration.
  1444. for (;;) {
  1445. t = r;
  1446. r = t.plus(divide(x, t, sd + 2, 1)).times(0.5);
  1447. // TODO? Replace with for-loop and checkRoundingDigits.
  1448. if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
  1449. n = n.slice(sd - 3, sd + 1);
  1450. // The 4th rounding digit may be in error by -1 so if the 4 rounding digits are 9999 or
  1451. // 4999, i.e. approaching a rounding boundary, continue the iteration.
  1452. if (n == '9999' || !rep && n == '4999') {
  1453. // On the first iteration only, check to see if rounding up gives the exact result as the
  1454. // nines may infinitely repeat.
  1455. if (!rep) {
  1456. finalise(t, e + 1, 0);
  1457. if (t.times(t).eq(x)) {
  1458. r = t;
  1459. break;
  1460. }
  1461. }
  1462. sd += 4;
  1463. rep = 1;
  1464. } else {
  1465. // If the rounding digits are null, 0{0,4} or 50{0,3}, check for an exact result.
  1466. // If not, then there are further digits and m will be truthy.
  1467. if (!+n || !+n.slice(1) && n.charAt(0) == '5') {
  1468. // Truncate to the first rounding digit.
  1469. finalise(r, e + 1, 1);
  1470. m = !r.times(r).eq(x);
  1471. }
  1472. break;
  1473. }
  1474. }
  1475. }
  1476. external = true;
  1477. return finalise(r, e, Ctor.rounding, m);
  1478. };
  1479. /*
  1480. * Return a new Decimal whose value is the tangent of the value in radians of this Decimal.
  1481. *
  1482. * Domain: [-Infinity, Infinity]
  1483. * Range: [-Infinity, Infinity]
  1484. *
  1485. * tan(0) = 0
  1486. * tan(-0) = -0
  1487. * tan(Infinity) = NaN
  1488. * tan(-Infinity) = NaN
  1489. * tan(NaN) = NaN
  1490. *
  1491. */
  1492. P.tangent = P.tan = function () {
  1493. var pr, rm,
  1494. x = this,
  1495. Ctor = x.constructor;
  1496. if (!x.isFinite()) return new Ctor(NaN);
  1497. if (x.isZero()) return new Ctor(x);
  1498. pr = Ctor.precision;
  1499. rm = Ctor.rounding;
  1500. Ctor.precision = pr + 10;
  1501. Ctor.rounding = 1;
  1502. x = x.sin();
  1503. x.s = 1;
  1504. x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0);
  1505. Ctor.precision = pr;
  1506. Ctor.rounding = rm;
  1507. return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true);
  1508. };
  1509. /*
  1510. * n * 0 = 0
  1511. * n * N = N
  1512. * n * I = I
  1513. * 0 * n = 0
  1514. * 0 * 0 = 0
  1515. * 0 * N = N
  1516. * 0 * I = N
  1517. * N * n = N
  1518. * N * 0 = N
  1519. * N * N = N
  1520. * N * I = N
  1521. * I * n = I
  1522. * I * 0 = N
  1523. * I * N = N
  1524. * I * I = I
  1525. *
  1526. * Return a new Decimal whose value is this Decimal times `y`, rounded to `precision` significant
  1527. * digits using rounding mode `rounding`.
  1528. *
  1529. */
  1530. P.times = P.mul = function (y) {
  1531. var carry, e, i, k, r, rL, t, xdL, ydL,
  1532. x = this,
  1533. Ctor = x.constructor,
  1534. xd = x.d,
  1535. yd = (y = new Ctor(y)).d;
  1536. y.s *= x.s;
  1537. // If either is NaN, ±Infinity or ±0...
  1538. if (!xd || !xd[0] || !yd || !yd[0]) {
  1539. return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd
  1540. // Return NaN if either is NaN.
  1541. // Return NaN if x is ±0 and y is ±Infinity, or y is ±0 and x is ±Infinity.
  1542. ? NaN
  1543. // Return ±Infinity if either is ±Infinity.
  1544. // Return ±0 if either is ±0.
  1545. : !xd || !yd ? y.s / 0 : y.s * 0);
  1546. }
  1547. e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE);
  1548. xdL = xd.length;
  1549. ydL = yd.length;
  1550. // Ensure xd points to the longer array.
  1551. if (xdL < ydL) {
  1552. r = xd;
  1553. xd = yd;
  1554. yd = r;
  1555. rL = xdL;
  1556. xdL = ydL;
  1557. ydL = rL;
  1558. }
  1559. // Initialise the result array with zeros.
  1560. r = [];
  1561. rL = xdL + ydL;
  1562. for (i = rL; i--;) r.push(0);
  1563. // Multiply!
  1564. for (i = ydL; --i >= 0;) {
  1565. carry = 0;
  1566. for (k = xdL + i; k > i;) {
  1567. t = r[k] + yd[i] * xd[k - i - 1] + carry;
  1568. r[k--] = t % BASE | 0;
  1569. carry = t / BASE | 0;
  1570. }
  1571. r[k] = (r[k] + carry) % BASE | 0;
  1572. }
  1573. // Remove trailing zeros.
  1574. for (; !r[--rL];) r.pop();
  1575. if (carry) ++e;
  1576. else r.shift();
  1577. y.d = r;
  1578. y.e = getBase10Exponent(r, e);
  1579. return external ? finalise(y, Ctor.precision, Ctor.rounding) : y;
  1580. };
  1581. /*
  1582. * Return a string representing the value of this Decimal in base 2, round to `sd` significant
  1583. * digits using rounding mode `rm`.
  1584. *
  1585. * If the optional `sd` argument is present then return binary exponential notation.
  1586. *
  1587. * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
  1588. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
  1589. *
  1590. */
  1591. P.toBinary = function (sd, rm) {
  1592. return toStringBinary(this, 2, sd, rm);
  1593. };
  1594. /*
  1595. * Return a new Decimal whose value is the value of this Decimal rounded to a maximum of `dp`
  1596. * decimal places using rounding mode `rm` or `rounding` if `rm` is omitted.
  1597. *
  1598. * If `dp` is omitted, return a new Decimal whose value is the value of this Decimal.
  1599. *
  1600. * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive.
  1601. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
  1602. *
  1603. */
  1604. P.toDecimalPlaces = P.toDP = function (dp, rm) {
  1605. var x = this,
  1606. Ctor = x.constructor;
  1607. x = new Ctor(x);
  1608. if (dp === void 0) return x;
  1609. checkInt32(dp, 0, MAX_DIGITS);
  1610. if (rm === void 0) rm = Ctor.rounding;
  1611. else checkInt32(rm, 0, 8);
  1612. return finalise(x, dp + x.e + 1, rm);
  1613. };
  1614. /*
  1615. * Return a string representing the value of this Decimal in exponential notation rounded to
  1616. * `dp` fixed decimal places using rounding mode `rounding`.
  1617. *
  1618. * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive.
  1619. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
  1620. *
  1621. */
  1622. P.toExponential = function (dp, rm) {
  1623. var str,
  1624. x = this,
  1625. Ctor = x.constructor;
  1626. if (dp === void 0) {
  1627. str = finiteToString(x, true);
  1628. } else {
  1629. checkInt32(dp, 0, MAX_DIGITS);
  1630. if (rm === void 0) rm = Ctor.rounding;
  1631. else checkInt32(rm, 0, 8);
  1632. x = finalise(new Ctor(x), dp + 1, rm);
  1633. str = finiteToString(x, true, dp + 1);
  1634. }
  1635. return x.isNeg() && !x.isZero() ? '-' + str : str;
  1636. };
  1637. /*
  1638. * Return a string representing the value of this Decimal in normal (fixed-point) notation to
  1639. * `dp` fixed decimal places and rounded using rounding mode `rm` or `rounding` if `rm` is
  1640. * omitted.
  1641. *
  1642. * As with JavaScript numbers, (-0).toFixed(0) is '0', but e.g. (-0.00001).toFixed(0) is '-0'.
  1643. *
  1644. * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive.
  1645. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
  1646. *
  1647. * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'.
  1648. * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'.
  1649. * (-0).toFixed(3) is '0.000'.
  1650. * (-0.5).toFixed(0) is '-0'.
  1651. *
  1652. */
  1653. P.toFixed = function (dp, rm) {
  1654. var str, y,
  1655. x = this,
  1656. Ctor = x.constructor;
  1657. if (dp === void 0) {
  1658. str = finiteToString(x);
  1659. } else {
  1660. checkInt32(dp, 0, MAX_DIGITS);
  1661. if (rm === void 0) rm = Ctor.rounding;
  1662. else checkInt32(rm, 0, 8);
  1663. y = finalise(new Ctor(x), dp + x.e + 1, rm);
  1664. str = finiteToString(y, false, dp + y.e + 1);
  1665. }
  1666. // To determine whether to add the minus sign look at the value before it was rounded,
  1667. // i.e. look at `x` rather than `y`.
  1668. return x.isNeg() && !x.isZero() ? '-' + str : str;
  1669. };
  1670. /*
  1671. * Return an array representing the value of this Decimal as a simple fraction with an integer
  1672. * numerator and an integer denominator.
  1673. *
  1674. * The denominator will be a positive non-zero value less than or equal to the specified maximum
  1675. * denominator. If a maximum denominator is not specified, the denominator will be the lowest
  1676. * value necessary to represent the number exactly.
  1677. *
  1678. * [maxD] {number|string|Decimal} Maximum denominator. Integer >= 1 and < Infinity.
  1679. *
  1680. */
  1681. P.toFraction = function (maxD) {
  1682. var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r,
  1683. x = this,
  1684. xd = x.d,
  1685. Ctor = x.constructor;
  1686. if (!xd) return new Ctor(x);
  1687. n1 = d0 = new Ctor(1);
  1688. d1 = n0 = new Ctor(0);
  1689. d = new Ctor(d1);
  1690. e = d.e = getPrecision(xd) - x.e - 1;
  1691. k = e % LOG_BASE;
  1692. d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k);
  1693. if (maxD == null) {
  1694. // d is 10**e, the minimum max-denominator needed.
  1695. maxD = e > 0 ? d : n1;
  1696. } else {
  1697. n = new Ctor(maxD);
  1698. if (!n.isInt() || n.lt(n1)) throw Error(invalidArgument + n);
  1699. maxD = n.gt(d) ? (e > 0 ? d : n1) : n;
  1700. }
  1701. external = false;
  1702. n = new Ctor(digitsToString(xd));
  1703. pr = Ctor.precision;
  1704. Ctor.precision = e = xd.length * LOG_BASE * 2;
  1705. for (;;) {
  1706. q = divide(n, d, 0, 1, 1);
  1707. d2 = d0.plus(q.times(d1));
  1708. if (d2.cmp(maxD) == 1) break;
  1709. d0 = d1;
  1710. d1 = d2;
  1711. d2 = n1;
  1712. n1 = n0.plus(q.times(d2));
  1713. n0 = d2;
  1714. d2 = d;
  1715. d = n.minus(q.times(d2));
  1716. n = d2;
  1717. }
  1718. d2 = divide(maxD.minus(d0), d1, 0, 1, 1);
  1719. n0 = n0.plus(d2.times(n1));
  1720. d0 = d0.plus(d2.times(d1));
  1721. n0.s = n1.s = x.s;
  1722. // Determine which fraction is closer to x, n0/d0 or n1/d1?
  1723. r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1
  1724. ? [n1, d1] : [n0, d0];
  1725. Ctor.precision = pr;
  1726. external = true;
  1727. return r;
  1728. };
  1729. /*
  1730. * Return a string representing the value of this Decimal in base 16, round to `sd` significant
  1731. * digits using rounding mode `rm`.
  1732. *
  1733. * If the optional `sd` argument is present then return binary exponential notation.
  1734. *
  1735. * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
  1736. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
  1737. *
  1738. */
  1739. P.toHexadecimal = P.toHex = function (sd, rm) {
  1740. return toStringBinary(this, 16, sd, rm);
  1741. };
  1742. /*
  1743. * Returns a new Decimal whose value is the nearest multiple of `y` in the direction of rounding
  1744. * mode `rm`, or `Decimal.rounding` if `rm` is omitted, to the value of this Decimal.
  1745. *
  1746. * The return value will always have the same sign as this Decimal, unless either this Decimal
  1747. * or `y` is NaN, in which case the return value will be also be NaN.
  1748. *
  1749. * The return value is not affected by the value of `precision`.
  1750. *
  1751. * y {number|string|Decimal} The magnitude to round to a multiple of.
  1752. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
  1753. *
  1754. * 'toNearest() rounding mode not an integer: {rm}'
  1755. * 'toNearest() rounding mode out of range: {rm}'
  1756. *
  1757. */
  1758. P.toNearest = function (y, rm) {
  1759. var x = this,
  1760. Ctor = x.constructor;
  1761. x = new Ctor(x);
  1762. if (y == null) {
  1763. // If x is not finite, return x.
  1764. if (!x.d) return x;
  1765. y = new Ctor(1);
  1766. rm = Ctor.rounding;
  1767. } else {
  1768. y = new Ctor(y);
  1769. if (rm === void 0) {
  1770. rm = Ctor.rounding;
  1771. } else {
  1772. checkInt32(rm, 0, 8);
  1773. }
  1774. // If x is not finite, return x if y is not NaN, else NaN.
  1775. if (!x.d) return y.s ? x : y;
  1776. // If y is not finite, return Infinity with the sign of x if y is Infinity, else NaN.
  1777. if (!y.d) {
  1778. if (y.s) y.s = x.s;
  1779. return y;
  1780. }
  1781. }
  1782. // If y is not zero, calculate the nearest multiple of y to x.
  1783. if (y.d[0]) {
  1784. external = false;
  1785. x = divide(x, y, 0, rm, 1).times(y);
  1786. external = true;
  1787. finalise(x);
  1788. // If y is zero, return zero with the sign of x.
  1789. } else {
  1790. y.s = x.s;
  1791. x = y;
  1792. }
  1793. return x;
  1794. };
  1795. /*
  1796. * Return the value of this Decimal converted to a number primitive.
  1797. * Zero keeps its sign.
  1798. *
  1799. */
  1800. P.toNumber = function () {
  1801. return +this;
  1802. };
  1803. /*
  1804. * Return a string representing the value of this Decimal in base 8, round to `sd` significant
  1805. * digits using rounding mode `rm`.
  1806. *
  1807. * If the optional `sd` argument is present then return binary exponential notation.
  1808. *
  1809. * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
  1810. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
  1811. *
  1812. */
  1813. P.toOctal = function (sd, rm) {
  1814. return toStringBinary(this, 8, sd, rm);
  1815. };
  1816. /*
  1817. * Return a new Decimal whose value is the value of this Decimal raised to the power `y`, rounded
  1818. * to `precision` significant digits using rounding mode `rounding`.
  1819. *
  1820. * ECMAScript compliant.
  1821. *
  1822. * pow(x, NaN) = NaN
  1823. * pow(x, ±0) = 1
  1824. * pow(NaN, non-zero) = NaN
  1825. * pow(abs(x) > 1, +Infinity) = +Infinity
  1826. * pow(abs(x) > 1, -Infinity) = +0
  1827. * pow(abs(x) == 1, ±Infinity) = NaN
  1828. * pow(abs(x) < 1, +Infinity) = +0
  1829. * pow(abs(x) < 1, -Infinity) = +Infinity
  1830. * pow(+Infinity, y > 0) = +Infinity
  1831. * pow(+Infinity, y < 0) = +0
  1832. * pow(-Infinity, odd integer > 0) = -Infinity
  1833. * pow(-Infinity, even integer > 0) = +Infinity
  1834. * pow(-Infinity, odd integer < 0) = -0
  1835. * pow(-Infinity, even integer < 0) = +0
  1836. * pow(+0, y > 0) = +0
  1837. * pow(+0, y < 0) = +Infinity
  1838. * pow(-0, odd integer > 0) = -0
  1839. * pow(-0, even integer > 0) = +0
  1840. * pow(-0, odd integer < 0) = -Infinity
  1841. * pow(-0, even integer < 0) = +Infinity
  1842. * pow(finite x < 0, finite non-integer) = NaN
  1843. *
  1844. * For non-integer or very large exponents pow(x, y) is calculated using
  1845. *
  1846. * x^y = exp(y*ln(x))
  1847. *
  1848. * Assuming the first 15 rounding digits are each equally likely to be any digit 0-9, the
  1849. * probability of an incorrectly rounded result
  1850. * P([49]9{14} | [50]0{14}) = 2 * 0.2 * 10^-14 = 4e-15 = 1/2.5e+14
  1851. * i.e. 1 in 250,000,000,000,000
  1852. *
  1853. * If a result is incorrectly rounded the maximum error will be 1 ulp (unit in last place).
  1854. *
  1855. * y {number|string|Decimal} The power to which to raise this Decimal.
  1856. *
  1857. */
  1858. P.toPower = P.pow = function (y) {
  1859. var e, k, pr, r, rm, s,
  1860. x = this,
  1861. Ctor = x.constructor,
  1862. yn = +(y = new Ctor(y));
  1863. // Either ±Infinity, NaN or ±0?
  1864. if (!x.d || !y.d || !x.d[0] || !y.d[0]) return new Ctor(mathpow(+x, yn));
  1865. x = new Ctor(x);
  1866. if (x.eq(1)) return x;
  1867. pr = Ctor.precision;
  1868. rm = Ctor.rounding;
  1869. if (y.eq(1)) return finalise(x, pr, rm);
  1870. // y exponent
  1871. e = mathfloor(y.e / LOG_BASE);
  1872. // If y is a small integer use the 'exponentiation by squaring' algorithm.
  1873. if (e >= y.d.length - 1 && (k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) {
  1874. r = intPow(Ctor, x, k, pr);
  1875. return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm);
  1876. }
  1877. s = x.s;
  1878. // if x is negative
  1879. if (s < 0) {
  1880. // if y is not an integer
  1881. if (e < y.d.length - 1) return new Ctor(NaN);
  1882. // Result is positive if x is negative and the last digit of integer y is even.
  1883. if ((y.d[e] & 1) == 0) s = 1;
  1884. // if x.eq(-1)
  1885. if (x.e == 0 && x.d[0] == 1 && x.d.length == 1) {
  1886. x.s = s;
  1887. return x;
  1888. }
  1889. }
  1890. // Estimate result exponent.
  1891. // x^y = 10^e, where e = y * log10(x)
  1892. // log10(x) = log10(x_significand) + x_exponent
  1893. // log10(x_significand) = ln(x_significand) / ln(10)
  1894. k = mathpow(+x, yn);
  1895. e = k == 0 || !isFinite(k)
  1896. ? mathfloor(yn * (Math.log('0.' + digitsToString(x.d)) / Math.LN10 + x.e + 1))
  1897. : new Ctor(k + '').e;
  1898. // Exponent estimate may be incorrect e.g. x: 0.999999999999999999, y: 2.29, e: 0, r.e: -1.
  1899. // Overflow/underflow?
  1900. if (e > Ctor.maxE + 1 || e < Ctor.minE - 1) return new Ctor(e > 0 ? s / 0 : 0);
  1901. external = false;
  1902. Ctor.rounding = x.s = 1;
  1903. // Estimate the extra guard digits needed to ensure five correct rounding digits from
  1904. // naturalLogarithm(x). Example of failure without these extra digits (precision: 10):
  1905. // new Decimal(2.32456).pow('2087987436534566.46411')
  1906. // should be 1.162377823e+764914905173815, but is 1.162355823e+764914905173815
  1907. k = Math.min(12, (e + '').length);
  1908. // r = x^y = exp(y*ln(x))
  1909. r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr);
  1910. // r may be Infinity, e.g. (0.9999999999999999).pow(-1e+40)
  1911. if (r.d) {
  1912. // Truncate to the required precision plus five rounding digits.
  1913. r = finalise(r, pr + 5, 1);
  1914. // If the rounding digits are [49]9999 or [50]0000 increase the precision by 10 and recalculate
  1915. // the result.
  1916. if (checkRoundingDigits(r.d, pr, rm)) {
  1917. e = pr + 10;
  1918. // Truncate to the increased precision plus five rounding digits.
  1919. r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1);
  1920. // Check for 14 nines from the 2nd rounding digit (the first rounding digit may be 4 or 9).
  1921. if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 1e14) {
  1922. r = finalise(r, pr + 1, 0);
  1923. }
  1924. }
  1925. }
  1926. r.s = s;
  1927. external = true;
  1928. Ctor.rounding = rm;
  1929. return finalise(r, pr, rm);
  1930. };
  1931. /*
  1932. * Return a string representing the value of this Decimal rounded to `sd` significant digits
  1933. * using rounding mode `rounding`.
  1934. *
  1935. * Return exponential notation if `sd` is less than the number of digits necessary to represent
  1936. * the integer part of the value in normal notation.
  1937. *
  1938. * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
  1939. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
  1940. *
  1941. */
  1942. P.toPrecision = function (sd, rm) {
  1943. var str,
  1944. x = this,
  1945. Ctor = x.constructor;
  1946. if (sd === void 0) {
  1947. str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
  1948. } else {
  1949. checkInt32(sd, 1, MAX_DIGITS);
  1950. if (rm === void 0) rm = Ctor.rounding;
  1951. else checkInt32(rm, 0, 8);
  1952. x = finalise(new Ctor(x), sd, rm);
  1953. str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd);
  1954. }
  1955. return x.isNeg() && !x.isZero() ? '-' + str : str;
  1956. };
  1957. /*
  1958. * Return a new Decimal whose value is the value of this Decimal rounded to a maximum of `sd`
  1959. * significant digits using rounding mode `rm`, or to `precision` and `rounding` respectively if
  1960. * omitted.
  1961. *
  1962. * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
  1963. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
  1964. *
  1965. * 'toSD() digits out of range: {sd}'
  1966. * 'toSD() digits not an integer: {sd}'
  1967. * 'toSD() rounding mode not an integer: {rm}'
  1968. * 'toSD() rounding mode out of range: {rm}'
  1969. *
  1970. */
  1971. P.toSignificantDigits = P.toSD = function (sd, rm) {
  1972. var x = this,
  1973. Ctor = x.constructor;
  1974. if (sd === void 0) {
  1975. sd = Ctor.precision;
  1976. rm = Ctor.rounding;
  1977. } else {
  1978. checkInt32(sd, 1, MAX_DIGITS);
  1979. if (rm === void 0) rm = Ctor.rounding;
  1980. else checkInt32(rm, 0, 8);
  1981. }
  1982. return finalise(new Ctor(x), sd, rm);
  1983. };
  1984. /*
  1985. * Return a string representing the value of this Decimal.
  1986. *
  1987. * Return exponential notation if this Decimal has a positive exponent equal to or greater than
  1988. * `toExpPos`, or a negative exponent equal to or less than `toExpNeg`.
  1989. *
  1990. */
  1991. P.toString = function () {
  1992. var x = this,
  1993. Ctor = x.constructor,
  1994. str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
  1995. return x.isNeg() && !x.isZero() ? '-' + str : str;
  1996. };
  1997. /*
  1998. * Return a new Decimal whose value is the value of this Decimal truncated to a whole number.
  1999. *
  2000. */
  2001. P.truncated = P.trunc = function () {
  2002. return finalise(new this.constructor(this), this.e + 1, 1);
  2003. };
  2004. /*
  2005. * Return a string representing the value of this Decimal.
  2006. * Unlike `toString`, negative zero will include the minus sign.
  2007. *
  2008. */
  2009. P.valueOf = P.toJSON = function () {
  2010. var x = this,
  2011. Ctor = x.constructor,
  2012. str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
  2013. return x.isNeg() ? '-' + str : str;
  2014. };
  2015. // Helper functions for Decimal.prototype (P) and/or Decimal methods, and their callers.
  2016. /*
  2017. * digitsToString P.cubeRoot, P.logarithm, P.squareRoot, P.toFraction, P.toPower,
  2018. * finiteToString, naturalExponential, naturalLogarithm
  2019. * checkInt32 P.toDecimalPlaces, P.toExponential, P.toFixed, P.toNearest,
  2020. * P.toPrecision, P.toSignificantDigits, toStringBinary, random
  2021. * checkRoundingDigits P.logarithm, P.toPower, naturalExponential, naturalLogarithm
  2022. * convertBase toStringBinary, parseOther
  2023. * cos P.cos
  2024. * divide P.atanh, P.cubeRoot, P.dividedBy, P.dividedToIntegerBy,
  2025. * P.logarithm, P.modulo, P.squareRoot, P.tan, P.tanh, P.toFraction,
  2026. * P.toNearest, toStringBinary, naturalExponential, naturalLogarithm,
  2027. * taylorSeries, atan2, parseOther
  2028. * finalise P.absoluteValue, P.atan, P.atanh, P.ceil, P.cos, P.cosh,
  2029. * P.cubeRoot, P.dividedToIntegerBy, P.floor, P.logarithm, P.minus,
  2030. * P.modulo, P.negated, P.plus, P.round, P.sin, P.sinh, P.squareRoot,
  2031. * P.tan, P.times, P.toDecimalPlaces, P.toExponential, P.toFixed,
  2032. * P.toNearest, P.toPower, P.toPrecision, P.toSignificantDigits,
  2033. * P.truncated, divide, getLn10, getPi, naturalExponential,
  2034. * naturalLogarithm, ceil, floor, round, trunc
  2035. * finiteToString P.toExponential, P.toFixed, P.toPrecision, P.toString, P.valueOf,
  2036. * toStringBinary
  2037. * getBase10Exponent P.minus, P.plus, P.times, parseOther
  2038. * getLn10 P.logarithm, naturalLogarithm
  2039. * getPi P.acos, P.asin, P.atan, toLessThanHalfPi, atan2
  2040. * getPrecision P.precision, P.toFraction
  2041. * getZeroString digitsToString, finiteToString
  2042. * intPow P.toPower, parseOther
  2043. * isOdd toLessThanHalfPi
  2044. * maxOrMin max, min
  2045. * naturalExponential P.naturalExponential, P.toPower
  2046. * naturalLogarithm P.acosh, P.asinh, P.atanh, P.logarithm, P.naturalLogarithm,
  2047. * P.toPower, naturalExponential
  2048. * nonFiniteToString finiteToString, toStringBinary
  2049. * parseDecimal Decimal
  2050. * parseOther Decimal
  2051. * sin P.sin
  2052. * taylorSeries P.cosh, P.sinh, cos, sin
  2053. * toLessThanHalfPi P.cos, P.sin
  2054. * toStringBinary P.toBinary, P.toHexadecimal, P.toOctal
  2055. * truncate intPow
  2056. *
  2057. * Throws: P.logarithm, P.precision, P.toFraction, checkInt32, getLn10, getPi,
  2058. * naturalLogarithm, config, parseOther, random, Decimal
  2059. */
  2060. function digitsToString(d) {
  2061. var i, k, ws,
  2062. indexOfLastWord = d.length - 1,
  2063. str = '',
  2064. w = d[0];
  2065. if (indexOfLastWord > 0) {
  2066. str += w;
  2067. for (i = 1; i < indexOfLastWord; i++) {
  2068. ws = d[i] + '';
  2069. k = LOG_BASE - ws.length;
  2070. if (k) str += getZeroString(k);
  2071. str += ws;
  2072. }
  2073. w = d[i];
  2074. ws = w + '';
  2075. k = LOG_BASE - ws.length;
  2076. if (k) str += getZeroString(k);
  2077. } else if (w === 0) {
  2078. return '0';
  2079. }
  2080. // Remove trailing zeros of last w.
  2081. for (; w % 10 === 0;) w /= 10;
  2082. return str + w;
  2083. }
  2084. function checkInt32(i, min, max) {
  2085. if (i !== ~~i || i < min || i > max) {
  2086. throw Error(invalidArgument + i);
  2087. }
  2088. }
  2089. /*
  2090. * Check 5 rounding digits if `repeating` is null, 4 otherwise.
  2091. * `repeating == null` if caller is `log` or `pow`,
  2092. * `repeating != null` if caller is `naturalLogarithm` or `naturalExponential`.
  2093. */
  2094. function checkRoundingDigits(d, i, rm, repeating) {
  2095. var di, k, r, rd;
  2096. // Get the length of the first word of the array d.
  2097. for (k = d[0]; k >= 10; k /= 10) --i;
  2098. // Is the rounding digit in the first word of d?
  2099. if (--i < 0) {
  2100. i += LOG_BASE;
  2101. di = 0;
  2102. } else {
  2103. di = Math.ceil((i + 1) / LOG_BASE);
  2104. i %= LOG_BASE;
  2105. }
  2106. // i is the index (0 - 6) of the rounding digit.
  2107. // E.g. if within the word 3487563 the first rounding digit is 5,
  2108. // then i = 4, k = 1000, rd = 3487563 % 1000 = 563
  2109. k = mathpow(10, LOG_BASE - i);
  2110. rd = d[di] % k | 0;
  2111. if (repeating == null) {
  2112. if (i < 3) {
  2113. if (i == 0) rd = rd / 100 | 0;
  2114. else if (i == 1) rd = rd / 10 | 0;
  2115. r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 50000 || rd == 0;
  2116. } else {
  2117. r = (rm < 4 && rd + 1 == k || rm > 3 && rd + 1 == k / 2) &&
  2118. (d[di + 1] / k / 100 | 0) == mathpow(10, i - 2) - 1 ||
  2119. (rd == k / 2 || rd == 0) && (d[di + 1] / k / 100 | 0) == 0;
  2120. }
  2121. } else {
  2122. if (i < 4) {
  2123. if (i == 0) rd = rd / 1000 | 0;
  2124. else if (i == 1) rd = rd / 100 | 0;
  2125. else if (i == 2) rd = rd / 10 | 0;
  2126. r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999;
  2127. } else {
  2128. r = ((repeating || rm < 4) && rd + 1 == k ||
  2129. (!repeating && rm > 3) && rd + 1 == k / 2) &&
  2130. (d[di + 1] / k / 1000 | 0) == mathpow(10, i - 3) - 1;
  2131. }
  2132. }
  2133. return r;
  2134. }
  2135. // Convert string of `baseIn` to an array of numbers of `baseOut`.
  2136. // Eg. convertBase('255', 10, 16) returns [15, 15].
  2137. // Eg. convertBase('ff', 16, 10) returns [2, 5, 5].
  2138. function convertBase(str, baseIn, baseOut) {
  2139. var j,
  2140. arr = [0],
  2141. arrL,
  2142. i = 0,
  2143. strL = str.length;
  2144. for (; i < strL;) {
  2145. for (arrL = arr.length; arrL--;) arr[arrL] *= baseIn;
  2146. arr[0] += NUMERALS.indexOf(str.charAt(i++));
  2147. for (j = 0; j < arr.length; j++) {
  2148. if (arr[j] > baseOut - 1) {
  2149. if (arr[j + 1] === void 0) arr[j + 1] = 0;
  2150. arr[j + 1] += arr[j] / baseOut | 0;
  2151. arr[j] %= baseOut;
  2152. }
  2153. }
  2154. }
  2155. return arr.reverse();
  2156. }
  2157. /*
  2158. * cos(x) = 1 - x^2/2! + x^4/4! - ...
  2159. * |x| < pi/2
  2160. *
  2161. */
  2162. function cosine(Ctor, x) {
  2163. var k, len, y;
  2164. if (x.isZero()) return x;
  2165. // Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1
  2166. // i.e. cos(x) = 8*(cos^4(x/4) - cos^2(x/4)) + 1
  2167. // Estimate the optimum number of times to use the argument reduction.
  2168. len = x.d.length;
  2169. if (len < 32) {
  2170. k = Math.ceil(len / 3);
  2171. y = (1 / tinyPow(4, k)).toString();
  2172. } else {
  2173. k = 16;
  2174. y = '2.3283064365386962890625e-10';
  2175. }
  2176. Ctor.precision += k;
  2177. x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1));
  2178. // Reverse argument reduction
  2179. for (var i = k; i--;) {
  2180. var cos2x = x.times(x);
  2181. x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1);
  2182. }
  2183. Ctor.precision -= k;
  2184. return x;
  2185. }
  2186. /*
  2187. * Perform division in the specified base.
  2188. */
  2189. var divide = (function () {
  2190. // Assumes non-zero x and k, and hence non-zero result.
  2191. function multiplyInteger(x, k, base) {
  2192. var temp,
  2193. carry = 0,
  2194. i = x.length;
  2195. for (x = x.slice(); i--;) {
  2196. temp = x[i] * k + carry;
  2197. x[i] = temp % base | 0;
  2198. carry = temp / base | 0;
  2199. }
  2200. if (carry) x.unshift(carry);
  2201. return x;
  2202. }
  2203. function compare(a, b, aL, bL) {
  2204. var i, r;
  2205. if (aL != bL) {
  2206. r = aL > bL ? 1 : -1;
  2207. } else {
  2208. for (i = r = 0; i < aL; i++) {
  2209. if (a[i] != b[i]) {
  2210. r = a[i] > b[i] ? 1 : -1;
  2211. break;
  2212. }
  2213. }
  2214. }
  2215. return r;
  2216. }
  2217. function subtract(a, b, aL, base) {
  2218. var i = 0;
  2219. // Subtract b from a.
  2220. for (; aL--;) {
  2221. a[aL] -= i;
  2222. i = a[aL] < b[aL] ? 1 : 0;
  2223. a[aL] = i * base + a[aL] - b[aL];
  2224. }
  2225. // Remove leading zeros.
  2226. for (; !a[0] && a.length > 1;) a.shift();
  2227. }
  2228. return function (x, y, pr, rm, dp, base) {
  2229. var cmp, e, i, k, logBase, more, prod, prodL, q, qd, rem, remL, rem0, sd, t, xi, xL, yd0,
  2230. yL, yz,
  2231. Ctor = x.constructor,
  2232. sign = x.s == y.s ? 1 : -1,
  2233. xd = x.d,
  2234. yd = y.d;
  2235. // Either NaN, Infinity or 0?
  2236. if (!xd || !xd[0] || !yd || !yd[0]) {
  2237. return new Ctor(// Return NaN if either NaN, or both Infinity or 0.
  2238. !x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN :
  2239. // Return ±0 if x is 0 or y is ±Infinity, or return ±Infinity as y is 0.
  2240. xd && xd[0] == 0 || !yd ? sign * 0 : sign / 0);
  2241. }
  2242. if (base) {
  2243. logBase = 1;
  2244. e = x.e - y.e;
  2245. } else {
  2246. base = BASE;
  2247. logBase = LOG_BASE;
  2248. e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase);
  2249. }
  2250. yL = yd.length;
  2251. xL = xd.length;
  2252. q = new Ctor(sign);
  2253. qd = q.d = [];
  2254. // Result exponent may be one less than e.
  2255. // The digit array of a Decimal from toStringBinary may have trailing zeros.
  2256. for (i = 0; yd[i] == (xd[i] || 0); i++);
  2257. if (yd[i] > (xd[i] || 0)) e--;
  2258. if (pr == null) {
  2259. sd = pr = Ctor.precision;
  2260. rm = Ctor.rounding;
  2261. } else if (dp) {
  2262. sd = pr + (x.e - y.e) + 1;
  2263. } else {
  2264. sd = pr;
  2265. }
  2266. if (sd < 0) {
  2267. qd.push(1);
  2268. more = true;
  2269. } else {
  2270. // Convert precision in number of base 10 digits to base 1e7 digits.
  2271. sd = sd / logBase + 2 | 0;
  2272. i = 0;
  2273. // divisor < 1e7
  2274. if (yL == 1) {
  2275. k = 0;
  2276. yd = yd[0];
  2277. sd++;
  2278. // k is the carry.
  2279. for (; (i < xL || k) && sd--; i++) {
  2280. t = k * base + (xd[i] || 0);
  2281. qd[i] = t / yd | 0;
  2282. k = t % yd | 0;
  2283. }
  2284. more = k || i < xL;
  2285. // divisor >= 1e7
  2286. } else {
  2287. // Normalise xd and yd so highest order digit of yd is >= base/2
  2288. k = base / (yd[0] + 1) | 0;
  2289. if (k > 1) {
  2290. yd = multiplyInteger(yd, k, base);
  2291. xd = multiplyInteger(xd, k, base);
  2292. yL = yd.length;
  2293. xL = xd.length;
  2294. }
  2295. xi = yL;
  2296. rem = xd.slice(0, yL);
  2297. remL = rem.length;
  2298. // Add zeros to make remainder as long as divisor.
  2299. for (; remL < yL;) rem[remL++] = 0;
  2300. yz = yd.slice();
  2301. yz.unshift(0);
  2302. yd0 = yd[0];
  2303. if (yd[1] >= base / 2) ++yd0;
  2304. do {
  2305. k = 0;
  2306. // Compare divisor and remainder.
  2307. cmp = compare(yd, rem, yL, remL);
  2308. // If divisor < remainder.
  2309. if (cmp < 0) {
  2310. // Calculate trial digit, k.
  2311. rem0 = rem[0];
  2312. if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
  2313. // k will be how many times the divisor goes into the current remainder.
  2314. k = rem0 / yd0 | 0;
  2315. // Algorithm:
  2316. // 1. product = divisor * trial digit (k)
  2317. // 2. if product > remainder: product -= divisor, k--
  2318. // 3. remainder -= product
  2319. // 4. if product was < remainder at 2:
  2320. // 5. compare new remainder and divisor
  2321. // 6. If remainder > divisor: remainder -= divisor, k++
  2322. if (k > 1) {
  2323. if (k >= base) k = base - 1;
  2324. // product = divisor * trial digit.
  2325. prod = multiplyInteger(yd, k, base);
  2326. prodL = prod.length;
  2327. remL = rem.length;
  2328. // Compare product and remainder.
  2329. cmp = compare(prod, rem, prodL, remL);
  2330. // product > remainder.
  2331. if (cmp == 1) {
  2332. k--;
  2333. // Subtract divisor from product.
  2334. subtract(prod, yL < prodL ? yz : yd, prodL, base);
  2335. }
  2336. } else {
  2337. // cmp is -1.
  2338. // If k is 0, there is no need to compare yd and rem again below, so change cmp to 1
  2339. // to avoid it. If k is 1 there is a need to compare yd and rem again below.
  2340. if (k == 0) cmp = k = 1;
  2341. prod = yd.slice();
  2342. }
  2343. prodL = prod.length;
  2344. if (prodL < remL) prod.unshift(0);
  2345. // Subtract product from remainder.
  2346. subtract(rem, prod, remL, base);
  2347. // If product was < previous remainder.
  2348. if (cmp == -1) {
  2349. remL = rem.length;
  2350. // Compare divisor and new remainder.
  2351. cmp = compare(yd, rem, yL, remL);
  2352. // If divisor < new remainder, subtract divisor from remainder.
  2353. if (cmp < 1) {
  2354. k++;
  2355. // Subtract divisor from remainder.
  2356. subtract(rem, yL < remL ? yz : yd, remL, base);
  2357. }
  2358. }
  2359. remL = rem.length;
  2360. } else if (cmp === 0) {
  2361. k++;
  2362. rem = [0];
  2363. } // if cmp === 1, k will be 0
  2364. // Add the next digit, k, to the result array.
  2365. qd[i++] = k;
  2366. // Update the remainder.
  2367. if (cmp && rem[0]) {
  2368. rem[remL++] = xd[xi] || 0;
  2369. } else {
  2370. rem = [xd[xi]];
  2371. remL = 1;
  2372. }
  2373. } while ((xi++ < xL || rem[0] !== void 0) && sd--);
  2374. more = rem[0] !== void 0;
  2375. }
  2376. // Leading zero?
  2377. if (!qd[0]) qd.shift();
  2378. }
  2379. // logBase is 1 when divide is being used for base conversion.
  2380. if (logBase == 1) {
  2381. q.e = e;
  2382. inexact = more;
  2383. } else {
  2384. // To calculate q.e, first get the number of digits of qd[0].
  2385. for (i = 1, k = qd[0]; k >= 10; k /= 10) i++;
  2386. q.e = i + e * logBase - 1;
  2387. finalise(q, dp ? pr + q.e + 1 : pr, rm, more);
  2388. }
  2389. return q;
  2390. };
  2391. })();
  2392. /*
  2393. * Round `x` to `sd` significant digits using rounding mode `rm`.
  2394. * Check for over/under-flow.
  2395. */
  2396. function finalise(x, sd, rm, isTruncated) {
  2397. var digits, i, j, k, rd, roundUp, w, xd, xdi,
  2398. Ctor = x.constructor;
  2399. // Don't round if sd is null or undefined.
  2400. out: if (sd != null) {
  2401. xd = x.d;
  2402. // Infinity/NaN.
  2403. if (!xd) return x;
  2404. // rd: the rounding digit, i.e. the digit after the digit that may be rounded up.
  2405. // w: the word of xd containing rd, a base 1e7 number.
  2406. // xdi: the index of w within xd.
  2407. // digits: the number of digits of w.
  2408. // i: what would be the index of rd within w if all the numbers were 7 digits long (i.e. if
  2409. // they had leading zeros)
  2410. // j: if > 0, the actual index of rd within w (if < 0, rd is a leading zero).
  2411. // Get the length of the first word of the digits array xd.
  2412. for (digits = 1, k = xd[0]; k >= 10; k /= 10) digits++;
  2413. i = sd - digits;
  2414. // Is the rounding digit in the first word of xd?
  2415. if (i < 0) {
  2416. i += LOG_BASE;
  2417. j = sd;
  2418. w = xd[xdi = 0];
  2419. // Get the rounding digit at index j of w.
  2420. rd = w / mathpow(10, digits - j - 1) % 10 | 0;
  2421. } else {
  2422. xdi = Math.ceil((i + 1) / LOG_BASE);
  2423. k = xd.length;
  2424. if (xdi >= k) {
  2425. if (isTruncated) {
  2426. // Needed by `naturalExponential`, `naturalLogarithm` and `squareRoot`.
  2427. for (; k++ <= xdi;) xd.push(0);
  2428. w = rd = 0;
  2429. digits = 1;
  2430. i %= LOG_BASE;
  2431. j = i - LOG_BASE + 1;
  2432. } else {
  2433. break out;
  2434. }
  2435. } else {
  2436. w = k = xd[xdi];
  2437. // Get the number of digits of w.
  2438. for (digits = 1; k >= 10; k /= 10) digits++;
  2439. // Get the index of rd within w.
  2440. i %= LOG_BASE;
  2441. // Get the index of rd within w, adjusted for leading zeros.
  2442. // The number of leading zeros of w is given by LOG_BASE - digits.
  2443. j = i - LOG_BASE + digits;
  2444. // Get the rounding digit at index j of w.
  2445. rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0;
  2446. }
  2447. }
  2448. // Are there any non-zero digits after the rounding digit?
  2449. isTruncated = isTruncated || sd < 0 ||
  2450. xd[xdi + 1] !== void 0 || (j < 0 ? w : w % mathpow(10, digits - j - 1));
  2451. // The expression `w % mathpow(10, digits - j - 1)` returns all the digits of w to the right
  2452. // of the digit at (left-to-right) index j, e.g. if w is 908714 and j is 2, the expression
  2453. // will give 714.
  2454. roundUp = rm < 4
  2455. ? (rd || isTruncated) && (rm == 0 || rm == (x.s < 0 ? 3 : 2))
  2456. : rd > 5 || rd == 5 && (rm == 4 || isTruncated || rm == 6 &&
  2457. // Check whether the digit to the left of the rounding digit is odd.
  2458. ((i > 0 ? j > 0 ? w / mathpow(10, digits - j) : 0 : xd[xdi - 1]) % 10) & 1 ||
  2459. rm == (x.s < 0 ? 8 : 7));
  2460. if (sd < 1 || !xd[0]) {
  2461. xd.length = 0;
  2462. if (roundUp) {
  2463. // Convert sd to decimal places.
  2464. sd -= x.e + 1;
  2465. // 1, 0.1, 0.01, 0.001, 0.0001 etc.
  2466. xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE);
  2467. x.e = -sd || 0;
  2468. } else {
  2469. // Zero.
  2470. xd[0] = x.e = 0;
  2471. }
  2472. return x;
  2473. }
  2474. // Remove excess digits.
  2475. if (i == 0) {
  2476. xd.length = xdi;
  2477. k = 1;
  2478. xdi--;
  2479. } else {
  2480. xd.length = xdi + 1;
  2481. k = mathpow(10, LOG_BASE - i);
  2482. // E.g. 56700 becomes 56000 if 7 is the rounding digit.
  2483. // j > 0 means i > number of leading zeros of w.
  2484. xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0;
  2485. }
  2486. if (roundUp) {
  2487. for (;;) {
  2488. // Is the digit to be rounded up in the first word of xd?
  2489. if (xdi == 0) {
  2490. // i will be the length of xd[0] before k is added.
  2491. for (i = 1, j = xd[0]; j >= 10; j /= 10) i++;
  2492. j = xd[0] += k;
  2493. for (k = 1; j >= 10; j /= 10) k++;
  2494. // if i != k the length has increased.
  2495. if (i != k) {
  2496. x.e++;
  2497. if (xd[0] == BASE) xd[0] = 1;
  2498. }
  2499. break;
  2500. } else {
  2501. xd[xdi] += k;
  2502. if (xd[xdi] != BASE) break;
  2503. xd[xdi--] = 0;
  2504. k = 1;
  2505. }
  2506. }
  2507. }
  2508. // Remove trailing zeros.
  2509. for (i = xd.length; xd[--i] === 0;) xd.pop();
  2510. }
  2511. if (external) {
  2512. // Overflow?
  2513. if (x.e > Ctor.maxE) {
  2514. // Infinity.
  2515. x.d = null;
  2516. x.e = NaN;
  2517. // Underflow?
  2518. } else if (x.e < Ctor.minE) {
  2519. // Zero.
  2520. x.e = 0;
  2521. x.d = [0];
  2522. // Ctor.underflow = true;
  2523. } // else Ctor.underflow = false;
  2524. }
  2525. return x;
  2526. }
  2527. function finiteToString(x, isExp, sd) {
  2528. if (!x.isFinite()) return nonFiniteToString(x);
  2529. var k,
  2530. e = x.e,
  2531. str = digitsToString(x.d),
  2532. len = str.length;
  2533. if (isExp) {
  2534. if (sd && (k = sd - len) > 0) {
  2535. str = str.charAt(0) + '.' + str.slice(1) + getZeroString(k);
  2536. } else if (len > 1) {
  2537. str = str.charAt(0) + '.' + str.slice(1);
  2538. }
  2539. str = str + (x.e < 0 ? 'e' : 'e+') + x.e;
  2540. } else if (e < 0) {
  2541. str = '0.' + getZeroString(-e - 1) + str;
  2542. if (sd && (k = sd - len) > 0) str += getZeroString(k);
  2543. } else if (e >= len) {
  2544. str += getZeroString(e + 1 - len);
  2545. if (sd && (k = sd - e - 1) > 0) str = str + '.' + getZeroString(k);
  2546. } else {
  2547. if ((k = e + 1) < len) str = str.slice(0, k) + '.' + str.slice(k);
  2548. if (sd && (k = sd - len) > 0) {
  2549. if (e + 1 === len) str += '.';
  2550. str += getZeroString(k);
  2551. }
  2552. }
  2553. return str;
  2554. }
  2555. // Calculate the base 10 exponent from the base 1e7 exponent.
  2556. function getBase10Exponent(digits, e) {
  2557. var w = digits[0];
  2558. // Add the number of digits of the first word of the digits array.
  2559. for ( e *= LOG_BASE; w >= 10; w /= 10) e++;
  2560. return e;
  2561. }
  2562. function getLn10(Ctor, sd, pr) {
  2563. if (sd > LN10_PRECISION) {
  2564. // Reset global state in case the exception is caught.
  2565. external = true;
  2566. if (pr) Ctor.precision = pr;
  2567. throw Error(precisionLimitExceeded);
  2568. }
  2569. return finalise(new Ctor(LN10), sd, 1, true);
  2570. }
  2571. function getPi(Ctor, sd, rm) {
  2572. if (sd > PI_PRECISION) throw Error(precisionLimitExceeded);
  2573. return finalise(new Ctor(PI), sd, rm, true);
  2574. }
  2575. function getPrecision(digits) {
  2576. var w = digits.length - 1,
  2577. len = w * LOG_BASE + 1;
  2578. w = digits[w];
  2579. // If non-zero...
  2580. if (w) {
  2581. // Subtract the number of trailing zeros of the last word.
  2582. for (; w % 10 == 0; w /= 10) len--;
  2583. // Add the number of digits of the first word.
  2584. for (w = digits[0]; w >= 10; w /= 10) len++;
  2585. }
  2586. return len;
  2587. }
  2588. function getZeroString(k) {
  2589. var zs = '';
  2590. for (; k--;) zs += '0';
  2591. return zs;
  2592. }
  2593. /*
  2594. * Return a new Decimal whose value is the value of Decimal `x` to the power `n`, where `n` is an
  2595. * integer of type number.
  2596. *
  2597. * Implements 'exponentiation by squaring'. Called by `pow` and `parseOther`.
  2598. *
  2599. */
  2600. function intPow(Ctor, x, n, pr) {
  2601. var isTruncated,
  2602. r = new Ctor(1),
  2603. // Max n of 9007199254740991 takes 53 loop iterations.
  2604. // Maximum digits array length; leaves [28, 34] guard digits.
  2605. k = Math.ceil(pr / LOG_BASE + 4);
  2606. external = false;
  2607. for (;;) {
  2608. if (n % 2) {
  2609. r = r.times(x);
  2610. if (truncate(r.d, k)) isTruncated = true;
  2611. }
  2612. n = mathfloor(n / 2);
  2613. if (n === 0) {
  2614. // To ensure correct rounding when r.d is truncated, increment the last word if it is zero.
  2615. n = r.d.length - 1;
  2616. if (isTruncated && r.d[n] === 0) ++r.d[n];
  2617. break;
  2618. }
  2619. x = x.times(x);
  2620. truncate(x.d, k);
  2621. }
  2622. external = true;
  2623. return r;
  2624. }
  2625. function isOdd(n) {
  2626. return n.d[n.d.length - 1] & 1;
  2627. }
  2628. /*
  2629. * Handle `max` and `min`. `ltgt` is 'lt' or 'gt'.
  2630. */
  2631. function maxOrMin(Ctor, args, ltgt) {
  2632. var y,
  2633. x = new Ctor(args[0]),
  2634. i = 0;
  2635. for (; ++i < args.length;) {
  2636. y = new Ctor(args[i]);
  2637. if (!y.s) {
  2638. x = y;
  2639. break;
  2640. } else if (x[ltgt](y)) {
  2641. x = y;
  2642. }
  2643. }
  2644. return x;
  2645. }
  2646. /*
  2647. * Return a new Decimal whose value is the natural exponential of `x` rounded to `sd` significant
  2648. * digits.
  2649. *
  2650. * Taylor/Maclaurin series.
  2651. *
  2652. * exp(x) = x^0/0! + x^1/1! + x^2/2! + x^3/3! + ...
  2653. *
  2654. * Argument reduction:
  2655. * Repeat x = x / 32, k += 5, until |x| < 0.1
  2656. * exp(x) = exp(x / 2^k)^(2^k)
  2657. *
  2658. * Previously, the argument was initially reduced by
  2659. * exp(x) = exp(r) * 10^k where r = x - k * ln10, k = floor(x / ln10)
  2660. * to first put r in the range [0, ln10], before dividing by 32 until |x| < 0.1, but this was
  2661. * found to be slower than just dividing repeatedly by 32 as above.
  2662. *
  2663. * Max integer argument: exp('20723265836946413') = 6.3e+9000000000000000
  2664. * Min integer argument: exp('-20723265836946411') = 1.2e-9000000000000000
  2665. * (Math object integer min/max: Math.exp(709) = 8.2e+307, Math.exp(-745) = 5e-324)
  2666. *
  2667. * exp(Infinity) = Infinity
  2668. * exp(-Infinity) = 0
  2669. * exp(NaN) = NaN
  2670. * exp(±0) = 1
  2671. *
  2672. * exp(x) is non-terminating for any finite, non-zero x.
  2673. *
  2674. * The result will always be correctly rounded.
  2675. *
  2676. */
  2677. function naturalExponential(x, sd) {
  2678. var denominator, guard, j, pow, sum, t, wpr,
  2679. rep = 0,
  2680. i = 0,
  2681. k = 0,
  2682. Ctor = x.constructor,
  2683. rm = Ctor.rounding,
  2684. pr = Ctor.precision;
  2685. // 0/NaN/Infinity?
  2686. if (!x.d || !x.d[0] || x.e > 17) {
  2687. return new Ctor(x.d
  2688. ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0
  2689. : x.s ? x.s < 0 ? 0 : x : 0 / 0);
  2690. }
  2691. if (sd == null) {
  2692. external = false;
  2693. wpr = pr;
  2694. } else {
  2695. wpr = sd;
  2696. }
  2697. t = new Ctor(0.03125);
  2698. // while abs(x) >= 0.1
  2699. while (x.e > -2) {
  2700. // x = x / 2^5
  2701. x = x.times(t);
  2702. k += 5;
  2703. }
  2704. // Use 2 * log10(2^k) + 5 (empirically derived) to estimate the increase in precision
  2705. // necessary to ensure the first 4 rounding digits are correct.
  2706. guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
  2707. wpr += guard;
  2708. denominator = pow = sum = new Ctor(1);
  2709. Ctor.precision = wpr;
  2710. for (;;) {
  2711. pow = finalise(pow.times(x), wpr, 1);
  2712. denominator = denominator.times(++i);
  2713. t = sum.plus(divide(pow, denominator, wpr, 1));
  2714. if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
  2715. j = k;
  2716. while (j--) sum = finalise(sum.times(sum), wpr, 1);
  2717. // Check to see if the first 4 rounding digits are [49]999.
  2718. // If so, repeat the summation with a higher precision, otherwise
  2719. // e.g. with precision: 18, rounding: 1
  2720. // exp(18.404272462595034083567793919843761) = 98372560.1229999999 (should be 98372560.123)
  2721. // `wpr - guard` is the index of first rounding digit.
  2722. if (sd == null) {
  2723. if (rep < 3 && checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
  2724. Ctor.precision = wpr += 10;
  2725. denominator = pow = t = new Ctor(1);
  2726. i = 0;
  2727. rep++;
  2728. } else {
  2729. return finalise(sum, Ctor.precision = pr, rm, external = true);
  2730. }
  2731. } else {
  2732. Ctor.precision = pr;
  2733. return sum;
  2734. }
  2735. }
  2736. sum = t;
  2737. }
  2738. }
  2739. /*
  2740. * Return a new Decimal whose value is the natural logarithm of `x` rounded to `sd` significant
  2741. * digits.
  2742. *
  2743. * ln(-n) = NaN
  2744. * ln(0) = -Infinity
  2745. * ln(-0) = -Infinity
  2746. * ln(1) = 0
  2747. * ln(Infinity) = Infinity
  2748. * ln(-Infinity) = NaN
  2749. * ln(NaN) = NaN
  2750. *
  2751. * ln(n) (n != 1) is non-terminating.
  2752. *
  2753. */
  2754. function naturalLogarithm(y, sd) {
  2755. var c, c0, denominator, e, numerator, rep, sum, t, wpr, x1, x2,
  2756. n = 1,
  2757. guard = 10,
  2758. x = y,
  2759. xd = x.d,
  2760. Ctor = x.constructor,
  2761. rm = Ctor.rounding,
  2762. pr = Ctor.precision;
  2763. // Is x negative or Infinity, NaN, 0 or 1?
  2764. if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) {
  2765. return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x);
  2766. }
  2767. if (sd == null) {
  2768. external = false;
  2769. wpr = pr;
  2770. } else {
  2771. wpr = sd;
  2772. }
  2773. Ctor.precision = wpr += guard;
  2774. c = digitsToString(xd);
  2775. c0 = c.charAt(0);
  2776. if (Math.abs(e = x.e) < 1.5e15) {
  2777. // Argument reduction.
  2778. // The series converges faster the closer the argument is to 1, so using
  2779. // ln(a^b) = b * ln(a), ln(a) = ln(a^b) / b
  2780. // multiply the argument by itself until the leading digits of the significand are 7, 8, 9,
  2781. // 10, 11, 12 or 13, recording the number of multiplications so the sum of the series can
  2782. // later be divided by this number, then separate out the power of 10 using
  2783. // ln(a*10^b) = ln(a) + b*ln(10).
  2784. // max n is 21 (gives 0.9, 1.0 or 1.1) (9e15 / 21 = 4.2e14).
  2785. //while (c0 < 9 && c0 != 1 || c0 == 1 && c.charAt(1) > 1) {
  2786. // max n is 6 (gives 0.7 - 1.3)
  2787. while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) {
  2788. x = x.times(y);
  2789. c = digitsToString(x.d);
  2790. c0 = c.charAt(0);
  2791. n++;
  2792. }
  2793. e = x.e;
  2794. if (c0 > 1) {
  2795. x = new Ctor('0.' + c);
  2796. e++;
  2797. } else {
  2798. x = new Ctor(c0 + '.' + c.slice(1));
  2799. }
  2800. } else {
  2801. // The argument reduction method above may result in overflow if the argument y is a massive
  2802. // number with exponent >= 1500000000000000 (9e15 / 6 = 1.5e15), so instead recall this
  2803. // function using ln(x*10^e) = ln(x) + e*ln(10).
  2804. t = getLn10(Ctor, wpr + 2, pr).times(e + '');
  2805. x = naturalLogarithm(new Ctor(c0 + '.' + c.slice(1)), wpr - guard).plus(t);
  2806. Ctor.precision = pr;
  2807. return sd == null ? finalise(x, pr, rm, external = true) : x;
  2808. }
  2809. // x1 is x reduced to a value near 1.
  2810. x1 = x;
  2811. // Taylor series.
  2812. // ln(y) = ln((1 + x)/(1 - x)) = 2(x + x^3/3 + x^5/5 + x^7/7 + ...)
  2813. // where x = (y - 1)/(y + 1) (|x| < 1)
  2814. sum = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1);
  2815. x2 = finalise(x.times(x), wpr, 1);
  2816. denominator = 3;
  2817. for (;;) {
  2818. numerator = finalise(numerator.times(x2), wpr, 1);
  2819. t = sum.plus(divide(numerator, new Ctor(denominator), wpr, 1));
  2820. if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
  2821. sum = sum.times(2);
  2822. // Reverse the argument reduction. Check that e is not 0 because, besides preventing an
  2823. // unnecessary calculation, -0 + 0 = +0 and to ensure correct rounding -0 needs to stay -0.
  2824. if (e !== 0) sum = sum.plus(getLn10(Ctor, wpr + 2, pr).times(e + ''));
  2825. sum = divide(sum, new Ctor(n), wpr, 1);
  2826. // Is rm > 3 and the first 4 rounding digits 4999, or rm < 4 (or the summation has
  2827. // been repeated previously) and the first 4 rounding digits 9999?
  2828. // If so, restart the summation with a higher precision, otherwise
  2829. // e.g. with precision: 12, rounding: 1
  2830. // ln(135520028.6126091714265381533) = 18.7246299999 when it should be 18.72463.
  2831. // `wpr - guard` is the index of first rounding digit.
  2832. if (sd == null) {
  2833. if (checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
  2834. Ctor.precision = wpr += guard;
  2835. t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1);
  2836. x2 = finalise(x.times(x), wpr, 1);
  2837. denominator = rep = 1;
  2838. } else {
  2839. return finalise(sum, Ctor.precision = pr, rm, external = true);
  2840. }
  2841. } else {
  2842. Ctor.precision = pr;
  2843. return sum;
  2844. }
  2845. }
  2846. sum = t;
  2847. denominator += 2;
  2848. }
  2849. }
  2850. // ±Infinity, NaN.
  2851. function nonFiniteToString(x) {
  2852. // Unsigned.
  2853. return String(x.s * x.s / 0);
  2854. }
  2855. /*
  2856. * Parse the value of a new Decimal `x` from string `str`.
  2857. */
  2858. function parseDecimal(x, str) {
  2859. var e, i, len;
  2860. // Decimal point?
  2861. if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');
  2862. // Exponential form?
  2863. if ((i = str.search(/e/i)) > 0) {
  2864. // Determine exponent.
  2865. if (e < 0) e = i;
  2866. e += +str.slice(i + 1);
  2867. str = str.substring(0, i);
  2868. } else if (e < 0) {
  2869. // Integer.
  2870. e = str.length;
  2871. }
  2872. // Determine leading zeros.
  2873. for (i = 0; str.charCodeAt(i) === 48; i++);
  2874. // Determine trailing zeros.
  2875. for (len = str.length; str.charCodeAt(len - 1) === 48; --len);
  2876. str = str.slice(i, len);
  2877. if (str) {
  2878. len -= i;
  2879. x.e = e = e - i - 1;
  2880. x.d = [];
  2881. // Transform base
  2882. // e is the base 10 exponent.
  2883. // i is where to slice str to get the first word of the digits array.
  2884. i = (e + 1) % LOG_BASE;
  2885. if (e < 0) i += LOG_BASE;
  2886. if (i < len) {
  2887. if (i) x.d.push(+str.slice(0, i));
  2888. for (len -= LOG_BASE; i < len;) x.d.push(+str.slice(i, i += LOG_BASE));
  2889. str = str.slice(i);
  2890. i = LOG_BASE - str.length;
  2891. } else {
  2892. i -= len;
  2893. }
  2894. for (; i--;) str += '0';
  2895. x.d.push(+str);
  2896. if (external) {
  2897. // Overflow?
  2898. if (x.e > x.constructor.maxE) {
  2899. // Infinity.
  2900. x.d = null;
  2901. x.e = NaN;
  2902. // Underflow?
  2903. } else if (x.e < x.constructor.minE) {
  2904. // Zero.
  2905. x.e = 0;
  2906. x.d = [0];
  2907. // x.constructor.underflow = true;
  2908. } // else x.constructor.underflow = false;
  2909. }
  2910. } else {
  2911. // Zero.
  2912. x.e = 0;
  2913. x.d = [0];
  2914. }
  2915. return x;
  2916. }
  2917. /*
  2918. * Parse the value of a new Decimal `x` from a string `str`, which is not a decimal value.
  2919. */
  2920. function parseOther(x, str) {
  2921. var base, Ctor, divisor, i, isFloat, len, p, xd, xe;
  2922. if (str.indexOf('_') > -1) {
  2923. str = str.replace(/(\d)_(?=\d)/g, '$1');
  2924. if (isDecimal.test(str)) return parseDecimal(x, str);
  2925. } else if (str === 'Infinity' || str === 'NaN') {
  2926. if (!+str) x.s = NaN;
  2927. x.e = NaN;
  2928. x.d = null;
  2929. return x;
  2930. }
  2931. if (isHex.test(str)) {
  2932. base = 16;
  2933. str = str.toLowerCase();
  2934. } else if (isBinary.test(str)) {
  2935. base = 2;
  2936. } else if (isOctal.test(str)) {
  2937. base = 8;
  2938. } else {
  2939. throw Error(invalidArgument + str);
  2940. }
  2941. // Is there a binary exponent part?
  2942. i = str.search(/p/i);
  2943. if (i > 0) {
  2944. p = +str.slice(i + 1);
  2945. str = str.substring(2, i);
  2946. } else {
  2947. str = str.slice(2);
  2948. }
  2949. // Convert `str` as an integer then divide the result by `base` raised to a power such that the
  2950. // fraction part will be restored.
  2951. i = str.indexOf('.');
  2952. isFloat = i >= 0;
  2953. Ctor = x.constructor;
  2954. if (isFloat) {
  2955. str = str.replace('.', '');
  2956. len = str.length;
  2957. i = len - i;
  2958. // log[10](16) = 1.2041... , log[10](88) = 1.9444....
  2959. divisor = intPow(Ctor, new Ctor(base), i, i * 2);
  2960. }
  2961. xd = convertBase(str, base, BASE);
  2962. xe = xd.length - 1;
  2963. // Remove trailing zeros.
  2964. for (i = xe; xd[i] === 0; --i) xd.pop();
  2965. if (i < 0) return new Ctor(x.s * 0);
  2966. x.e = getBase10Exponent(xd, xe);
  2967. x.d = xd;
  2968. external = false;
  2969. // At what precision to perform the division to ensure exact conversion?
  2970. // maxDecimalIntegerPartDigitCount = ceil(log[10](b) * otherBaseIntegerPartDigitCount)
  2971. // log[10](2) = 0.30103, log[10](8) = 0.90309, log[10](16) = 1.20412
  2972. // E.g. ceil(1.2 * 3) = 4, so up to 4 decimal digits are needed to represent 3 hex int digits.
  2973. // maxDecimalFractionPartDigitCount = {Hex:4|Oct:3|Bin:1} * otherBaseFractionPartDigitCount
  2974. // Therefore using 4 * the number of digits of str will always be enough.
  2975. if (isFloat) x = divide(x, divisor, len * 4);
  2976. // Multiply by the binary exponent part if present.
  2977. if (p) x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
  2978. external = true;
  2979. return x;
  2980. }
  2981. /*
  2982. * sin(x) = x - x^3/3! + x^5/5! - ...
  2983. * |x| < pi/2
  2984. *
  2985. */
  2986. function sine(Ctor, x) {
  2987. var k,
  2988. len = x.d.length;
  2989. if (len < 3) {
  2990. return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
  2991. }
  2992. // Argument reduction: sin(5x) = 16*sin^5(x) - 20*sin^3(x) + 5*sin(x)
  2993. // i.e. sin(x) = 16*sin^5(x/5) - 20*sin^3(x/5) + 5*sin(x/5)
  2994. // and sin(x) = sin(x/5)(5 + sin^2(x/5)(16sin^2(x/5) - 20))
  2995. // Estimate the optimum number of times to use the argument reduction.
  2996. k = 1.4 * Math.sqrt(len);
  2997. k = k > 16 ? 16 : k | 0;
  2998. x = x.times(1 / tinyPow(5, k));
  2999. x = taylorSeries(Ctor, 2, x, x);
  3000. // Reverse argument reduction
  3001. var sin2_x,
  3002. d5 = new Ctor(5),
  3003. d16 = new Ctor(16),
  3004. d20 = new Ctor(20);
  3005. for (; k--;) {
  3006. sin2_x = x.times(x);
  3007. x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20))));
  3008. }
  3009. return x;
  3010. }
  3011. // Calculate Taylor series for `cos`, `cosh`, `sin` and `sinh`.
  3012. function taylorSeries(Ctor, n, x, y, isHyperbolic) {
  3013. var j, t, u, x2,
  3014. i = 1,
  3015. pr = Ctor.precision,
  3016. k = Math.ceil(pr / LOG_BASE);
  3017. external = false;
  3018. x2 = x.times(x);
  3019. u = new Ctor(y);
  3020. for (;;) {
  3021. t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1);
  3022. u = isHyperbolic ? y.plus(t) : y.minus(t);
  3023. y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1);
  3024. t = u.plus(y);
  3025. if (t.d[k] !== void 0) {
  3026. for (j = k; t.d[j] === u.d[j] && j--;);
  3027. if (j == -1) break;
  3028. }
  3029. j = u;
  3030. u = y;
  3031. y = t;
  3032. t = j;
  3033. i++;
  3034. }
  3035. external = true;
  3036. t.d.length = k + 1;
  3037. return t;
  3038. }
  3039. // Exponent e must be positive and non-zero.
  3040. function tinyPow(b, e) {
  3041. var n = b;
  3042. while (--e) n *= b;
  3043. return n;
  3044. }
  3045. // Return the absolute value of `x` reduced to less than or equal to half pi.
  3046. function toLessThanHalfPi(Ctor, x) {
  3047. var t,
  3048. isNeg = x.s < 0,
  3049. pi = getPi(Ctor, Ctor.precision, 1),
  3050. halfPi = pi.times(0.5);
  3051. x = x.abs();
  3052. if (x.lte(halfPi)) {
  3053. quadrant = isNeg ? 4 : 1;
  3054. return x;
  3055. }
  3056. t = x.divToInt(pi);
  3057. if (t.isZero()) {
  3058. quadrant = isNeg ? 3 : 2;
  3059. } else {
  3060. x = x.minus(t.times(pi));
  3061. // 0 <= x < pi
  3062. if (x.lte(halfPi)) {
  3063. quadrant = isOdd(t) ? (isNeg ? 2 : 3) : (isNeg ? 4 : 1);
  3064. return x;
  3065. }
  3066. quadrant = isOdd(t) ? (isNeg ? 1 : 4) : (isNeg ? 3 : 2);
  3067. }
  3068. return x.minus(pi).abs();
  3069. }
  3070. /*
  3071. * Return the value of Decimal `x` as a string in base `baseOut`.
  3072. *
  3073. * If the optional `sd` argument is present include a binary exponent suffix.
  3074. */
  3075. function toStringBinary(x, baseOut, sd, rm) {
  3076. var base, e, i, k, len, roundUp, str, xd, y,
  3077. Ctor = x.constructor,
  3078. isExp = sd !== void 0;
  3079. if (isExp) {
  3080. checkInt32(sd, 1, MAX_DIGITS);
  3081. if (rm === void 0) rm = Ctor.rounding;
  3082. else checkInt32(rm, 0, 8);
  3083. } else {
  3084. sd = Ctor.precision;
  3085. rm = Ctor.rounding;
  3086. }
  3087. if (!x.isFinite()) {
  3088. str = nonFiniteToString(x);
  3089. } else {
  3090. str = finiteToString(x);
  3091. i = str.indexOf('.');
  3092. // Use exponential notation according to `toExpPos` and `toExpNeg`? No, but if required:
  3093. // maxBinaryExponent = floor((decimalExponent + 1) * log[2](10))
  3094. // minBinaryExponent = floor(decimalExponent * log[2](10))
  3095. // log[2](10) = 3.321928094887362347870319429489390175864
  3096. if (isExp) {
  3097. base = 2;
  3098. if (baseOut == 16) {
  3099. sd = sd * 4 - 3;
  3100. } else if (baseOut == 8) {
  3101. sd = sd * 3 - 2;
  3102. }
  3103. } else {
  3104. base = baseOut;
  3105. }
  3106. // Convert the number as an integer then divide the result by its base raised to a power such
  3107. // that the fraction part will be restored.
  3108. // Non-integer.
  3109. if (i >= 0) {
  3110. str = str.replace('.', '');
  3111. y = new Ctor(1);
  3112. y.e = str.length - i;
  3113. y.d = convertBase(finiteToString(y), 10, base);
  3114. y.e = y.d.length;
  3115. }
  3116. xd = convertBase(str, 10, base);
  3117. e = len = xd.length;
  3118. // Remove trailing zeros.
  3119. for (; xd[--len] == 0;) xd.pop();
  3120. if (!xd[0]) {
  3121. str = isExp ? '0p+0' : '0';
  3122. } else {
  3123. if (i < 0) {
  3124. e--;
  3125. } else {
  3126. x = new Ctor(x);
  3127. x.d = xd;
  3128. x.e = e;
  3129. x = divide(x, y, sd, rm, 0, base);
  3130. xd = x.d;
  3131. e = x.e;
  3132. roundUp = inexact;
  3133. }
  3134. // The rounding digit, i.e. the digit after the digit that may be rounded up.
  3135. i = xd[sd];
  3136. k = base / 2;
  3137. roundUp = roundUp || xd[sd + 1] !== void 0;
  3138. roundUp = rm < 4
  3139. ? (i !== void 0 || roundUp) && (rm === 0 || rm === (x.s < 0 ? 3 : 2))
  3140. : i > k || i === k && (rm === 4 || roundUp || rm === 6 && xd[sd - 1] & 1 ||
  3141. rm === (x.s < 0 ? 8 : 7));
  3142. xd.length = sd;
  3143. if (roundUp) {
  3144. // Rounding up may mean the previous digit has to be rounded up and so on.
  3145. for (; ++xd[--sd] > base - 1;) {
  3146. xd[sd] = 0;
  3147. if (!sd) {
  3148. ++e;
  3149. xd.unshift(1);
  3150. }
  3151. }
  3152. }
  3153. // Determine trailing zeros.
  3154. for (len = xd.length; !xd[len - 1]; --len);
  3155. // E.g. [4, 11, 15] becomes 4bf.
  3156. for (i = 0, str = ''; i < len; i++) str += NUMERALS.charAt(xd[i]);
  3157. // Add binary exponent suffix?
  3158. if (isExp) {
  3159. if (len > 1) {
  3160. if (baseOut == 16 || baseOut == 8) {
  3161. i = baseOut == 16 ? 4 : 3;
  3162. for (--len; len % i; len++) str += '0';
  3163. xd = convertBase(str, base, baseOut);
  3164. for (len = xd.length; !xd[len - 1]; --len);
  3165. // xd[0] will always be be 1
  3166. for (i = 1, str = '1.'; i < len; i++) str += NUMERALS.charAt(xd[i]);
  3167. } else {
  3168. str = str.charAt(0) + '.' + str.slice(1);
  3169. }
  3170. }
  3171. str = str + (e < 0 ? 'p' : 'p+') + e;
  3172. } else if (e < 0) {
  3173. for (; ++e;) str = '0' + str;
  3174. str = '0.' + str;
  3175. } else {
  3176. if (++e > len) for (e -= len; e-- ;) str += '0';
  3177. else if (e < len) str = str.slice(0, e) + '.' + str.slice(e);
  3178. }
  3179. }
  3180. str = (baseOut == 16 ? '0x' : baseOut == 2 ? '0b' : baseOut == 8 ? '0o' : '') + str;
  3181. }
  3182. return x.s < 0 ? '-' + str : str;
  3183. }
  3184. // Does not strip trailing zeros.
  3185. function truncate(arr, len) {
  3186. if (arr.length > len) {
  3187. arr.length = len;
  3188. return true;
  3189. }
  3190. }
  3191. // Decimal methods
  3192. /*
  3193. * abs
  3194. * acos
  3195. * acosh
  3196. * add
  3197. * asin
  3198. * asinh
  3199. * atan
  3200. * atanh
  3201. * atan2
  3202. * cbrt
  3203. * ceil
  3204. * clamp
  3205. * clone
  3206. * config
  3207. * cos
  3208. * cosh
  3209. * div
  3210. * exp
  3211. * floor
  3212. * hypot
  3213. * ln
  3214. * log
  3215. * log2
  3216. * log10
  3217. * max
  3218. * min
  3219. * mod
  3220. * mul
  3221. * pow
  3222. * random
  3223. * round
  3224. * set
  3225. * sign
  3226. * sin
  3227. * sinh
  3228. * sqrt
  3229. * sub
  3230. * sum
  3231. * tan
  3232. * tanh
  3233. * trunc
  3234. */
  3235. /*
  3236. * Return a new Decimal whose value is the absolute value of `x`.
  3237. *
  3238. * x {number|string|Decimal}
  3239. *
  3240. */
  3241. function abs(x) {
  3242. return new this(x).abs();
  3243. }
  3244. /*
  3245. * Return a new Decimal whose value is the arccosine in radians of `x`.
  3246. *
  3247. * x {number|string|Decimal}
  3248. *
  3249. */
  3250. function acos(x) {
  3251. return new this(x).acos();
  3252. }
  3253. /*
  3254. * Return a new Decimal whose value is the inverse of the hyperbolic cosine of `x`, rounded to
  3255. * `precision` significant digits using rounding mode `rounding`.
  3256. *
  3257. * x {number|string|Decimal} A value in radians.
  3258. *
  3259. */
  3260. function acosh(x) {
  3261. return new this(x).acosh();
  3262. }
  3263. /*
  3264. * Return a new Decimal whose value is the sum of `x` and `y`, rounded to `precision` significant
  3265. * digits using rounding mode `rounding`.
  3266. *
  3267. * x {number|string|Decimal}
  3268. * y {number|string|Decimal}
  3269. *
  3270. */
  3271. function add(x, y) {
  3272. return new this(x).plus(y);
  3273. }
  3274. /*
  3275. * Return a new Decimal whose value is the arcsine in radians of `x`, rounded to `precision`
  3276. * significant digits using rounding mode `rounding`.
  3277. *
  3278. * x {number|string|Decimal}
  3279. *
  3280. */
  3281. function asin(x) {
  3282. return new this(x).asin();
  3283. }
  3284. /*
  3285. * Return a new Decimal whose value is the inverse of the hyperbolic sine of `x`, rounded to
  3286. * `precision` significant digits using rounding mode `rounding`.
  3287. *
  3288. * x {number|string|Decimal} A value in radians.
  3289. *
  3290. */
  3291. function asinh(x) {
  3292. return new this(x).asinh();
  3293. }
  3294. /*
  3295. * Return a new Decimal whose value is the arctangent in radians of `x`, rounded to `precision`
  3296. * significant digits using rounding mode `rounding`.
  3297. *
  3298. * x {number|string|Decimal}
  3299. *
  3300. */
  3301. function atan(x) {
  3302. return new this(x).atan();
  3303. }
  3304. /*
  3305. * Return a new Decimal whose value is the inverse of the hyperbolic tangent of `x`, rounded to
  3306. * `precision` significant digits using rounding mode `rounding`.
  3307. *
  3308. * x {number|string|Decimal} A value in radians.
  3309. *
  3310. */
  3311. function atanh(x) {
  3312. return new this(x).atanh();
  3313. }
  3314. /*
  3315. * Return a new Decimal whose value is the arctangent in radians of `y/x` in the range -pi to pi
  3316. * (inclusive), rounded to `precision` significant digits using rounding mode `rounding`.
  3317. *
  3318. * Domain: [-Infinity, Infinity]
  3319. * Range: [-pi, pi]
  3320. *
  3321. * y {number|string|Decimal} The y-coordinate.
  3322. * x {number|string|Decimal} The x-coordinate.
  3323. *
  3324. * atan2(±0, -0) = ±pi
  3325. * atan2(±0, +0) = ±0
  3326. * atan2(±0, -x) = ±pi for x > 0
  3327. * atan2(±0, x) = ±0 for x > 0
  3328. * atan2(-y, ±0) = -pi/2 for y > 0
  3329. * atan2(y, ±0) = pi/2 for y > 0
  3330. * atan2(±y, -Infinity) = ±pi for finite y > 0
  3331. * atan2(±y, +Infinity) = ±0 for finite y > 0
  3332. * atan2(±Infinity, x) = ±pi/2 for finite x
  3333. * atan2(±Infinity, -Infinity) = ±3*pi/4
  3334. * atan2(±Infinity, +Infinity) = ±pi/4
  3335. * atan2(NaN, x) = NaN
  3336. * atan2(y, NaN) = NaN
  3337. *
  3338. */
  3339. function atan2(y, x) {
  3340. y = new this(y);
  3341. x = new this(x);
  3342. var r,
  3343. pr = this.precision,
  3344. rm = this.rounding,
  3345. wpr = pr + 4;
  3346. // Either NaN
  3347. if (!y.s || !x.s) {
  3348. r = new this(NaN);
  3349. // Both ±Infinity
  3350. } else if (!y.d && !x.d) {
  3351. r = getPi(this, wpr, 1).times(x.s > 0 ? 0.25 : 0.75);
  3352. r.s = y.s;
  3353. // x is ±Infinity or y is ±0
  3354. } else if (!x.d || y.isZero()) {
  3355. r = x.s < 0 ? getPi(this, pr, rm) : new this(0);
  3356. r.s = y.s;
  3357. // y is ±Infinity or x is ±0
  3358. } else if (!y.d || x.isZero()) {
  3359. r = getPi(this, wpr, 1).times(0.5);
  3360. r.s = y.s;
  3361. // Both non-zero and finite
  3362. } else if (x.s < 0) {
  3363. this.precision = wpr;
  3364. this.rounding = 1;
  3365. r = this.atan(divide(y, x, wpr, 1));
  3366. x = getPi(this, wpr, 1);
  3367. this.precision = pr;
  3368. this.rounding = rm;
  3369. r = y.s < 0 ? r.minus(x) : r.plus(x);
  3370. } else {
  3371. r = this.atan(divide(y, x, wpr, 1));
  3372. }
  3373. return r;
  3374. }
  3375. /*
  3376. * Return a new Decimal whose value is the cube root of `x`, rounded to `precision` significant
  3377. * digits using rounding mode `rounding`.
  3378. *
  3379. * x {number|string|Decimal}
  3380. *
  3381. */
  3382. function cbrt(x) {
  3383. return new this(x).cbrt();
  3384. }
  3385. /*
  3386. * Return a new Decimal whose value is `x` rounded to an integer using `ROUND_CEIL`.
  3387. *
  3388. * x {number|string|Decimal}
  3389. *
  3390. */
  3391. function ceil(x) {
  3392. return finalise(x = new this(x), x.e + 1, 2);
  3393. }
  3394. /*
  3395. * Return a new Decimal whose value is `x` clamped to the range delineated by `min` and `max`.
  3396. *
  3397. * x {number|string|Decimal}
  3398. * min {number|string|Decimal}
  3399. * max {number|string|Decimal}
  3400. *
  3401. */
  3402. function clamp(x, min, max) {
  3403. return new this(x).clamp(min, max);
  3404. }
  3405. /*
  3406. * Configure global settings for a Decimal constructor.
  3407. *
  3408. * `obj` is an object with one or more of the following properties,
  3409. *
  3410. * precision {number}
  3411. * rounding {number}
  3412. * toExpNeg {number}
  3413. * toExpPos {number}
  3414. * maxE {number}
  3415. * minE {number}
  3416. * modulo {number}
  3417. * crypto {boolean|number}
  3418. * defaults {true}
  3419. *
  3420. * E.g. Decimal.config({ precision: 20, rounding: 4 })
  3421. *
  3422. */
  3423. function config(obj) {
  3424. if (!obj || typeof obj !== 'object') throw Error(decimalError + 'Object expected');
  3425. var i, p, v,
  3426. useDefaults = obj.defaults === true,
  3427. ps = [
  3428. 'precision', 1, MAX_DIGITS,
  3429. 'rounding', 0, 8,
  3430. 'toExpNeg', -EXP_LIMIT, 0,
  3431. 'toExpPos', 0, EXP_LIMIT,
  3432. 'maxE', 0, EXP_LIMIT,
  3433. 'minE', -EXP_LIMIT, 0,
  3434. 'modulo', 0, 9
  3435. ];
  3436. for (i = 0; i < ps.length; i += 3) {
  3437. if (p = ps[i], useDefaults) this[p] = DEFAULTS[p];
  3438. if ((v = obj[p]) !== void 0) {
  3439. if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
  3440. else throw Error(invalidArgument + p + ': ' + v);
  3441. }
  3442. }
  3443. if (p = 'crypto', useDefaults) this[p] = DEFAULTS[p];
  3444. if ((v = obj[p]) !== void 0) {
  3445. if (v === true || v === false || v === 0 || v === 1) {
  3446. if (v) {
  3447. if (typeof crypto != 'undefined' && crypto &&
  3448. (crypto.getRandomValues || crypto.randomBytes)) {
  3449. this[p] = true;
  3450. } else {
  3451. throw Error(cryptoUnavailable);
  3452. }
  3453. } else {
  3454. this[p] = false;
  3455. }
  3456. } else {
  3457. throw Error(invalidArgument + p + ': ' + v);
  3458. }
  3459. }
  3460. return this;
  3461. }
  3462. /*
  3463. * Return a new Decimal whose value is the cosine of `x`, rounded to `precision` significant
  3464. * digits using rounding mode `rounding`.
  3465. *
  3466. * x {number|string|Decimal} A value in radians.
  3467. *
  3468. */
  3469. function cos(x) {
  3470. return new this(x).cos();
  3471. }
  3472. /*
  3473. * Return a new Decimal whose value is the hyperbolic cosine of `x`, rounded to precision
  3474. * significant digits using rounding mode `rounding`.
  3475. *
  3476. * x {number|string|Decimal} A value in radians.
  3477. *
  3478. */
  3479. function cosh(x) {
  3480. return new this(x).cosh();
  3481. }
  3482. /*
  3483. * Create and return a Decimal constructor with the same configuration properties as this Decimal
  3484. * constructor.
  3485. *
  3486. */
  3487. function clone(obj) {
  3488. var i, p, ps;
  3489. /*
  3490. * The Decimal constructor and exported function.
  3491. * Return a new Decimal instance.
  3492. *
  3493. * v {number|string|Decimal} A numeric value.
  3494. *
  3495. */
  3496. function Decimal(v) {
  3497. var e, i, t,
  3498. x = this;
  3499. // Decimal called without new.
  3500. if (!(x instanceof Decimal)) return new Decimal(v);
  3501. // Retain a reference to this Decimal constructor, and shadow Decimal.prototype.constructor
  3502. // which points to Object.
  3503. x.constructor = Decimal;
  3504. // Duplicate.
  3505. if (isDecimalInstance(v)) {
  3506. x.s = v.s;
  3507. if (external) {
  3508. if (!v.d || v.e > Decimal.maxE) {
  3509. // Infinity.
  3510. x.e = NaN;
  3511. x.d = null;
  3512. } else if (v.e < Decimal.minE) {
  3513. // Zero.
  3514. x.e = 0;
  3515. x.d = [0];
  3516. } else {
  3517. x.e = v.e;
  3518. x.d = v.d.slice();
  3519. }
  3520. } else {
  3521. x.e = v.e;
  3522. x.d = v.d ? v.d.slice() : v.d;
  3523. }
  3524. return;
  3525. }
  3526. t = typeof v;
  3527. if (t === 'number') {
  3528. if (v === 0) {
  3529. x.s = 1 / v < 0 ? -1 : 1;
  3530. x.e = 0;
  3531. x.d = [0];
  3532. return;
  3533. }
  3534. if (v < 0) {
  3535. v = -v;
  3536. x.s = -1;
  3537. } else {
  3538. x.s = 1;
  3539. }
  3540. // Fast path for small integers.
  3541. if (v === ~~v && v < 1e7) {
  3542. for (e = 0, i = v; i >= 10; i /= 10) e++;
  3543. if (external) {
  3544. if (e > Decimal.maxE) {
  3545. x.e = NaN;
  3546. x.d = null;
  3547. } else if (e < Decimal.minE) {
  3548. x.e = 0;
  3549. x.d = [0];
  3550. } else {
  3551. x.e = e;
  3552. x.d = [v];
  3553. }
  3554. } else {
  3555. x.e = e;
  3556. x.d = [v];
  3557. }
  3558. return;
  3559. // Infinity, NaN.
  3560. } else if (v * 0 !== 0) {
  3561. if (!v) x.s = NaN;
  3562. x.e = NaN;
  3563. x.d = null;
  3564. return;
  3565. }
  3566. return parseDecimal(x, v.toString());
  3567. } else if (t !== 'string') {
  3568. throw Error(invalidArgument + v);
  3569. }
  3570. // Minus sign?
  3571. if ((i = v.charCodeAt(0)) === 45) {
  3572. v = v.slice(1);
  3573. x.s = -1;
  3574. } else {
  3575. // Plus sign?
  3576. if (i === 43) v = v.slice(1);
  3577. x.s = 1;
  3578. }
  3579. return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
  3580. }
  3581. Decimal.prototype = P;
  3582. Decimal.ROUND_UP = 0;
  3583. Decimal.ROUND_DOWN = 1;
  3584. Decimal.ROUND_CEIL = 2;
  3585. Decimal.ROUND_FLOOR = 3;
  3586. Decimal.ROUND_HALF_UP = 4;
  3587. Decimal.ROUND_HALF_DOWN = 5;
  3588. Decimal.ROUND_HALF_EVEN = 6;
  3589. Decimal.ROUND_HALF_CEIL = 7;
  3590. Decimal.ROUND_HALF_FLOOR = 8;
  3591. Decimal.EUCLID = 9;
  3592. Decimal.config = Decimal.set = config;
  3593. Decimal.clone = clone;
  3594. Decimal.isDecimal = isDecimalInstance;
  3595. Decimal.abs = abs;
  3596. Decimal.acos = acos;
  3597. Decimal.acosh = acosh; // ES6
  3598. Decimal.add = add;
  3599. Decimal.asin = asin;
  3600. Decimal.asinh = asinh; // ES6
  3601. Decimal.atan = atan;
  3602. Decimal.atanh = atanh; // ES6
  3603. Decimal.atan2 = atan2;
  3604. Decimal.cbrt = cbrt; // ES6
  3605. Decimal.ceil = ceil;
  3606. Decimal.clamp = clamp;
  3607. Decimal.cos = cos;
  3608. Decimal.cosh = cosh; // ES6
  3609. Decimal.div = div;
  3610. Decimal.exp = exp;
  3611. Decimal.floor = floor;
  3612. Decimal.hypot = hypot; // ES6
  3613. Decimal.ln = ln;
  3614. Decimal.log = log;
  3615. Decimal.log10 = log10; // ES6
  3616. Decimal.log2 = log2; // ES6
  3617. Decimal.max = max;
  3618. Decimal.min = min;
  3619. Decimal.mod = mod;
  3620. Decimal.mul = mul;
  3621. Decimal.pow = pow;
  3622. Decimal.random = random;
  3623. Decimal.round = round;
  3624. Decimal.sign = sign; // ES6
  3625. Decimal.sin = sin;
  3626. Decimal.sinh = sinh; // ES6
  3627. Decimal.sqrt = sqrt;
  3628. Decimal.sub = sub;
  3629. Decimal.sum = sum;
  3630. Decimal.tan = tan;
  3631. Decimal.tanh = tanh; // ES6
  3632. Decimal.trunc = trunc; // ES6
  3633. if (obj === void 0) obj = {};
  3634. if (obj) {
  3635. if (obj.defaults !== true) {
  3636. ps = ['precision', 'rounding', 'toExpNeg', 'toExpPos', 'maxE', 'minE', 'modulo', 'crypto'];
  3637. for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
  3638. }
  3639. }
  3640. Decimal.config(obj);
  3641. return Decimal;
  3642. }
  3643. /*
  3644. * Return a new Decimal whose value is `x` divided by `y`, rounded to `precision` significant
  3645. * digits using rounding mode `rounding`.
  3646. *
  3647. * x {number|string|Decimal}
  3648. * y {number|string|Decimal}
  3649. *
  3650. */
  3651. function div(x, y) {
  3652. return new this(x).div(y);
  3653. }
  3654. /*
  3655. * Return a new Decimal whose value is the natural exponential of `x`, rounded to `precision`
  3656. * significant digits using rounding mode `rounding`.
  3657. *
  3658. * x {number|string|Decimal} The power to which to raise the base of the natural log.
  3659. *
  3660. */
  3661. function exp(x) {
  3662. return new this(x).exp();
  3663. }
  3664. /*
  3665. * Return a new Decimal whose value is `x` round to an integer using `ROUND_FLOOR`.
  3666. *
  3667. * x {number|string|Decimal}
  3668. *
  3669. */
  3670. function floor(x) {
  3671. return finalise(x = new this(x), x.e + 1, 3);
  3672. }
  3673. /*
  3674. * Return a new Decimal whose value is the square root of the sum of the squares of the arguments,
  3675. * rounded to `precision` significant digits using rounding mode `rounding`.
  3676. *
  3677. * hypot(a, b, ...) = sqrt(a^2 + b^2 + ...)
  3678. *
  3679. * arguments {number|string|Decimal}
  3680. *
  3681. */
  3682. function hypot() {
  3683. var i, n,
  3684. t = new this(0);
  3685. external = false;
  3686. for (i = 0; i < arguments.length;) {
  3687. n = new this(arguments[i++]);
  3688. if (!n.d) {
  3689. if (n.s) {
  3690. external = true;
  3691. return new this(1 / 0);
  3692. }
  3693. t = n;
  3694. } else if (t.d) {
  3695. t = t.plus(n.times(n));
  3696. }
  3697. }
  3698. external = true;
  3699. return t.sqrt();
  3700. }
  3701. /*
  3702. * Return true if object is a Decimal instance (where Decimal is any Decimal constructor),
  3703. * otherwise return false.
  3704. *
  3705. */
  3706. function isDecimalInstance(obj) {
  3707. return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
  3708. }
  3709. /*
  3710. * Return a new Decimal whose value is the natural logarithm of `x`, rounded to `precision`
  3711. * significant digits using rounding mode `rounding`.
  3712. *
  3713. * x {number|string|Decimal}
  3714. *
  3715. */
  3716. function ln(x) {
  3717. return new this(x).ln();
  3718. }
  3719. /*
  3720. * Return a new Decimal whose value is the log of `x` to the base `y`, or to base 10 if no base
  3721. * is specified, rounded to `precision` significant digits using rounding mode `rounding`.
  3722. *
  3723. * log[y](x)
  3724. *
  3725. * x {number|string|Decimal} The argument of the logarithm.
  3726. * y {number|string|Decimal} The base of the logarithm.
  3727. *
  3728. */
  3729. function log(x, y) {
  3730. return new this(x).log(y);
  3731. }
  3732. /*
  3733. * Return a new Decimal whose value is the base 2 logarithm of `x`, rounded to `precision`
  3734. * significant digits using rounding mode `rounding`.
  3735. *
  3736. * x {number|string|Decimal}
  3737. *
  3738. */
  3739. function log2(x) {
  3740. return new this(x).log(2);
  3741. }
  3742. /*
  3743. * Return a new Decimal whose value is the base 10 logarithm of `x`, rounded to `precision`
  3744. * significant digits using rounding mode `rounding`.
  3745. *
  3746. * x {number|string|Decimal}
  3747. *
  3748. */
  3749. function log10(x) {
  3750. return new this(x).log(10);
  3751. }
  3752. /*
  3753. * Return a new Decimal whose value is the maximum of the arguments.
  3754. *
  3755. * arguments {number|string|Decimal}
  3756. *
  3757. */
  3758. function max() {
  3759. return maxOrMin(this, arguments, 'lt');
  3760. }
  3761. /*
  3762. * Return a new Decimal whose value is the minimum of the arguments.
  3763. *
  3764. * arguments {number|string|Decimal}
  3765. *
  3766. */
  3767. function min() {
  3768. return maxOrMin(this, arguments, 'gt');
  3769. }
  3770. /*
  3771. * Return a new Decimal whose value is `x` modulo `y`, rounded to `precision` significant digits
  3772. * using rounding mode `rounding`.
  3773. *
  3774. * x {number|string|Decimal}
  3775. * y {number|string|Decimal}
  3776. *
  3777. */
  3778. function mod(x, y) {
  3779. return new this(x).mod(y);
  3780. }
  3781. /*
  3782. * Return a new Decimal whose value is `x` multiplied by `y`, rounded to `precision` significant
  3783. * digits using rounding mode `rounding`.
  3784. *
  3785. * x {number|string|Decimal}
  3786. * y {number|string|Decimal}
  3787. *
  3788. */
  3789. function mul(x, y) {
  3790. return new this(x).mul(y);
  3791. }
  3792. /*
  3793. * Return a new Decimal whose value is `x` raised to the power `y`, rounded to precision
  3794. * significant digits using rounding mode `rounding`.
  3795. *
  3796. * x {number|string|Decimal} The base.
  3797. * y {number|string|Decimal} The exponent.
  3798. *
  3799. */
  3800. function pow(x, y) {
  3801. return new this(x).pow(y);
  3802. }
  3803. /*
  3804. * Returns a new Decimal with a random value equal to or greater than 0 and less than 1, and with
  3805. * `sd`, or `Decimal.precision` if `sd` is omitted, significant digits (or less if trailing zeros
  3806. * are produced).
  3807. *
  3808. * [sd] {number} Significant digits. Integer, 0 to MAX_DIGITS inclusive.
  3809. *
  3810. */
  3811. function random(sd) {
  3812. var d, e, k, n,
  3813. i = 0,
  3814. r = new this(1),
  3815. rd = [];
  3816. if (sd === void 0) sd = this.precision;
  3817. else checkInt32(sd, 1, MAX_DIGITS);
  3818. k = Math.ceil(sd / LOG_BASE);
  3819. if (!this.crypto) {
  3820. for (; i < k;) rd[i++] = Math.random() * 1e7 | 0;
  3821. // Browsers supporting crypto.getRandomValues.
  3822. } else if (crypto.getRandomValues) {
  3823. d = crypto.getRandomValues(new Uint32Array(k));
  3824. for (; i < k;) {
  3825. n = d[i];
  3826. // 0 <= n < 4294967296
  3827. // Probability n >= 4.29e9, is 4967296 / 4294967296 = 0.00116 (1 in 865).
  3828. if (n >= 4.29e9) {
  3829. d[i] = crypto.getRandomValues(new Uint32Array(1))[0];
  3830. } else {
  3831. // 0 <= n <= 4289999999
  3832. // 0 <= (n % 1e7) <= 9999999
  3833. rd[i++] = n % 1e7;
  3834. }
  3835. }
  3836. // Node.js supporting crypto.randomBytes.
  3837. } else if (crypto.randomBytes) {
  3838. // buffer
  3839. d = crypto.randomBytes(k *= 4);
  3840. for (; i < k;) {
  3841. // 0 <= n < 2147483648
  3842. n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 0x7f) << 24);
  3843. // Probability n >= 2.14e9, is 7483648 / 2147483648 = 0.0035 (1 in 286).
  3844. if (n >= 2.14e9) {
  3845. crypto.randomBytes(4).copy(d, i);
  3846. } else {
  3847. // 0 <= n <= 2139999999
  3848. // 0 <= (n % 1e7) <= 9999999
  3849. rd.push(n % 1e7);
  3850. i += 4;
  3851. }
  3852. }
  3853. i = k / 4;
  3854. } else {
  3855. throw Error(cryptoUnavailable);
  3856. }
  3857. k = rd[--i];
  3858. sd %= LOG_BASE;
  3859. // Convert trailing digits to zeros according to sd.
  3860. if (k && sd) {
  3861. n = mathpow(10, LOG_BASE - sd);
  3862. rd[i] = (k / n | 0) * n;
  3863. }
  3864. // Remove trailing words which are zero.
  3865. for (; rd[i] === 0; i--) rd.pop();
  3866. // Zero?
  3867. if (i < 0) {
  3868. e = 0;
  3869. rd = [0];
  3870. } else {
  3871. e = -1;
  3872. // Remove leading words which are zero and adjust exponent accordingly.
  3873. for (; rd[0] === 0; e -= LOG_BASE) rd.shift();
  3874. // Count the digits of the first word of rd to determine leading zeros.
  3875. for (k = 1, n = rd[0]; n >= 10; n /= 10) k++;
  3876. // Adjust the exponent for leading zeros of the first word of rd.
  3877. if (k < LOG_BASE) e -= LOG_BASE - k;
  3878. }
  3879. r.e = e;
  3880. r.d = rd;
  3881. return r;
  3882. }
  3883. /*
  3884. * Return a new Decimal whose value is `x` rounded to an integer using rounding mode `rounding`.
  3885. *
  3886. * To emulate `Math.round`, set rounding to 7 (ROUND_HALF_CEIL).
  3887. *
  3888. * x {number|string|Decimal}
  3889. *
  3890. */
  3891. function round(x) {
  3892. return finalise(x = new this(x), x.e + 1, this.rounding);
  3893. }
  3894. /*
  3895. * Return
  3896. * 1 if x > 0,
  3897. * -1 if x < 0,
  3898. * 0 if x is 0,
  3899. * -0 if x is -0,
  3900. * NaN otherwise
  3901. *
  3902. * x {number|string|Decimal}
  3903. *
  3904. */
  3905. function sign(x) {
  3906. x = new this(x);
  3907. return x.d ? (x.d[0] ? x.s : 0 * x.s) : x.s || NaN;
  3908. }
  3909. /*
  3910. * Return a new Decimal whose value is the sine of `x`, rounded to `precision` significant digits
  3911. * using rounding mode `rounding`.
  3912. *
  3913. * x {number|string|Decimal} A value in radians.
  3914. *
  3915. */
  3916. function sin(x) {
  3917. return new this(x).sin();
  3918. }
  3919. /*
  3920. * Return a new Decimal whose value is the hyperbolic sine of `x`, rounded to `precision`
  3921. * significant digits using rounding mode `rounding`.
  3922. *
  3923. * x {number|string|Decimal} A value in radians.
  3924. *
  3925. */
  3926. function sinh(x) {
  3927. return new this(x).sinh();
  3928. }
  3929. /*
  3930. * Return a new Decimal whose value is the square root of `x`, rounded to `precision` significant
  3931. * digits using rounding mode `rounding`.
  3932. *
  3933. * x {number|string|Decimal}
  3934. *
  3935. */
  3936. function sqrt(x) {
  3937. return new this(x).sqrt();
  3938. }
  3939. /*
  3940. * Return a new Decimal whose value is `x` minus `y`, rounded to `precision` significant digits
  3941. * using rounding mode `rounding`.
  3942. *
  3943. * x {number|string|Decimal}
  3944. * y {number|string|Decimal}
  3945. *
  3946. */
  3947. function sub(x, y) {
  3948. return new this(x).sub(y);
  3949. }
  3950. /*
  3951. * Return a new Decimal whose value is the sum of the arguments, rounded to `precision`
  3952. * significant digits using rounding mode `rounding`.
  3953. *
  3954. * Only the result is rounded, not the intermediate calculations.
  3955. *
  3956. * arguments {number|string|Decimal}
  3957. *
  3958. */
  3959. function sum() {
  3960. var i = 0,
  3961. args = arguments,
  3962. x = new this(args[i]);
  3963. external = false;
  3964. for (; x.s && ++i < args.length;) x = x.plus(args[i]);
  3965. external = true;
  3966. return finalise(x, this.precision, this.rounding);
  3967. }
  3968. /*
  3969. * Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant
  3970. * digits using rounding mode `rounding`.
  3971. *
  3972. * x {number|string|Decimal} A value in radians.
  3973. *
  3974. */
  3975. function tan(x) {
  3976. return new this(x).tan();
  3977. }
  3978. /*
  3979. * Return a new Decimal whose value is the hyperbolic tangent of `x`, rounded to `precision`
  3980. * significant digits using rounding mode `rounding`.
  3981. *
  3982. * x {number|string|Decimal} A value in radians.
  3983. *
  3984. */
  3985. function tanh(x) {
  3986. return new this(x).tanh();
  3987. }
  3988. /*
  3989. * Return a new Decimal whose value is `x` truncated to an integer.
  3990. *
  3991. * x {number|string|Decimal}
  3992. *
  3993. */
  3994. function trunc(x) {
  3995. return finalise(x = new this(x), x.e + 1, 1);
  3996. }
  3997. // Create and configure initial Decimal constructor.
  3998. Decimal = clone(DEFAULTS);
  3999. Decimal.prototype.constructor = Decimal;
  4000. Decimal['default'] = Decimal.Decimal = Decimal;
  4001. // Create the internal constants from their string values.
  4002. LN10 = new Decimal(LN10);
  4003. PI = new Decimal(PI);
  4004. // Export.
  4005. // AMD.
  4006. if (typeof define == 'function' && define.amd) {
  4007. define(function () {
  4008. return Decimal;
  4009. });
  4010. // Node and other environments that support module.exports.
  4011. } else if (typeof module != 'undefined' && module.exports) {
  4012. if (typeof Symbol == 'function' && typeof Symbol.iterator == 'symbol') {
  4013. P[Symbol['for']('nodejs.util.inspect.custom')] = P.toString;
  4014. P[Symbol.toStringTag] = 'Decimal';
  4015. }
  4016. module.exports = Decimal;
  4017. // Browser.
  4018. } else {
  4019. if (!globalScope) {
  4020. globalScope = typeof self != 'undefined' && self && self.self == self ? self : window;
  4021. }
  4022. noConflict = globalScope.Decimal;
  4023. Decimal.noConflict = function () {
  4024. globalScope.Decimal = noConflict;
  4025. return Decimal;
  4026. };
  4027. globalScope.Decimal = Decimal;
  4028. }
  4029. })(this);
  4030. }, function(modId) {var map = {}; return __REQUIRE__(map[modId], modId); })
  4031. return __REQUIRE__(1720091880135);
  4032. })()
  4033. //miniprogram-npm-outsideDeps=[]
  4034. //# sourceMappingURL=index.js.map