multiprecision.qbk 338 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198
  1. [/
  2. Copyright 2011, 2013 John Maddock.
  3. Copyright 2013 - 2019 Paul A. Bristow.
  4. Copyright 2013 Christopher Kormanyos.
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt).
  8. ]
  9. [library Boost.Multiprecision
  10. [quickbook 1.7]
  11. [copyright 2002-2019 John Maddock and Christopher Kormanyos]
  12. [purpose Multiprecision Number library]
  13. [license
  14. Distributed under the Boost Software License, Version 1.0.
  15. (See accompanying file LICENSE_1_0.txt or copy at
  16. [@http://www.boost.org/LICENSE_1_0.txt])
  17. ]
  18. [authors [Maddock, John], [Kormanyos, Christopher]]
  19. [/last-revision $Date: 2011-07-08 18:51:46 +0100 (Fri, 08 Jul 2011) $]
  20. ]
  21. [import html4_symbols.qbk] [/Ideally this should be the same as Boost.Math I:\boost\libs\math\doc]
  22. [import ../example/gmp_snips.cpp]
  23. [import ../example/mpfr_snips.cpp]
  24. [import ../example/mpfi_snips.cpp]
  25. [import ../example/float128_snips.cpp]
  26. [import ../example/cpp_dec_float_snips.cpp]
  27. [import ../example/cpp_bin_float_snips.cpp]
  28. [import ../example/cpp_int_import_export.cpp]
  29. [import ../example/cpp_bin_float_import_export.cpp]
  30. [import ../example/tommath_snips.cpp]
  31. [import ../example/cpp_int_snips.cpp]
  32. [import ../example/random_snips.cpp]
  33. [import ../example/safe_prime.cpp]
  34. [import ../example/mixed_integer_arithmetic.cpp]
  35. [import ../example/logged_adaptor.cpp]
  36. [import ../example/numeric_limits_snips.cpp]
  37. [import ../example/hashing_examples.cpp]
  38. [import ../example/cpp_complex_examples.cpp]
  39. [import ../example/mpc_examples.cpp]
  40. [import ../example/complex128_examples.cpp]
  41. [import ../example/eigen_example.cpp]
  42. [import ../example/mpfr_precision.cpp]
  43. [import ../example/constexpr_float_arithmetic_examples.cpp]
  44. [import ../test/constexpr_test_cpp_int_5.cpp]
  45. [/External links as templates (see also some defs below)]
  46. [template mpfr[] [@http://www.mpfr.org MPFR]]
  47. [template mpc[] [@http://www.multiprecision.org MPC]]
  48. [template mpfi[] [@http://perso.ens-lyon.fr/nathalie.revol/software.html MPFI]]
  49. [template gmp[] [@http://gmplib.org GMP]]
  50. [template mpf_class[] [@http://gmplib.org/manual/C_002b_002b-Interface-Floats.html#C_002b_002b-Interface-Floats mpf_class]]
  51. [template mpfr_class[] [@http://math.berkeley.edu/~wilken/code/gmpfrxx/ mpfr_class]]
  52. [template mpreal[] [@http://www.holoborodko.com/pavel/mpfr/ mpreal]]
  53. [template mpir[] [@http://mpir.org/ MPIR]]
  54. [template tommath[] [@http://libtom.net libtommath]]
  55. [template quadmath[] [@http://gcc.gnu.org/onlinedocs/libquadmath/ libquadmath]]
  56. [template super[x]'''<superscript>'''[x]'''</superscript>''']
  57. [template sub[x]'''<subscript>'''[x]'''</subscript>''']
  58. [/insert Equation as a PNG or SVG image, previous generated with an external tool like Latex.]
  59. [/Used thus [equation ellint6] - without the file type suffix which will chosen automatically.]
  60. [template equation[name] '''<inlinemediaobject>
  61. <imageobject role="html">
  62. <imagedata fileref="../'''[name]'''.png"></imagedata>
  63. </imageobject>
  64. <imageobject role="print">
  65. <imagedata fileref="../'''[name]'''.svg"></imagedata>
  66. </imageobject>
  67. </inlinemediaobject>''']
  68. [/insert Indented one-line expression italic and serif font probably using Unicode symbols for Greek and symbols.]
  69. [/Example: [expression [sub 1]F[sub 0](a, z) = (1-z)[super -a]]]
  70. [template expression[equation]
  71. [:
  72. [role serif_italic [equation]]
  73. ]
  74. [/ Hint you may need to enclose equation in brackets if it contains comma(s) to avoid "error invalid number of arguments"]
  75. ]
  76. [def __tick [role aligncenter [role green \u2714]]] [/ u2714 is a HEAVY CHECK MARK tick (2713 check mark), green]
  77. [def __cross [role aligncenter [role red \u2718]]] [/ u2718 is a heavy cross, red]
  78. [def __star [role aligncenter [role red \u2736]]] [/ 6-point star red ]
  79. [/Boost.Multiprecision internals links]
  80. [def __cpp_int [link boost_multiprecision.tut.ints.cpp_int cpp_int]]
  81. [def __gmp_int [link boost_multiprecision.tut.ints.gmp_int gmp_int]]
  82. [def __tom_int [link boost_multiprecision.tut.ints.tom_int tom_int]]
  83. [def __gmp_float [link boost_multiprecision.tut.floats.gmp_float gmp_float]]
  84. [def __mpf_float [link boost_multiprecision.tut.floats.gmp_float gmp_float]]
  85. [def __mpfr_float_backend [link boost_multiprecision.tut.floats.mpfr_float mpfr_float]]
  86. [def __cpp_bin_float [link boost_multiprecision.tut.floats.cpp_bin_float cpp_bin_float]]
  87. [def __cpp_dec_float [link boost_multiprecision.tut.floats.cpp_dec_float cpp_dec_float]]
  88. [def __gmp_rational [link boost_multiprecision.tut.rational.gmp_rational gmp_rational]]
  89. [def __cpp_rational [link boost_multiprecision.tut.rational.cpp_rational cpp_rational]]
  90. [def __tommath_rational [link boost_multiprecision.tut.rational.tommath_rational tommath_rational]]
  91. [def __number [link boost_multiprecision.ref.number number]]
  92. [def __float128 [link boost_multiprecision.tut.floats.float128 float128]]
  93. [def __cpp_complex [link boost_multiprecision.tut.complex.cpp_complex cpp_complex]]
  94. [def __mpc_complex [link boost_multiprecision.tut.complex.mpc_complex mpc_complex]]
  95. [def __debug_adaptor [link boost_multiprecision.tut.misc.debug_adaptor debug_adaptor]]
  96. [def __logged_adaptor [link boost_multiprecision.tut.misc.logged_adaptor logged_adaptor]]
  97. [def __rational_adaptor [link boost_multiprecision.tut.rational.rational_adaptor rational_adaptor]]
  98. [def __cpp_complex [link boost_multiprecision.tut.complex.cpp_complex cpp_complex]]
  99. [def __mpc_complex [link boost_multiprecision.tut.complex.mpc_complex mpc_complex]]
  100. [def __complex128 [link boost_multiprecision.tut.complex.complex128 complex128]]
  101. [def __complex_adaptor [link boost_multiprecision.tut.complex.complex_adaptor complex_adaptor]]
  102. [/External links as macro definitions.]
  103. [def __expression_template [@https://en.wikipedia.org/wiki/Expression_templates expression template]]
  104. [def __expression_templates [@https://en.wikipedia.org/wiki/Expression_templates expression templates]] [/plural version]
  105. [def __UDT [@http://eel.is/c++draft/definitions#defns.prog.def.type program-defined type]]
  106. [def __fundamental_type [@https://en.cppreference.com/w/cpp/language/types fundamental (built-in) type]]
  107. [section:intro Introduction]
  108. The Multiprecision Library provides [link boost_multiprecision.tut.ints integer],
  109. [link boost_multiprecision.tut.rational rational],
  110. [link boost_multiprecision.tut.floats floating-point],
  111. and [link boost_multiprecision.tut.complex complex] types in C++ that have more
  112. range and precision than C++'s ordinary built-in types.
  113. The big number types in Multiprecision can be used with a wide
  114. selection of basic mathematical operations, elementary transcendental
  115. functions as well as the functions in Boost.Math.
  116. The Multiprecision types can also interoperate with any
  117. __fundamental_type in C++ using clearly defined conversion rules.
  118. This allows Boost.Multiprecision to be used for all
  119. kinds of mathematical calculations involving integer,
  120. rational and floating-point types requiring extended
  121. range and precision.
  122. Multiprecision consists of a generic interface to the
  123. mathematics of large numbers as well as a selection of
  124. big number back-ends, with support for integer, rational,
  125. floating-point, and complex types. Boost.Multiprecision provides a selection
  126. of back-ends provided off-the-rack in including
  127. interfaces to GMP, MPFR, MPIR, MPC, TomMath as well as
  128. its own collection of Boost-licensed, header-only back-ends for
  129. integers, rationals and floats. In addition, user-defined back-ends
  130. can be created and used with the interface of Multiprecision,
  131. provided the class implementation adheres to the necessary
  132. [link boost_multiprecision.ref.backendconc concepts].
  133. Depending upon the number type, precision may be arbitrarily large
  134. (limited only by available memory), fixed at compile time
  135. (for example, 50 or 100 decimal digits), or a variable controlled at run-time
  136. by member functions. The types are __expression_templates - enabled for
  137. better performance than naive user-defined types.
  138. The Multiprecision library comes in two distinct parts:
  139. * An expression-template-enabled front-end `number`
  140. that handles all the operator overloading, expression evaluation optimization, and code reduction.
  141. * A selection of back-ends that implement the actual arithmetic operations, and need conform only to the
  142. reduced interface requirements of the front-end.
  143. Separation of front-end and back-end allows use of highly refined, but restricted license libraries
  144. where possible, but provides Boost license alternatives for users who must have a portable
  145. unconstrained license.
  146. Which is to say some back-ends rely on 3rd party libraries,
  147. but a header-only Boost license version is always available (if somewhat slower).
  148. [h5:getting_started Getting started with Boost.Multiprecision]
  149. Should you just wish to 'cut to the chase' just to get bigger integers and/or bigger and more precise reals as simply and portably as possible,
  150. close to 'drop-in' replacements for the __fundamental_type analogs,
  151. then use a fully Boost-licensed number type, and skip to one of more of :
  152. * __cpp_int for multiprecision integers,
  153. * __cpp_rational for rational types,
  154. * __cpp_bin_float and __cpp_dec_float for multiprecision floating-point types,
  155. * __cpp_complex for complex types.
  156. The library is very often used via one of the predefined convenience `typedef`s
  157. like `boost::multiprecision::int128_t` or `boost::multiprecision::cpp_bin_float_quad`.
  158. For example, if you want a signed, 128-bit fixed size integer:
  159. #include <boost/multiprecision/cpp_int.hpp> // Integer types.
  160. boost::multiprecision::int128_t my_128_bit_int;
  161. Alternatively, and more adventurously, if you wanted an
  162. [@http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic arbitrary precision]
  163. integer type using [gmp] as the underlying implementation then you could use:
  164. #include <boost/multiprecision/gmp.hpp> // Defines the wrappers around the GMP library's types
  165. boost::multiprecision::mpz_int myint; // Arbitrary precision integer type.
  166. Or for a simple, portable 128-bit floating-point close to a drop-in for a __fundamental_type like `double`, usually 64-bit
  167. #include <boost/multiprecision/cpp_bin_float.hpp>
  168. boost::multiprecision::cpp_bin_float_quad my_quad_real;
  169. Alternatively, you can compose your own 'custom' multiprecision type, by combining `number` with one of the
  170. predefined back-end types. For example, suppose you wanted a 300 decimal digit floating-point type
  171. based on the [mpfr] library. In this case, there's no predefined `typedef` with that level of precision,
  172. so instead we compose our own:
  173. #include <boost/multiprecision/mpfr.hpp> // Defines the Backend type that wraps MPFR.
  174. namespace mp = boost::multiprecision; // Reduce the typing a bit later...
  175. typedef mp::number<mp::mpfr_float_backend<300> > my_float;
  176. my_float a, b, c; // These variables have 300 decimal digits precision.
  177. We can repeat the above example, but with the expression templates disabled (for faster compile times, but slower runtimes)
  178. by passing a second template argument to `number`:
  179. #include <boost/multiprecision/mpfr.hpp> // Defines the Backend type that wraps MPFR.
  180. namespace mp = boost::multiprecision; // Reduce the typing a bit later...
  181. typedef mp::number<mp::mpfr_float_backend<300>, et_off> my_float;
  182. my_float a, b, c; // These variables have 300 decimal digits precision
  183. We can also mix arithmetic operations between different types, provided there is an unambiguous implicit conversion from one
  184. type to the other:
  185. #include <boost/multiprecision/cpp_int.hpp>
  186. namespace mp = boost::multiprecision; // Reduce the typing a bit later...
  187. mp::int128_t a(3), b(4);
  188. mp::int512_t c(50), d;
  189. d = c * a; // OK, result of mixed arithmetic is an int512_t
  190. Conversions are also allowed:
  191. d = a; // OK, widening conversion.
  192. d = a * b; // OK, can convert from an expression template too.
  193. However conversions that are inherently lossy are either declared explicit or else forbidden altogether:
  194. d = 3.14; // Error implicit conversion from double not allowed.
  195. d = static_cast<mp::int512_t>(3.14); // OK explicit construction is allowed
  196. Mixed arithmetic will fail if the conversion is either ambiguous or explicit:
  197. number<cpp_int_backend<>, et_off> a(2);
  198. number<cpp_int_backend<>, et_on> b(3);
  199. b = a * b; // Error, implicit conversion could go either way.
  200. b = a * 3.14; // Error, no operator overload if the conversion would be explicit.
  201. [h4 Move Semantics]
  202. On compilers that support rvalue-references, class `number` is move-enabled if the underlying backend is.
  203. In addition the non-expression template operator overloads (see below) are move aware and have overloads
  204. that look something like:
  205. template <class B>
  206. number<B, et_off> operator + (number<B, et_off>&& a, const number<B, et_off>& b)
  207. {
  208. return std::move(a += b);
  209. }
  210. These operator overloads ensure that many expressions can be evaluated without actually generating any temporaries.
  211. However, there are still many simple expressions such as
  212. a = b * c;
  213. which don't noticeably benefit from move support. Therefore, optimal performance comes from having both
  214. move-support, and expression templates enabled.
  215. Note that while "moved-from" objects are left in a sane state, they have an unspecified value, and the only permitted
  216. operations on them are destruction or the assignment of a new value. Any other operation should be considered
  217. a programming error and all of our backends will trigger an assertion if any other operation is attempted. This behavior
  218. allows for optimal performance on move-construction (i.e. no allocation required, we just take ownership of the existing
  219. object's internal state), while maintaining usability in the standard library containers.
  220. [h4:expression_templates Expression Templates]
  221. Class `number` is expression-template-enabled: that means that rather than having a multiplication
  222. operator that looks like this:
  223. template <class Backend>
  224. number<Backend> operator * (const number<Backend>& a, const number<Backend>& b)
  225. {
  226. number<Backend> result(a);
  227. result *= b;
  228. return result;
  229. }
  230. Instead the operator looks more like this:
  231. template <class Backend>
  232. ``['unmentionable-type]`` operator * (const number<Backend>& a, const number<Backend>& b);
  233. Where the '['unmentionable]' return type is an implementation detail that, rather than containing the result
  234. of the multiplication, contains instructions on how to compute the result. In effect it's just a pair
  235. of references to the arguments of the function, plus some compile-time information that stores what the operation
  236. is.
  237. The great advantage of this method is the ['elimination of temporaries]: for example, the "naive" implementation
  238. of `operator*` above, requires one temporary for computing the result, and at least another one to return it. It's true
  239. that sometimes this overhead can be reduced by using move-semantics, but it can't be eliminated completely. For example,
  240. lets suppose we're evaluating a polynomial via Horner's method, something like this:
  241. T a[7] = { /* some values */ };
  242. //....
  243. y = (((((a[6] * x + a[5]) * x + a[4]) * x + a[3]) * x + a[2]) * x + a[1]) * x + a[0];
  244. If type `T` is a `number`, then this expression is evaluated ['without creating a single temporary value]. In contrast,
  245. if we were using the [mpfr_class] C++ wrapper for [mpfr] - then this expression would result in no less than 11
  246. temporaries (this is true even though [mpfr_class] does use expression templates to reduce the number of temporaries somewhat). Had
  247. we used an even simpler wrapper around [mpfr] like [mpreal] things would have been even worse and no less that 24 temporaries
  248. are created for this simple expression (note - we actually measure the number of memory allocations performed rather than
  249. the number of temporaries directly, note also that the [mpf_class] wrapper that will be supplied with GMP-5.1 reduces the number of
  250. temporaries to pretty much zero). Note that if we compile with expression templates disabled and rvalue-reference support
  251. on, then actually still have no wasted memory allocations as even though temporaries are created, their contents are moved
  252. rather than copied.
  253. [footnote The actual number generated will depend on the compiler, how well it optimizes the code, and whether it supports
  254. rvalue references. The number of 11 temporaries was generated with Visual C++ 2010.]
  255. [important
  256. Expression templates can radically reorder the operations in an expression, for example:
  257. a = (b * c) * a;
  258. Will get transformed into:
  259. a *= c;
  260. a *= b;
  261. If this is likely to be an issue for a particular application, then they should be disabled.
  262. ]
  263. This library also extends expression template support to standard library functions like `abs` or `sin` with `number`
  264. arguments. This means that an expression such as:
  265. y = abs(x);
  266. can be evaluated without a single temporary being calculated. Even expressions like:
  267. y = sin(x);
  268. get this treatment, so that variable 'y' is used as "working storage" within the implementation of `sin`,
  269. thus reducing the number of temporaries used by one. Of course, should you write:
  270. x = sin(x);
  271. Then we clearly can't use `x` as working storage during the calculation, so then a temporary variable
  272. is created in this case.
  273. Given the comments above, you might be forgiven for thinking that expression-templates are some kind of universal-panacea:
  274. sadly though, all tricks like this have their downsides. For one thing, expression template libraries
  275. like this one, tend to be slower to compile than their simpler cousins, they're also harder to debug
  276. (should you actually want to step through our code!), and rely on compiler optimizations being turned
  277. on to give really good performance. Also, since the return type from expressions involving `number`s
  278. is an "unmentionable implementation detail", you have to be careful to cast the result of an expression
  279. to the actual number type when passing an expression to a template function. For example, given:
  280. template <class T>
  281. void my_proc(const T&);
  282. Then calling:
  283. my_proc(a+b);
  284. Will very likely result in obscure error messages inside the body of `my_proc` - since we've passed it
  285. an expression template type, and not a number type. Instead we probably need:
  286. my_proc(my_number_type(a+b));
  287. Having said that, these situations don't occur that often - or indeed not at all for non-template functions.
  288. In addition, all the functions in the Boost.Math library will automatically convert expression-template arguments
  289. to the underlying number type without you having to do anything, so:
  290. mpfr_float_100 a(20), delta(0.125);
  291. boost::math::gamma_p(a, a + delta);
  292. Will work just fine, with the `a + delta` expression template argument getting converted to an `mpfr_float_100`
  293. internally by the Boost.Math library.
  294. [caution In C++11 you should never store an expression template using:
  295. `auto my_expression = a + b - c;`
  296. unless you're absolutely sure that the lifetimes of `a`, `b` and `c` will outlive that of `my_expression`.
  297. In fact, it is particularly easy to create dangling references by mixing expression templates with the `auto`
  298. keyword, for example:
  299. `auto val = cpp_dec_float_50("23.1") * 100;`
  300. In this situation, the integer literal is stored directly in the expression template - so its use is OK here -
  301. but the `cpp_dec_float_50` temporary is stored by reference and then destructed when the statement completes,
  302. leaving a dangling reference.
  303. [*['If in doubt, do not ever mix expression templates with the `auto` keyword.]]
  304. ]
  305. And finally... the performance improvements from an expression template library like this are often not as
  306. dramatic as the reduction in number of temporaries would suggest. For example, if we compare this library with
  307. [mpfr_class] and [mpreal], with all three using the underlying [mpfr] library at 50 decimal digits precision then
  308. we see the following typical results for polynomial execution:
  309. [table Evaluation of Order 6 Polynomial.
  310. [[Library] [Relative Time] [Relative number of memory allocations]]
  311. [[number] [1.0 (0.00957s)] [1.0 (2996 total)]]
  312. [[[mpfr_class]] [1.1 (0.0102s)] [4.3 (12976 total)]]
  313. [[[mpreal]] [1.6 (0.0151s)] [9.3 (27947 total)]]
  314. ]
  315. As you can see, the execution time increases a lot more slowly than the number of memory allocations. There are
  316. a number of reasons for this:
  317. * The cost of extended-precision multiplication and division is so great, that the times taken for these tend to
  318. swamp everything else.
  319. * The cost of an in-place multiplication (using `operator*=`) tends to be more than an out-of-place
  320. `operator*` (typically `operator *=` has to create a temporary workspace to carry out the multiplication, where
  321. as `operator*` can use the target variable as workspace). Since the expression templates carry out their
  322. magic by converting out-of-place operators to in-place ones, we necessarily take this hit. Even so the
  323. transformation is more efficient than creating the extra temporary variable, just not by as much as
  324. one would hope.
  325. Finally, note that `number` takes a second template argument, which, when set to `et_off` disables all
  326. the expression template machinery. The result is much faster to compile, but slower at runtime.
  327. We'll conclude this section by providing some more performance comparisons between these three libraries,
  328. again, all are using [mpfr] to carry out the underlying arithmetic, and all are operating at the same precision
  329. (50 decimal digits):
  330. [table Evaluation of Boost.Math's Bessel function test data
  331. [[Library] [Relative Time] [Relative Number of Memory Allocations]]
  332. [[mpfr_float_50] [1.0 (5.78s)] [1.0 (1611963)]]
  333. [[number<mpfr_float_backend<50>, et_off>[br](but with rvalue reference support)]
  334. [1.1 (6.29s)] [2.64 (4260868)]]
  335. [[[mpfr_class]] [1.1 (6.28s)] [2.45 (3948316)]]
  336. [[[mpreal]] [1.65 (9.54s)] [8.21 (13226029)]]
  337. ]
  338. [table Evaluation of Boost.Math's Non-Central T distribution test data
  339. [[Library][Relative Time][Relative Number of Memory Allocations]]
  340. [[number] [1.0 (263s)][1.0 (127710873)]]
  341. [[number<mpfr_float_backend<50>, et_off>[br](but with rvalue reference support)]
  342. [1.0 (260s)][1.2 (156797871)]]
  343. [[[mpfr_class]] [1.1 (287s)][2.1 (268336640)]]
  344. [[[mpreal]] [1.5 (389s)][3.6 (466960653)]]
  345. ]
  346. The above results were generated on Win32 compiling with Visual C++ 2010, all optimizations on (/Ox),
  347. with MPFR 3.0 and MPIR 2.3.0.
  348. [endsect] [/section:intro Introduction]
  349. [section:tut Tutorial]
  350. In order to use this library you need to make two choices:
  351. * What kind of number do I want ([link boost_multiprecision.tut.ints integer],
  352. [link boost_multiprecision.tut.floats floating-point], [link boost_multiprecision.tut.rational rational], or [link boost_multiprecision.tut.complex complex]).
  353. * Which back-end do I want to perform the actual arithmetic (Boost-supplied, GMP, MPFR, MPC, Tommath etc)?
  354. [section:ints Integer Types]
  355. The following back-ends provide integer arithmetic:
  356. [table
  357. [[Backend Type][Header][Radix][Dependencies][Pros][Cons]]
  358. [[`cpp_int`][boost/multiprecision/cpp_int.hpp][2][None]
  359. [Very versatile, Boost licensed, all C++ integer type which support both [@http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic arbitrary precision] and fixed precision integer types.][Slower than [gmp], though typically not as slow as [tommath]]]
  360. [[`gmp_int`][boost/multiprecision/gmp.hpp][2][[gmp]][Very fast and efficient back-end.][Dependency on GNU licensed [gmp] library.]]
  361. [[`tom_int`][boost/multiprecision/tommath.hpp][2][[tommath]][Public domain back-end with no licence restrictions.][Slower than [gmp].]]
  362. ]
  363. [section:cpp_int cpp_int]
  364. `#include <boost/multiprecision/cpp_int.hpp>`
  365. namespace boost{ namespace multiprecision{
  366. typedef unspecified-type limb_type;
  367. enum cpp_integer_type { signed_magnitude, unsigned_magnitude };
  368. enum cpp_int_check_type { checked, unchecked };
  369. template <unsigned MinBits = 0,
  370. unsigned MaxBits = 0,
  371. cpp_integer_type SignType = signed_magnitude,
  372. cpp_int_check_type Checked = unchecked,
  373. class Allocator = std::allocator<limb_type> >
  374. class cpp_int_backend;
  375. //
  376. // Expression templates default to et_off if there is no allocator:
  377. //
  378. template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked>
  379. struct expression_template_default<cpp_int_backend<MinBits, MaxBits, SignType, Checked, void> >
  380. { static const expression_template_option value = et_off; };
  381. typedef number<cpp_int_backend<> > cpp_int; // arbitrary precision integer
  382. typedef rational_adaptor<cpp_int_backend<> > cpp_rational_backend;
  383. typedef number<cpp_rational_backend> cpp_rational; // arbitrary precision rational number
  384. // Fixed precision unsigned types:
  385. typedef number<cpp_int_backend<128, 128, unsigned_magnitude, unchecked, void> > uint128_t;
  386. typedef number<cpp_int_backend<256, 256, unsigned_magnitude, unchecked, void> > uint256_t;
  387. typedef number<cpp_int_backend<512, 512, unsigned_magnitude, unchecked, void> > uint512_t;
  388. typedef number<cpp_int_backend<1024, 1024, unsigned_magnitude, unchecked, void> > uint1024_t;
  389. // Fixed precision signed types:
  390. typedef number<cpp_int_backend<128, 128, signed_magnitude, unchecked, void> > int128_t;
  391. typedef number<cpp_int_backend<256, 256, signed_magnitude, unchecked, void> > int256_t;
  392. typedef number<cpp_int_backend<512, 512, signed_magnitude, unchecked, void> > int512_t;
  393. typedef number<cpp_int_backend<1024, 1024, signed_magnitude, unchecked, void> > int1024_t;
  394. // Over again, but with checking enabled this time:
  395. typedef number<cpp_int_backend<0, 0, signed_magnitude, checked> > checked_cpp_int;
  396. typedef rational_adaptor<cpp_int_backend<0, 0, signed_magnitude, checked> > checked_cpp_rational_backend;
  397. typedef number<cpp_rational_backend> checked_cpp_rational;
  398. // Checked fixed precision unsigned types:
  399. typedef number<cpp_int_backend<128, 128, unsigned_magnitude, checked, void> > checked_uint128_t;
  400. typedef number<cpp_int_backend<256, 256, unsigned_magnitude, checked, void> > checked_uint256_t;
  401. typedef number<cpp_int_backend<512, 512, unsigned_magnitude, checked, void> > checked_uint512_t;
  402. typedef number<cpp_int_backend<1024, 1024, unsigned_magnitude, checked, void> > checked_uint1024_t;
  403. // Fixed precision signed types:
  404. typedef number<cpp_int_backend<128, 128, signed_magnitude, checked, void> > checked_int128_t;
  405. typedef number<cpp_int_backend<256, 256, signed_magnitude, checked, void> > checked_int256_t;
  406. typedef number<cpp_int_backend<512, 512, signed_magnitude, checked, void> > checked_int512_t;
  407. typedef number<cpp_int_backend<1024, 1024, signed_magnitude, checked, void> > checked_int1024_t;
  408. }} // namespaces
  409. The `cpp_int_backend` type is normally used via one of the convenience typedefs given above.
  410. This back-end is the "Swiss Army Knife" of integer types as it can represent both fixed and
  411. [@http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic arbitrary precision]
  412. integer types, and both signed and unsigned types. There are five template arguments:
  413. [variablelist
  414. [[MinBits][Determines the number of Bits to store directly within the object before resorting to dynamic memory
  415. allocation. When zero, this field is determined automatically based on how many bits can be stored
  416. in union with the dynamic storage header: setting a larger value may improve performance as larger integer
  417. values will be stored internally before memory allocation is required.]]
  418. [[MaxBits][Determines the maximum number of bits to be stored in the type: resulting in a fixed precision type.
  419. When this value is the same as MinBits, then the Allocator parameter is ignored, as no dynamic
  420. memory allocation will ever be performed: in this situation the Allocator parameter should be set to
  421. type `void`. Note that this parameter should not be used simply to prevent large memory
  422. allocations, not only is that role better performed by the allocator, but fixed precision
  423. integers have a tendency to allocate all of MaxBits of storage more often than one would expect.]]
  424. [[SignType][Determines whether the resulting type is signed or not. Note that for
  425. [@http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic arbitrary precision] types
  426. this parameter must be `signed_magnitude`. For fixed precision
  427. types then this type may be either `signed_magnitude` or `unsigned_magnitude`.]]
  428. [[Checked][This parameter has two values: `checked` or `unchecked`. See below.]]
  429. [[Allocator][The allocator to use for dynamic memory allocation, or type `void` if MaxBits == MinBits.]]
  430. ]
  431. When the template parameter Checked is set to `checked` then the result is a ['checked-integer], checked
  432. and unchecked integers have the following properties:
  433. [table
  434. [[Condition][Checked-Integer][Unchecked-Integer]]
  435. [[Numeric overflow in fixed precision arithmetic][Throws a `std::overflow_error`.][Performs arithmetic modulo 2[super MaxBits]]]
  436. [[Constructing an integer from a value that can not be represented in the target type][Throws a `std::range_error`.]
  437. [Converts the value modulo 2[super MaxBits], signed to unsigned conversions extract the last MaxBits bits of the
  438. 2's complement representation of the input value.]]
  439. [[Unsigned subtraction yielding a negative value.][Throws a `std::range_error`.][Yields the value that would
  440. result from treating the unsigned type as a 2's complement signed type.]]
  441. [[Attempting a bitwise operation on a negative value.][Throws a `std::range_error`][Yields the value, but not the bit pattern,
  442. that would result from performing the operation on a 2's complement integer type.]]
  443. ]
  444. Things you should know when using this type:
  445. * Default constructed `cpp_int_backend`s have the value zero.
  446. * Division by zero results in a `std::overflow_error` being thrown.
  447. * Construction from a string that contains invalid non-numeric characters results in a `std::runtime_error` being thrown.
  448. * Since the precision of `cpp_int_backend` is necessarily limited when the allocator parameter is void,
  449. care should be taken to avoid numeric overflow when using this type
  450. unless you actually want modulo-arithmetic behavior.
  451. * The type uses a sign-magnitude representation internally, so type `int128_t` has 128-bits of precision plus an extra sign bit.
  452. In this respect the behaviour of these types differs from built-in 2's complement types. In might be tempting to use a
  453. 127-bit type instead, and indeed this does work, but behaviour is still slightly different from a 2's complement built-in type
  454. as the min and max values are identical (apart from the sign), where as they differ by one for a true 2's complement type.
  455. That said it should be noted that there's no requirement for built-in types to be 2's complement either - it's simply that this
  456. is the most common format by far.
  457. * Attempting to print negative values as either an Octal or Hexadecimal string results in a `std::runtime_error` being thrown,
  458. this is a direct consequence of the sign-magnitude representation.
  459. * The fixed precision types `[checked_][u]intXXX_t` have expression template support turned off - it seems to make little
  460. difference to the performance of these types either way - so we may as well have the faster compile times by turning
  461. the feature off.
  462. * Unsigned types support subtraction - the result is "as if" a 2's complement operation had been performed as long as they are not
  463. ['checked-integers] (see above).
  464. In other words they behave pretty much as a built in integer type would in this situation. So for example if we were using
  465. `uint128_t` then `uint128_t(1)-4` would result in the value `0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD`
  466. of type `uint128_t`. However, had this operation been performed on `checked_uint128_t` then a `std::range_error` would have
  467. been thrown.
  468. * Unary negation of unsigned types results in a compiler error (static assertion).
  469. * This backend supports rvalue-references and is move-aware, making instantiations of `number` on this backend move aware.
  470. * When used at fixed precision, the size of this type is always one machine word (plus any compiler-applied alignment padding)
  471. larger than you would expect for an N-bit integer:
  472. the extra word stores both the sign, and how many machine words in the integer are actually in use.
  473. The latter is an optimisation for larger fixed precision integers, so that a 1024-bit integer has almost the same performance
  474. characteristics as a 128-bit integer, rather than being 4 times slower for addition and 16 times slower for multiplication
  475. (assuming the values involved would always fit in 128 bits).
  476. Typically this means you can use
  477. an integer type wide enough for the "worst case scenario" with only minor performance degradation even if most of the time
  478. the arithmetic could in fact be done with a narrower type.
  479. Also note that unsigned fixed precision types small enough to fit inside the largest native integer become a simple wrapper around that type,
  480. this includes the "checked" variants. Small signed types will always have an extra sign word and so be larger than their native equivalent.
  481. * When used at fixed precision and MaxBits is smaller than the number of bits in the largest native integer type, then
  482. internally `cpp_int_backend` switches to a "trivial" implementation where it is just a thin wrapper around a single
  483. integer. Note that it will still be slightly slower than a bare native integer, as it emulates a
  484. signed-magnitude representation rather than simply using the platforms native sign representation: this ensures
  485. there is no step change in behavior as a cpp_int grows in size.
  486. * Fixed precision `cpp_int`'s have some support for `constexpr` values and user-defined literals, see
  487. [link boost_multiprecision.tut.lits here] for the full description. For example `0xfffff_cppi1024`
  488. specifies a 1024-bit integer with the value 0xffff. This can be used to generate compile time constants that are
  489. too large to fit into any built in number type.
  490. * The __cpp_int types support constexpr arithmetic, provided it is a fixed precision type with no allocator. It may also
  491. be a checked integer: in which case a compiler error will be generated on overflow or undefined behaviour. In addition
  492. the free functions `abs`, `swap`, `multiply`, `add`, `subtract`, `divide_qr`, `integer_modulus`, `powm`, `lsb`, `msb`,
  493. `bit_test`, `bit_set`, `bit_unset`, `bit_flip`, `sqrt`, `gcd`, `lcm` are all supported. Use of __cpp_int in this way
  494. requires either a C++2a compiler (one which supports `std::is_constant_evaluated()`), or GCC-6 or later in C++14 mode.
  495. Compilers other than GCC and without `std::is_constant_evaluated()` will support a very limited set of operations:
  496. expect to hit roadblocks rather easily.
  497. * You can import/export the raw bits of a __cpp_int to and from external storage via the `import_bits` and `export_bits`
  498. functions. More information is in the [link boost_multiprecision.tut.import_export section on import/export].
  499. [h5:cpp_int_eg Example:]
  500. [cpp_int_eg]
  501. [endsect] [/section:cpp_int cpp_int]
  502. [section:gmp_int gmp_int]
  503. `#include <boost/multiprecision/gmp.hpp>`
  504. namespace boost{ namespace multiprecision{
  505. class gmp_int;
  506. typedef number<gmp_int > mpz_int;
  507. }} // namespaces
  508. The `gmp_int` back-end is used via the typedef `boost::multiprecision::mpz_int`. It acts as a thin wrapper around the [gmp] `mpz_t`
  509. to provide an integer type that is a drop-in replacement for the native C++ integer types, but with unlimited precision.
  510. As well as the usual conversions from arithmetic and string types, type `mpz_int` is copy constructible and assignable from:
  511. * The [gmp] native types: `mpf_t`, `mpz_t`, `mpq_t`.
  512. * Instances of `number<T>` that are wrappers around those types: `number<gmp_float<N> >`, `number<gmp_rational>`.
  513. It's also possible to access the underlying `mpz_t` via the `data()` member function of `gmp_int`.
  514. Things you should know when using this type:
  515. * No changes are made to the GMP library's global settings - so you can safely mix this type with
  516. existing code that uses [gmp].
  517. * Default constructed `gmp_int`s have the value zero (this is GMP's default behavior).
  518. * Formatted IO for this type does not support octal or hexadecimal notation for negative values,
  519. as a result performing formatted output on this type when the argument is negative and either of the flags
  520. `std::ios_base::oct` or `std::ios_base::hex` are set, will result in a `std::runtime_error` will be thrown.
  521. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  522. as a valid integer.
  523. * Division by zero results in a `std::overflow_error` being thrown.
  524. * Although this type is a wrapper around [gmp] it will work equally well with [mpir]. Indeed use of [mpir]
  525. is recommended on Win32.
  526. * This backend supports rvalue-references and is move-aware, making instantiations of `number` on this backend move aware.
  527. [h5 Example:]
  528. [mpz_eg]
  529. [endsect] [/section:gmp_int gmp_int]
  530. [section:tom_int tom_int]
  531. `#include <boost/multiprecision/tommath.hpp>`
  532. namespace boost{ namespace multiprecision{
  533. class tommath_int;
  534. typedef number<tommath_int > tom_int;
  535. }} // namespaces
  536. The `tommath_int` back-end is used via the typedef `boost::multiprecision::tom_int`. It acts as a thin wrapper around the [tommath] `tom_int`
  537. to provide an integer type that is a drop-in replacement for the native C++ integer types, but with unlimited precision.
  538. Things you should know when using this type:
  539. * Default constructed objects have the value zero (this is [tommath]'s default behavior).
  540. * Although `tom_int` is mostly a drop in replacement for the builtin integer types, it should be noted that it is a
  541. rather strange beast as it's a signed type that is not a 2's complement type. As a result the bitwise operations
  542. `| & ^` will throw a `std::runtime_error` exception if either of the arguments is negative. Similarly the complement
  543. operator`~` is deliberately not implemented for this type.
  544. * Formatted IO for this type does not support octal or hexadecimal notation for negative values,
  545. as a result performing formatted output on this type when the argument is negative and either of the flags
  546. `std::ios_base::oct` or `std::ios_base::hex` are set, will result in a `std::runtime_error` will be thrown.
  547. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  548. as a valid integer.
  549. * Division by zero results in a `std::overflow_error` being thrown.
  550. [h5 Example:]
  551. [tommath_eg]
  552. [endsect] [/section:tom_int tom_int]
  553. [section:egs Examples]
  554. [import ../example/integer_examples.cpp]
  555. [section:factorials Factorials]
  556. [FAC1]
  557. [endsect] [/section:factorials Factorials]
  558. [section:bitops Bit Operations]
  559. [BITOPS]
  560. [endsect] [/section:bitops Bit Operations]
  561. [endsect]
  562. [endsect]
  563. [section:floats floating-point Numbers]
  564. The following back-ends provide floating-point arithmetic:
  565. [table
  566. [[Backend Type][Header][Radix][Dependencies][Pros][Cons]]
  567. [[`cpp_bin_float<N>`][boost/multiprecision/cpp_bin_float.hpp][2][None][Header only, all C++ implementation. Boost licence.][Approximately 2x slower than the [mpfr] or [gmp] libraries.]]
  568. [[`cpp_dec_float<N>`][boost/multiprecision/cpp_dec_float.hpp][10][None][Header only, all C++ implementation. Boost licence.][Approximately 2x slower than the [mpfr] or [gmp] libraries.]]
  569. [[`mpf_float<N>`][boost/multiprecision/gmp.hpp][2][[gmp]][Very fast and efficient back-end.][Dependency on GNU licensed [gmp] library.]]
  570. [[`mpfr_float<N>`][boost/multiprecision/mpfr.hpp][2][[gmp] and [mpfr]][Very fast and efficient back-end, with its own standard library implementation.][Dependency on GNU licensed [gmp] and [mpfr] libraries.]]
  571. [[`float128`][boost/multiprecision/float128.hpp][2][Either [quadmath] or the Intel C++ Math library.][Very fast and efficient back-end for 128-bit floating-point values (113-bit mantissa, equivalent to FORTRAN's QUAD real)][Depends on the compiler being either recent GCC or Intel C++ versions.]]
  572. ]
  573. [section:cpp_bin_float cpp_bin_float]
  574. `#include <boost/multiprecision/cpp_bin_float.hpp>`
  575. namespace boost{ namespace multiprecision{
  576. enum digit_base_type
  577. {
  578. digit_base_2 = 2,
  579. digit_base_10 = 10
  580. };
  581. template <unsigned Digits, digit_base_type base = digit_base_10, class Allocator = void, class Exponent = int, ExponentMin = 0, ExponentMax = 0>
  582. class cpp_bin_float;
  583. typedef number<cpp_bin_float<50> > cpp_bin_float_50;
  584. typedef number<cpp_bin_float<100> > cpp_bin_float_100;
  585. typedef number<backends::cpp_bin_float<24, backends::digit_base_2, void, boost::int16_t, -126, 127>, et_off> cpp_bin_float_single;
  586. typedef number<backends::cpp_bin_float<53, backends::digit_base_2, void, boost::int16_t, -1022, 1023>, et_off> cpp_bin_float_double;
  587. typedef number<backends::cpp_bin_float<64, backends::digit_base_2, void, boost::int16_t, -16382, 16383>, et_off> cpp_bin_float_double_extended;
  588. typedef number<backends::cpp_bin_float<113, backends::digit_base_2, void, boost::int16_t, -16382, 16383>, et_off> cpp_bin_float_quad;
  589. typedef number<backends::cpp_bin_float<237, backends::digit_base_2, void, boost::int32_t, -262142, 262143>, et_off> cpp_bin_float_oct;
  590. }} // namespaces
  591. The `cpp_bin_float` back-end is used in conjunction with `number`: It acts as an entirely C++ (header only and dependency free)
  592. floating-point number type that is a drop-in replacement for the native C++ floating-point types, but with
  593. much greater precision.
  594. Type `cpp_bin_float` can be used at fixed precision by specifying a non-zero `Digits` template parameter.
  595. The typedefs `cpp_bin_float_50` and `cpp_bin_float_100` provide arithmetic types at 50 and 100 decimal digits precision
  596. respectively.
  597. Optionally, you can specify whether the precision is specified in decimal digits or binary bits - for example
  598. to declare a `cpp_bin_float` with exactly the same precision as `double` one would use
  599. `number<cpp_bin_float<53, digit_base_2> >`. The typedefs `cpp_bin_float_single`, `cpp_bin_float_double`,
  600. `cpp_bin_float_quad`, `cpp_bin_float_oct` and `cpp_bin_float_double_extended` provide
  601. software analogues of the IEEE single, double, quad and octuple float data types, plus the Intel-extended-double type respectively.
  602. Note that while these types are functionally equivalent to the native IEEE types, but they do not have the same size
  603. or bit-layout as true IEEE compatible types.
  604. Normally `cpp_bin_float` allocates no memory: all of the space required for its digits are allocated
  605. directly within the class. As a result care should be taken not to use the class with too high a digit count
  606. as stack space requirements can grow out of control. If that represents a problem then providing an allocator
  607. as a template parameter causes `cpp_bin_float` to dynamically allocate the memory it needs: this
  608. significantly reduces the size of `cpp_bin_float` and increases the viable upper limit on the number of digits
  609. at the expense of performance. However, please bear in mind that arithmetic operations rapidly become ['very] expensive
  610. as the digit count grows: the current implementation really isn't optimized or designed for large digit counts.
  611. Note that since the actual type of the objects allocated
  612. is completely opaque, the suggestion would be to use an allocator with `void` `value_type`, for example:
  613. `number<cpp_bin_float<1000, digit_base_10, std::allocator<void> > >`.
  614. The final template parameters determine the type and range of the exponent: parameter `Exponent` can be
  615. any signed integer type, but note that `MinExponent` and `MaxExponent` can not go right up to the limits
  616. of the `Exponent` type as there has to be a little extra headroom for internal calculations. You will
  617. get a compile time error if this is the case. In addition if MinExponent or MaxExponent are zero, then
  618. the library will choose suitable values that are as large as possible given the constraints of the type
  619. and need for extra headroom for internal calculations.
  620. There is full standard library and `numeric_limits` support available for this type.
  621. Things you should know when using this type:
  622. * Default constructed `cpp_bin_float`s have a value of zero.
  623. * The radix of this type is 2, even when the precision is specified as decimal digits.
  624. * The type supports both infinities and NaN's. An infinity is generated whenever the result would overflow,
  625. and a NaN is generated for any mathematically undefined operation.
  626. * There is a `std::numeric_limits` specialisation for this type.
  627. * Any `number` instantiated on this type, is convertible to any other `number` instantiated on this type -
  628. for example you can convert from `number<cpp_bin_float<50> >` to `number<cpp_bin_float<SomeOtherValue> >`.
  629. Narrowing conversions round to nearest and are `explicit`.
  630. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  631. as a valid floating-point number.
  632. * All arithmetic operations are correctly rounded to nearest. String conversions and the `sqrt` function
  633. are also correctly rounded, but transcendental functions (sin, cos, pow, exp etc) are not.
  634. [h5 cpp_bin_float example:]
  635. [cpp_bin_float_eg]
  636. [endsect]
  637. [section:cpp_dec_float cpp_dec_float]
  638. `#include <boost/multiprecision/cpp_dec_float.hpp>`
  639. namespace boost{ namespace multiprecision{
  640. template <unsigned Digits10, class ExponentType = boost::int32_t, class Allocator = void>
  641. class cpp_dec_float;
  642. typedef number<cpp_dec_float<50> > cpp_dec_float_50;
  643. typedef number<cpp_dec_float<100> > cpp_dec_float_100;
  644. }} // namespaces
  645. The `cpp_dec_float` back-end is used in conjunction with `number`: It acts as an entirely C++ (header only and dependency free)
  646. floating-point number type that is a drop-in replacement for the native C++ floating-point types, but with
  647. much greater precision.
  648. Type `cpp_dec_float` can be used at fixed precision by specifying a non-zero `Digits10` template parameter.
  649. The typedefs `cpp_dec_float_50` and `cpp_dec_float_100` provide arithmetic types at 50 and 100 decimal digits precision
  650. respectively. Optionally, you can specify an integer type to use for the exponent, this defaults to a 32-bit integer type
  651. which is more than large enough for the vast majority of use cases, but larger types such as `long long` can also be specified
  652. if you need a truly huge exponent range. In any case the ExponentType must be a built in signed integer type at least 2 bytes
  653. and 16-bits wide.
  654. Normally `cpp_dec_float` allocates no memory: all of the space required for its digits are allocated
  655. directly within the class. As a result care should be taken not to use the class with too high a digit count
  656. as stack space requirements can grow out of control. If that represents a problem then providing an allocator
  657. as the final template parameter causes `cpp_dec_float` to dynamically allocate the memory it needs: this
  658. significantly reduces the size of `cpp_dec_float` and increases the viable upper limit on the number of digits
  659. at the expense of performance. However, please bear in mind that arithmetic operations rapidly become ['very] expensive
  660. as the digit count grows: the current implementation really isn't optimized or designed for large digit counts.
  661. There is full standard library and `numeric_limits` support available for this type.
  662. Things you should know when using this type:
  663. * Default constructed `cpp_dec_float`s have a value of zero.
  664. * The radix of this type is 10. As a result it can behave subtly differently from base-2 types.
  665. * The type has a number of internal guard digits over and above those specified in the template argument.
  666. Normally these should not be visible to the user.
  667. * The type supports both infinities and NaN's. An infinity is generated whenever the result would overflow,
  668. and a NaN is generated for any mathematically undefined operation.
  669. * There is a `std::numeric_limits` specialisation for this type.
  670. * Any `number` instantiated on this type, is convertible to any other `number` instantiated on this type -
  671. for example you can convert from `number<cpp_dec_float<50> >` to `number<cpp_dec_float<SomeOtherValue> >`.
  672. Narrowing conversions are truncating and `explicit`.
  673. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  674. as a valid floating-point number.
  675. * The actual precision of a `cpp_dec_float` is always slightly higher than the number of digits specified in
  676. the template parameter, actually how much higher is an implementation detail but is always at least 8 decimal
  677. digits.
  678. * Operations involving `cpp_dec_float` are always truncating. However, note that since their are guard digits
  679. in effect, in practice this has no real impact on accuracy for most use cases.
  680. [h5 cpp_dec_float example:]
  681. [cpp_dec_float_eg]
  682. [endsect]
  683. [section:gmp_float gmp_float]
  684. `#include <boost/multiprecision/gmp.hpp>`
  685. namespace boost{ namespace multiprecision{
  686. template <unsigned Digits10>
  687. class gmp_float;
  688. typedef number<gmp_float<50> > mpf_float_50;
  689. typedef number<gmp_float<100> > mpf_float_100;
  690. typedef number<gmp_float<500> > mpf_float_500;
  691. typedef number<gmp_float<1000> > mpf_float_1000;
  692. typedef number<gmp_float<0> > mpf_float;
  693. }} // namespaces
  694. The `gmp_float` back-end is used in conjunction with `number` : it acts as a thin wrapper around the [gmp] `mpf_t`
  695. to provide an real-number type that is a drop-in replacement for the native C++ floating-point types, but with
  696. much greater precision.
  697. Type `gmp_float` can be used at fixed precision by specifying a non-zero `Digits10` template parameter, or
  698. at variable precision by setting the template argument to zero. The typedefs mpf_float_50, mpf_float_100,
  699. mpf_float_500, mpf_float_1000 provide arithmetic types at 50, 100, 500 and 1000 decimal digits precision
  700. respectively. The typedef mpf_float provides a variable precision type whose precision can be controlled via the
  701. `number`s member functions.
  702. [note This type only provides standard library and `numeric_limits` support when the precision is fixed at compile time.]
  703. As well as the usual conversions from arithmetic and string types, instances of `number<mpf_float<N> >` are
  704. copy constructible and assignable from:
  705. * The [gmp] native types `mpf_t`, `mpz_t`, `mpq_t`.
  706. * The `number` wrappers around those types: `number<mpf_float<M> >`, `number<gmp_int>`, `number<gmp_rational>`.
  707. It's also possible to access the underlying `mpf_t` via the `data()` member function of `gmp_float`.
  708. Things you should know when using this type:
  709. * Default constructed `gmp_float`s have the value zero (this is the [gmp] library's default behavior).
  710. * No changes are made to the [gmp] library's global settings, so this type can be safely mixed with
  711. existing [gmp] code.
  712. * This backend supports rvalue-references and is move-aware, making instantiations of `number` on this backend move aware.
  713. * It is not possible to round-trip objects of this type to and from a string and get back
  714. exactly the same value. This appears to be a limitation of [gmp].
  715. * Since the underlying [gmp] types have no notion of infinities or NaN's, care should be taken
  716. to avoid numeric overflow or division by zero. That latter will result in a std::overflow_error being thrown,
  717. while generating excessively large exponents may result in instability of the underlying [gmp]
  718. library (in testing, converting a number with an excessively large or small exponent
  719. to a string caused [gmp] to segfault).
  720. * This type can equally be used with [mpir] as the underlying implementation - indeed that is
  721. the recommended option on Win32.
  722. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  723. as a valid floating-point number.
  724. * Division by zero results in a `std::overflow_error` being thrown.
  725. [h5 [gmp] example:]
  726. [mpf_eg]
  727. [endsect]
  728. [section:mpfr_float mpfr_float]
  729. `#include <boost/multiprecision/mpfr.hpp>`
  730. namespace boost{ namespace multiprecision{
  731. enum mpfr_allocation_type
  732. {
  733. allocate_stack,
  734. allocate_dynamic
  735. };
  736. template <unsigned Digits10, mpfr_allocation_type AllocateType = allocate_dynamic>
  737. class mpfr_float_backend;
  738. typedef number<mpfr_float_backend<50> > mpfr_float_50;
  739. typedef number<mpfr_float_backend<100> > mpfr_float_100;
  740. typedef number<mpfr_float_backend<500> > mpfr_float_500;
  741. typedef number<mpfr_float_backend<1000> > mpfr_float_1000;
  742. typedef number<mpfr_float_backend<0> > mpfr_float;
  743. typedef number<mpfr_float_backend<50, allocate_stack> > static_mpfr_float_50;
  744. typedef number<mpfr_float_backend<100, allocate_stack> > static_mpfr_float_100;
  745. }} // namespaces
  746. The `mpfr_float_backend` type is used in conjunction with `number`: It acts as a thin wrapper around the [mpfr] `mpfr_t`
  747. to provide an real-number type that is a drop-in replacement for the native C++ floating-point types, but with
  748. much greater precision.
  749. Type `mpfr_float_backend` can be used at fixed precision by specifying a non-zero `Digits10` template parameter, or
  750. at variable precision by setting the template argument to zero. The typedefs mpfr_float_50, mpfr_float_100,
  751. mpfr_float_500, mpfr_float_1000 provide arithmetic types at 50, 100, 500 and 1000 decimal digits precision
  752. respectively. The typedef mpfr_float provides a variable precision type whose precision can be controlled via the
  753. `number`s member functions.
  754. In addition the second template parameter lets you choose between dynamic allocation (the default,
  755. and uses MPFR's normal allocation routines),
  756. or stack allocation (where all the memory required for the underlying data types is stored
  757. within `mpfr_float_backend`). The latter option can result in significantly faster code, at the
  758. expense of growing the size of `mpfr_float_backend`. It can only be used at fixed precision, and
  759. should only be used for lower digit counts. Note that we can not guarantee that using `allocate_stack`
  760. won't cause any calls to mpfr's allocation routines, as mpfr may call these inside it's own code.
  761. The following table gives an idea of the performance tradeoff's at 50 decimal digits
  762. precision[footnote Compiled with VC++10 and /Ox, with MPFR-3.0.0 and MPIR-2.3.0]:
  763. [table
  764. [[Type][Bessel function evaluation, relative times]]
  765. [[`number<mpfr_float_backend<50, allocate_static>, et_on>`][1.0 (5.5s)]]
  766. [[`number<mpfr_float_backend<50, allocate_static>, et_off>`][1.05 (5.8s)]]
  767. [[`number<mpfr_float_backend<50, allocate_dynamic>, et_on>`][1.05 (5.8s)]]
  768. [[`number<mpfr_float_backend<50, allocate_dynamic>, et_off>`][1.16 (6.4s)]]
  769. ]
  770. [note This type only provides `numeric_limits` support when the precision is fixed at compile time.]
  771. As well as the usual conversions from arithmetic and string types, instances of `number<mpfr_float_backend<N> >` are
  772. copy constructible and assignable from:
  773. * The [gmp] native types `mpf_t`, `mpz_t`, `mpq_t`.
  774. * The [mpfr] native type `mpfr_t`.
  775. * The `number` wrappers around those types: `number<mpfr_float_backend<M> >`, `number<mpf_float<M> >`, `number<gmp_int>`, `number<gmp_rational>`.
  776. It's also possible to access the underlying `mpfr_t` via the data() member function of `mpfr_float_backend`.
  777. Things you should know when using this type:
  778. * A default constructed `mpfr_float_backend` is set to zero (['Note that this is [*not] the default [mpfr] behavior]).
  779. * All operations use round to nearest.
  780. * No changes are made to [gmp] or [mpfr] global settings, so this type can coexist with existing
  781. [mpfr] or [gmp] code.
  782. * The code can equally use [mpir] in place of [gmp] - indeed that is the preferred option on Win32.
  783. * This backend supports rvalue-references and is move-aware, making instantiations of `number` on this backend move aware.
  784. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  785. as a valid floating-point number.
  786. * Division by zero results in an infinity.
  787. * When using the variable precision type `mpfr_float`, then copy construction and assignment ['copies the precision
  788. of the source variable]. Likewise move construction and assignment.
  789. * When constructing the variable precision type `mpfr_float` you can specify two arguments to the constructor - the first
  790. is the value to assign to the variable, the second is an unsigned integer specifying the precision in decimal places. The
  791. `assign` member function similarly has a 2-argument overload taking the value to assign and the precision. You can use this
  792. to preserve the precision of the target variable using the somewhat arcane: `a.assign(b, a.precision())`, which assigns `b` to `a`
  793. but preserves the precision of `a`.
  794. [h5 [mpfr] example:]
  795. [mpfr_eg]
  796. [endsect]
  797. [section:float128 float128]
  798. `#include <boost/multiprecision/float128.hpp>`
  799. namespace boost{ namespace multiprecision{
  800. class float128_backend;
  801. typedef number<float128_backend, et_off> float128;
  802. }} // namespaces
  803. The `float128` number type is a very thin wrapper around GCC's `__float128` or Intel's `_Quad` data types
  804. and provides an real-number type that is a drop-in replacement for the native C++ floating-point types, but with
  805. a 113 bit mantissa, and compatible with FORTRAN's 128-bit QUAD real.
  806. All the usual standard library and `numeric_limits` support are available, performance should be equivalent
  807. to the underlying native types: for example the LINPACK benchmarks for GCC's `__float128` and
  808. `boost::multiprecision::float128` both achieved 5.6 MFLOPS[footnote On 64-bit Ubuntu 11.10, GCC-4.8.0, Intel Core 2 Duo T5800.].
  809. As well as the usual conversions from arithmetic and string types, instances of `float128` are
  810. copy constructible and assignable from GCC's `__float128` and Intel's `_Quad` data types.
  811. It's also possible to access the underlying `__float128` or `_Quad` type via the `data()` member
  812. function of `float128_backend`.
  813. Things you should know when using this type:
  814. * Default constructed `float128`s have the value zero.
  815. * This backend supports rvalue-references and is move-aware, making instantiations of `number` on this backend move aware.
  816. * This type is fully `constexpr` aware - basic constexpr arithmetic is supported from C++14 and onwards, comparisons,
  817. plus the functions `fabs`, `abs`, `fpclassify`, `isnormal`, `isfinite`, `isinf` and `isnan` are also supported if either
  818. the compiler implements C++20's `std::is_constant_evaluated()`, or if the compiler is GCC.
  819. * It is not possible to round-trip objects of this type to and from a string and get back
  820. exactly the same value when compiled with Intel's C++ compiler and using `_Quad` as the underlying type: this is a current limitation of
  821. our code. Round tripping when using `__float128` as the underlying type is possible (both for GCC and Intel).
  822. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  823. as a valid floating-point number.
  824. * Division by zero results in an infinity being produced.
  825. * Type `float128` can be used as a literal type (constexpr support).
  826. * Type `float128` can be used for full `constexpr` arithmetic from C++14 and later with GCC. The functions `abs`, `fabs`,
  827. `fpclassify`, `isnan`, `isinf`, `isfinite` and `isnormal` are also `constexpr`, but the transcendental functions are not.
  828. * When using the Intel compiler, the underlying type defaults to `__float128` if it's available and `_Quad` if not. You can override
  829. the default by defining either `BOOST_MP_USE_FLOAT128` or `BOOST_MP_USE_QUAD`.
  830. * When the underlying type is Intel's `_Quad` type, the code must be compiled with the compiler option `-Qoption,cpp,--extended_float_type`.
  831. * When compiling with `gcc`, you need to use the flag `--std=gnu++11/14/17`, as the suffix 'Q' is a GNU extension. Compilation fails with the flag `--std=c++11/14/17`
  832. unless you also use `-fext-numeric-literals`.
  833. [h5 float128 example:]
  834. [float128_eg]
  835. [endsect]
  836. [section:fp_eg Examples]
  837. [import ../example/floating_point_examples.cpp]
  838. [section:aos Area of Circle]
  839. [AOS1]
  840. [AOS2]
  841. [AOS3]
  842. [endsect]
  843. [section:jel Defining a Special Function.]
  844. [JEL]
  845. [endsect]
  846. [section:nd Calculating a Derivative]
  847. [ND1]
  848. [ND2]
  849. [ND3]
  850. [endsect]
  851. [section:gi Calculating an Integral]
  852. [GI1]
  853. [GI2]
  854. [endsect]
  855. [section:poly_eg Polynomial Evaluation]
  856. [POLY]
  857. [endsect] [/section:poly_eg Polynomial Evaluation]
  858. [section:variable_precision Variable Precision Newton Evaluation]
  859. [mpfr_variable]
  860. [endsect]
  861. [endsect] [/section:fp_eg Examples]
  862. [endsect] [/section:floats floating-point Numbers]
  863. [section:interval Interval Number Types]
  864. There is one currently only one interval number type supported - [mpfi].
  865. [section:mpfi mpfi_float]
  866. `#include <boost/multiprecision/mpfi.hpp>`
  867. namespace boost{ namespace multiprecision{
  868. template <unsigned Digits10>
  869. class mpfi_float_backend;
  870. typedef number<mpfi_float_backend<50> > mpfi_float_50;
  871. typedef number<mpfi_float_backend<100> > mpfifloat_100;
  872. typedef number<mpfi_float_backend<500> > mpfifloat_500;
  873. typedef number<mpfi_float_backend<1000> > mpfi_float_1000;
  874. typedef number<mpfi_float_backend<0> > mpfi_float;
  875. }} // namespaces
  876. The `mpfi_float_backend` type is used in conjunction with `number`: It acts as a thin wrapper around the [mpfi] `mpfi_t`
  877. to provide an real-number type that is a drop-in replacement for the native C++ floating-point types, but with
  878. much greater precision and implementing interval arithmetic.
  879. Type `mpfi_float_backend` can be used at fixed precision by specifying a non-zero `Digits10` template parameter, or
  880. at variable precision by setting the template argument to zero. The `typedef`s `mpfi_float_50`, `mpfi_float_100`,
  881. `mpfi_float_500`, `mpfi_float_1000` provide arithmetic types at 50, 100, 500 and 1000 decimal digits precision
  882. respectively. The `typedef mpfi_float` provides a variable precision type whose precision can be controlled via the
  883. `number`s member functions.
  884. [note This type only provides `numeric_limits` support when the precision is fixed at compile time.]
  885. As well as the usual conversions from arithmetic and string types, instances of `number<mpfi_float_backend<N> >` are
  886. copy constructible and assignable from:
  887. * The [mpfi] native type `mpfi_t`.
  888. * The `number` wrappers around [mpfi] or [mpfr]: `number<mpfi_float_backend<M> >` and `number<mpfr_float<M> >`.
  889. * There is a two argument constructor taking two `number<mpfr_float<M> >` arguments specifying the interval.
  890. It's also possible to access the underlying `mpfi_t` via the data() member function of `mpfi_float_backend`.
  891. Things you should know when using this type:
  892. * A default constructed `mpfi_float_backend` is set to zero (['Note that this is [*not] the default [mpfi] behavior]).
  893. * No changes are made to [gmp] or [mpfr] global settings, so this type can coexist with existing
  894. [mpfr] or [gmp] code.
  895. * The code can equally use [mpir] in place of [gmp] - indeed that is the preferred option on Win32.
  896. * This backend supports rvalue-references and is move-aware, making instantiations of `number` on this backend move aware.
  897. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  898. as a valid floating-point number.
  899. * Division by zero results in an infinity.
  900. There are some additional non member functions for working on intervals:
  901. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  902. number<mpfr_float_backend<Digits10>, ExpressionTemplates> lower(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val);
  903. Returns the lower end of the interval.
  904. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  905. number<mpfr_float_backend<Digits10>, ExpressionTemplates> upper(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val);
  906. Returns the upper end of the interval.
  907. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  908. number<mpfr_float_backend<Digits10>, ExpressionTemplates> median(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val);
  909. Returns the mid point of the interval.
  910. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  911. number<mpfr_float_backend<Digits10>, ExpressionTemplates> width(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val);
  912. Returns the absolute width of the interval.
  913. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  914. number<mpfi_float_backend<Digits10>, ExpressionTemplates> intersect(
  915. const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a,
  916. const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b);
  917. Returns the interval which is the intersection of the ['a] and ['b]. Returns an
  918. unspecified empty interval if there is no such intersection.
  919. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  920. number<mpfi_float_backend<Digits10>, ExpressionTemplates> hull(
  921. const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a,
  922. const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b);
  923. Returns the interval which is the union of ['a] and ['b].
  924. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  925. bool overlap(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a,
  926. const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b);
  927. Returns `true` only if the intervals ['a] and ['b] overlap.
  928. template <unsigned Digits10, expression_template_option ExpressionTemplates1, expression_template_option ExpressionTemplates2>
  929. bool in(const number<mpfr_float_backend<Digits10>, ExpressionTemplates1>& a,
  930. const number<mpfi_float_backend<Digits10>, ExpressionTemplates2>& b);
  931. Returns `true` only if point ['a] is contained within the interval ['b].
  932. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  933. bool zero_in(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a);
  934. Returns `true` only if the interval ['a] contains the value zero.
  935. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  936. bool subset(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a,
  937. const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b);
  938. Returns `true` only if ['a] is a subset of ['b].
  939. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  940. bool proper_subset(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a,
  941. const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b);
  942. Returns `true` only if ['a] is a proper subset of ['b].
  943. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  944. bool empty(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a);
  945. Returns `true` only if ['a] is an empty interval, equivalent to `upper(a) < lower(a)`.
  946. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  947. bool singleton(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a);
  948. Returns `true` if `lower(a) == upper(a)`.
  949. [h5 [mpfi] example:]
  950. [mpfi_eg]
  951. [endsect]
  952. [endsect]
  953. [section:complex Complex Number Types]
  954. The following backends provide complex number arithmetic:
  955. [table
  956. [[Backend Type][Header][Radix][Dependencies][Pros][Cons]]
  957. [[`cpp_complex`][boost/multiprecision/cpp_complex.hpp][2][None][An all C++ Boost-licensed implementation.][Slower than [mpc].]]
  958. [[`mpc`][boost/multiprecision/mpc.hpp][2][[mpc]][Very fast and efficient back-end.][Dependency on LGLP-licensed [MPC] library.]]
  959. [[`compplex128`][boost/multiprecision/complex128.hpp][2][`__float128` and libquadmath][Very fast and efficient number type.][128-bit precision only, and resticted to GCC.]]
  960. [[`complex_adaptor`][boost/multiprecision/complex_adaptor.hpp][-][none][Can convert any backend type into a complex number backend.][Not a numbe rin it's own right, and hard to use as a result.]]
  961. ]
  962. [section:cpp_complex cpp_complex]
  963. `#include <boost/multiprecision/cpp_complex.hpp>`
  964. namespace boost{ namespace multiprecision{
  965. template <unsigned Digits, backends::digit_base_type DigitBase = backends::digit_base_10, class Allocator = void, class Exponent = int, Exponent MinExponent = 0, Exponent MaxExponent = 0>
  966. using cpp_complex_backend = complex_adaptor<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinExponent, MaxExponent> >;
  967. template <unsigned Digits, backends::digit_base_type DigitBase = digit_base_10, class Allocator = void, class Exponent = int, Exponent MinExponent = 0, Exponent MaxExponent = 0, expression_template_option ExpressionTemplates = et_off>
  968. using cpp_complex = number<complex_adaptor<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinExponent, MaxExponent> >, ExpressionTemplates>;
  969. typedef cpp_complex<50> cpp_complex_50;
  970. typedef cpp_complex<100> cpp_complex_100;
  971. typedef cpp_complex<24, backends::digit_base_2, void, boost::int16_t, -126, 127> cpp_complex_single;
  972. typedef cpp_complex<53, backends::digit_base_2, void, boost::int16_t, -1022, 1023> cpp_complex_double;
  973. typedef cpp_complex<64, backends::digit_base_2, void, boost::int16_t, -16382, 16383> cpp_complex_extended;
  974. typedef cpp_complex<113, backends::digit_base_2, void, boost::int16_t, -16382, 16383> cpp_complex_quad;
  975. typedef cpp_complex<237, backends::digit_base_2, void, boost::int32_t, -262142, 262143> cpp_complex_oct;
  976. }} // namespaces
  977. The `cpp_complex_backend` back-end is used in conjunction with `number`: It acts as an entirely C++ (header only and dependency free)
  978. complex number type that is a drop-in replacement for `std::complex`, but with much greater precision.
  979. The template alias `cpp_complex` avoids the need to use class `number` directly.
  980. Type `cpp_complex` can be used at fixed precision by specifying a non-zero `Digits` template parameter.
  981. The typedefs `cpp_complex_50` and `cpp_complex_100` provide complex number types at 50 and 100 decimal digits precision
  982. respectively.
  983. Optionally, you can specify whether the precision is specified in decimal digits or binary bits - for example
  984. to declare a `cpp_complex` with exactly the same precision as `std::complex<double>` one would use
  985. `cpp_complex<53, digit_base_2>`. The typedefs `cpp_complex_single`, `cpp_complex_double`,
  986. `cpp_complex_quad`, `cpp_complex_oct` and `cpp_complex_double_extended` provide
  987. software analogues of the IEEE single, double, quad and octuple float data types, plus the Intel-extended-double type respectively.
  988. Note that while these types are functionally equivalent to the native IEEE types, but they do not have the same size
  989. or bit-layout as true IEEE compatible types.
  990. Normally `cpp_complex` allocates no memory: all of the space required for its digits are allocated
  991. directly within the class. As a result care should be taken not to use the class with too high a digit count
  992. as stack space requirements can grow out of control. If that represents a problem then providing an allocator
  993. as a template parameter causes `cpp_complex` to dynamically allocate the memory it needs: this
  994. significantly reduces the size of `cpp_complex` and increases the viable upper limit on the number of digits
  995. at the expense of performance. However, please bear in mind that arithmetic operations rapidly become ['very] expensive
  996. as the digit count grows: the current implementation really isn't optimized or designed for large digit counts.
  997. Note that since the actual type of the objects allocated
  998. is completely opaque, the suggestion would be to use an allocator with `char` `value_type`, for example:
  999. `cpp_complex<1000, digit_base_10, std::allocator<char> >`.
  1000. The next template parameters determine the type and range of the exponent: parameter `Exponent` can be
  1001. any signed integer type, but note that `MinExponent` and `MaxExponent` can not go right up to the limits
  1002. of the `Exponent` type as there has to be a little extra headroom for internal calculations. You will
  1003. get a compile time error if this is the case. In addition if MinExponent or MaxExponent are zero, then
  1004. the library will choose suitable values that are as large as possible given the constraints of the type
  1005. and need for extra headroom for internal calculations.
  1006. Finally, as with class `number`, the final template parameter determines whether expression templates are turn
  1007. on or not. Since by default this type allocates no memory, expression template support is off by default.
  1008. However, you should probably turn it on if you specify an allocator.
  1009. There is full standard library support available for this type, comparable with what `std::complex` provides.
  1010. Things you should know when using this type:
  1011. * Default constructed `cpp_complex`s have a value of zero.
  1012. * The radix of this type is 2, even when the precision is specified as decimal digits.
  1013. * The type supports both infinities and NaN's. An infinity is generated whenever the result would overflow,
  1014. and a NaN is generated for any mathematically undefined operation.
  1015. * There is no `std::numeric_limits` specialisation for this type: this is the same behaviour as `std::complex`. If you need
  1016. `std::numeric_limits` support you need to look at `std::numeric_limits<my_complex_number_type::value_type>`.
  1017. * Any `number` instantiated on this type, is convertible to any other `number` instantiated on this type -
  1018. for example you can convert from `number<cpp_complex<50> >` to `number<cpp_bin_float<SomeOtherValue> >`.
  1019. Narrowing conversions round to nearest and are `explicit`.
  1020. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  1021. as a valid complex number.
  1022. [h5 example:]
  1023. [cpp_complex_eg]
  1024. Which produces the output (for the multiprecision type):
  1025. [cpp_complex_out]
  1026. [endsect]
  1027. [section:mpc_complex mpc_complex]
  1028. `#include <boost/multiprecision/mpc.hpp>`
  1029. namespace boost{ namespace multiprecision{
  1030. template <unsigned Digits10>
  1031. class mpc_complex_backend;
  1032. typedef number<mpc_complex_backend<50> > mpc_complex_50;
  1033. typedef number<mpc_complex_backend<100> > mpc_complex_100;
  1034. typedef number<mpc_complex_backend<500> > mpc_complex_500;
  1035. typedef number<mpc_complex_backend<1000> > mpc_complex_1000;
  1036. typedef number<mpc_complex_backend<0> > mpc_complex;
  1037. }} // namespaces
  1038. The `mpc_complex_backend` type is used in conjunction with `number`: It acts as a thin wrapper around the [mpc] `mpc_t`
  1039. to provide an real-number type that is a drop-in replacement for `std::complex`, but with
  1040. much greater precision.
  1041. Type `mpc_complex_backend` can be used at fixed precision by specifying a non-zero `Digits10` template parameter, or
  1042. at variable precision by setting the template argument to zero. The typedefs mpc_complex_50, mpc_complex_100,
  1043. mpc_complex_500, mpc_complex_1000 provide complex types at 50, 100, 500 and 1000 decimal digits precision
  1044. respectively. The typedef mpc_complex provides a variable precision type whose precision can be controlled via the
  1045. `number`s member functions.
  1046. The `mpc` backend should allow use of the same syntax as the C++ standard library complex type.
  1047. When using this backend, remember to link with the flags `-lmpc -lmpfr -lgmp`.
  1048. As well as the usual conversions from arithmetic and string types, instances of `number<mpc_complex_backend<N> >` are
  1049. copy constructible and assignable from:
  1050. * The [gmp] native types `mpf_t`, `mpz_t`, `mpq_t`.
  1051. * The [mpfr] native type `mpfr_t`.
  1052. * The [mpc] native type `mpc_t`.
  1053. * The `number` wrappers around those types: `number<mpfr_float_backend<M> >`, `number<mpf_float<M> >`, `number<gmp_int>`, `number<gmp_rational>`.
  1054. It's also possible to access the underlying `mpc_t` via the data() member function of `mpfr_float_backend`.
  1055. Things you should know when using this type:
  1056. * A default constructed `mpc_complex_backend` is set to zero (['Note that this is [*not] the default [mpc] behavior]).
  1057. * All operations use round to nearest.
  1058. * No changes are made to [mpc], [gmp] or [mpfr] global settings, so this type can coexist with existing
  1059. [mpc], [mpfr] or [gmp] code.
  1060. * The code can equally use [mpir] in place of [gmp] - indeed that is the preferred option on Win32.
  1061. * This backend supports rvalue-references and is move-aware, making instantiations of `number` on this backend move aware.
  1062. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  1063. as a valid complex number.
  1064. * Division by zero results in a complex-infinity.
  1065. * Unlike `std::complex`, you can not use `reinterpret_cast` to treat this type as an array of the underlying floating point type.
  1066. * Unlike `std::complex`, there are no literals for imaginary values.
  1067. * When using the variable precision type `mpc_complex`, then copy construction and assignment ['copies the precision
  1068. of the source variable]. Likewise move construction and assignment.
  1069. * When constructing the variable precision type `mpc_complex` you can specify two arguments to the constructor - the first
  1070. is the value to assign to the variable, the second is an unsigned integer specifying the precision in decimal places. The
  1071. `assign` member function similarly has a 2-argument overload taking the value to assign and the precision. You can use this
  1072. to preserve the precision of the target variable using the somewhat arcane: `a.assign(b, a.precision())`, which assigns `b` to `a`
  1073. but preserves the precision of `a`.
  1074. [h5 [mpc] example:]
  1075. [mpc_eg]
  1076. Which produces the output (for the multiprecision type):
  1077. [mpc_out]
  1078. [endsect]
  1079. [section:complex128 complex128]
  1080. `#include <boost/multiprecision/complex128.hpp>`
  1081. namespace boost{ namespace multiprecision{
  1082. class complex128_backend;
  1083. typedef number<complex128_backend, et_off> complex128;
  1084. }} // namespaces
  1085. The `complex128` number type is a very thin wrapper around GCC's `__float128` or Intel's `_Quad` data types
  1086. and provides a complex-number type that is a drop-in replacement for the native C++ floating-point types, but with
  1087. a 113 bit mantissa, and compatible with FORTRAN's 128-bit QUAD real.
  1088. All the usual standard library functions are available, performance should be equivalent
  1089. to the underlying native types.
  1090. As well as the usual conversions from arithmetic and string types, instances of `float128` are
  1091. copy constructible and assignable from GCC's `__float128` and Intel's `_Quad` data types.
  1092. Things you should know when using this type:
  1093. * Default constructed `complex128`s have the value zero.
  1094. * This backend supports rvalue-references and is move-aware, making instantiations of `number` on this backend move aware.
  1095. * It is not possible to round-trip objects of this type to and from a string and get back
  1096. exactly the same value when compiled with Intel's C++ compiler and using `_Quad` as the underlying type: this is a current limitation of
  1097. our code. Round tripping when using `__float128` as the underlying type is possible (both for GCC and Intel).
  1098. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be interpreted
  1099. as a valid floating-point number.
  1100. * Division by zero results in an infinity being produced.
  1101. * When using the Intel compiler, the underlying type defaults to `__float128` if it's available and `_Quad` if not. You can override
  1102. the default by defining either `BOOST_MP_USE_FLOAT128` or `BOOST_MP_USE_QUAD`.
  1103. * When the underlying type is Intel's `_Quad` type, the code must be compiled with the compiler option `-Qoption,cpp,--extended_float_type`.
  1104. [h5 complex128 example:]
  1105. [complex128_eg]
  1106. Which results in the output:
  1107. [complex128_out]
  1108. [endsect]
  1109. [section:complex_adaptor complex_adaptor]
  1110. namespace boost{ namespace multiprecision{
  1111. template <class Backend>
  1112. struct complex_adaptor;
  1113. }}
  1114. Class template `complex_adaptor` is designed to sit inbetween class `number` and an actual floating point backend,
  1115. in order to create a new complex number type.
  1116. It is the means by which we implement __cpp_complex and __complex128.
  1117. [endsect]
  1118. [endsect]
  1119. [section:rational Rational Number Types]
  1120. The following back-ends provide rational number arithmetic:
  1121. [table
  1122. [[Backend Type][Header][Radix][Dependencies][Pros][Cons]]
  1123. [[`cpp_rational`][boost/multiprecision/cpp_int.hpp][2][None][An all C++ Boost-licensed implementation.][Slower than [gmp].]]
  1124. [[`gmp_rational`][boost/multiprecision/gmp.hpp][2][[gmp]][Very fast and efficient back-end.][Dependency on GNU licensed [gmp] library.]]
  1125. [[`tommath_rational`][boost/multiprecision/tommath.hpp][2][[tommath]][All C/C++ implementation that's Boost Software Licence compatible.][Slower than [gmp].]]
  1126. [[`rational_adaptor`][boost/multiprecision/rational_adaptor.hpp][N/A][none][All C++ adaptor that allows any integer back-end type to be used as a rational type.][Requires an underlying integer back-end type.]]
  1127. [[`boost::rational`][boost/rational.hpp][N/A][None][A C++ rational number type that can used with any `number` integer type.][The expression templates used by `number` end up being "hidden" inside `boost::rational`: performance may well suffer as a result.]]
  1128. ]
  1129. [section:cpp_rational cpp_rational]
  1130. `#include <boost/multiprecision/cpp_int.hpp>`
  1131. namespace boost{ namespace multiprecision{
  1132. typedef rational_adaptor<cpp_int_backend<> > cpp_rational_backend;
  1133. typedef number<cpp_rational_backend> cpp_rational;
  1134. }} // namespaces
  1135. The `cpp_rational_backend` type is used via the typedef `boost::multiprecision::cpp_rational`. It provides
  1136. a rational number type that is a drop-in replacement for the native C++ number types, but with unlimited precision.
  1137. As well as the usual conversions from arithmetic and string types, instances of `cpp_rational` are copy constructible
  1138. and assignable from type `cpp_int`.
  1139. There is also a two argument constructor that accepts a numerator and denominator: both of type `cpp_int`.
  1140. There are also non-member functions:
  1141. cpp_int numerator(const cpp_rational&);
  1142. cpp_int denominator(const cpp_rational&);
  1143. which return the numerator and denominator of the number.
  1144. Things you should know when using this type:
  1145. * Default constructed `cpp_rational`s have the value zero.
  1146. * Division by zero results in a `std::overflow_error` being thrown.
  1147. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be
  1148. interpreted as a valid rational number.
  1149. [h5 Example:]
  1150. [cpp_rational_eg]
  1151. [endsect]
  1152. [section:gmp_rational gmp_rational]
  1153. `#include <boost/multiprecision/gmp.hpp>`
  1154. namespace boost{ namespace multiprecision{
  1155. class gmp_rational;
  1156. typedef number<gmp_rational > mpq_rational;
  1157. }} // namespaces
  1158. The `gmp_rational` back-end is used via the typedef `boost::multiprecision::mpq_rational`. It acts as a thin wrapper around the [gmp] `mpq_t`
  1159. to provide a rational number type that is a drop-in replacement for the native C++ number types, but with unlimited precision.
  1160. As well as the usual conversions from arithmetic and string types, instances of `number<gmp_rational>` are copy constructible
  1161. and assignable from:
  1162. * The [gmp] native types: `mpz_t`, `mpq_t`.
  1163. * `number<gmp_int>`.
  1164. There is also a two-argument constructor that accepts a numerator and denominator (both of type `number<gmp_int>`).
  1165. There are also non-member functions:
  1166. mpz_int numerator(const mpq_rational&);
  1167. mpz_int denominator(const mpq_rational&);
  1168. which return the numerator and denominator of the number.
  1169. It's also possible to access the underlying `mpq_t` via the `data()` member function of `mpq_rational`.
  1170. Things you should know when using this type:
  1171. * Default constructed `mpq_rational`s have the value zero (this is the [gmp] default behavior).
  1172. * Division by zero results in a `std::overflow_error` being thrown.
  1173. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be
  1174. interpreted as a valid rational number.
  1175. * No changes are made to the [gmp] library's global settings, so this type can coexist with existing
  1176. [gmp] code.
  1177. * The code can equally be used with [mpir] as the underlying library - indeed that is the preferred option on Win32.
  1178. [h5 Example:]
  1179. [mpq_eg]
  1180. [endsect]
  1181. [section:tommath_rational tommath_rational]
  1182. `#include <boost/multiprecision/tommath.hpp>`
  1183. namespace boost{ namespace multiprecision{
  1184. typedef rational_adpater<tommath_int> tommath_rational;
  1185. typedef number<tommath_rational > tom_rational;
  1186. }} // namespaces
  1187. The `tommath_rational` back-end is used via the typedef `boost::multiprecision::tom_rational`. It acts as a thin wrapper around
  1188. `boost::rational<tom_int>`
  1189. to provide a rational number type that is a drop-in replacement for the native C++ number types, but with unlimited precision.
  1190. The advantage of using this type rather than `boost::rational<tom_int>` directly, is that it is expression-template enabled,
  1191. greatly reducing the number of temporaries created in complex expressions.
  1192. There are also non-member functions:
  1193. tom_int numerator(const tom_rational&);
  1194. tom_int denominator(const tom_rational&);
  1195. which return the numerator and denominator of the number.
  1196. Things you should know when using this type:
  1197. * Default constructed `tom_rational`s have the value zero (this the inherited Boost.Rational behavior).
  1198. * Division by zero results in a `std::overflow_error` being thrown.
  1199. * Conversion from a string results in a `std::runtime_error` being thrown if the string can not be
  1200. interpreted as a valid rational number.
  1201. * No changes are made to [tommath]'s global state, so this type can safely coexist with other [tommath] code.
  1202. * Performance of this type has been found to be pretty poor - this need further investigation - but it appears that Boost.Rational
  1203. needs some improvement in this area.
  1204. [h5 Example:]
  1205. [mp_rat_eg]
  1206. [endsect]
  1207. [section:br Use With Boost.Rational]
  1208. All of the integer types in this library can be used as template arguments to `boost::rational<IntType>`.
  1209. Note that using the library in this way largely negates the effect of the expression templates in `number`.
  1210. [endsect]
  1211. [section:rational_adaptor rational_adaptor]
  1212. namespace boost{ namespace multiprecision{
  1213. template <class IntBackend>
  1214. class rational_adpater;
  1215. }}
  1216. The class template `rational_adaptor` is a back-end for `number` which converts any existing integer back-end
  1217. into a rational-number back-end.
  1218. So for example, given an integer back-end type `MyIntegerBackend`, the use would be something like:
  1219. typedef number<MyIntegerBackend> MyInt;
  1220. typedef number<rational_adaptor<MyIntegerBackend> > MyRational;
  1221. MyRational r = 2;
  1222. r /= 3;
  1223. MyInt i = numerator(r);
  1224. assert(i == 2);
  1225. [endsect]
  1226. [endsect]
  1227. [section:misc Miscellaneous Number Types.]
  1228. Backend types listed in this section are predominantly designed to aid debugging.
  1229. [section:logged_adaptor logged_adaptor]
  1230. `#include <boost/multiprecision/logged_adaptor.hpp>`
  1231. namespace boost{ namespace multiprecision{
  1232. template <class Backend>
  1233. void log_postfix_event(const Backend& result, const char* event_description);
  1234. template <class Backend, class T>
  1235. void log_postfix_event(const Backend& result1, const T& result2, const char* event_description);
  1236. template <class Backend>
  1237. void log_prefix_event(const Backend& arg1, const char* event_description);
  1238. template <class Backend, class T>
  1239. void log_prefix_event(const Backend& arg1, const T& arg2, const char* event_description);
  1240. template <class Backend, class T, class U>
  1241. void log_prefix_event(const Backend& arg1, const T& arg2, const U& arg3, const char* event_description);
  1242. template <class Backend, class T, class U, class V>
  1243. void log_prefix_event(const Backend& arg1, const T& arg2, const U& arg3, const V& arg4, const char* event_description);
  1244. template <Backend>
  1245. class logged_adaptor;
  1246. }} // namespaces
  1247. The `logged_adaptor` type is used in conjunction with `number` and some other backend type: it acts as a thin wrapper around
  1248. some other backend to class `number` and logs all the events that take place on that object. Before any number operation takes
  1249. place, it calls `log_prefix_event` with the arguments to the operation (up to 4), plus a string describing the operation.
  1250. Then after the operation it calls `log_postfix_event` with the result of the operation, plus a string describing the operation.
  1251. Optionally, `log_postfix_event` takes a second result argument: this occurs when the result of the operation is not a `number`,
  1252. for example when `fpclassify` is called, `log_postfix_event` will be called with `result1` being the argument to the function, and
  1253. `result2` being the integer result of `fpclassify`.
  1254. The default versions of `log_prefix_event` and `log_postfix_event` do nothing, it is therefore up to the user to overload these
  1255. for the particular backend being observed.
  1256. This type provides `numeric_limits` support whenever the template argument Backend does so.
  1257. This type is particularly useful when combined with an interval number type - in this case we can use `log_postfix_event`
  1258. to monitor the error accumulated after each operation. We could either set some kind of trap whenever the accumulated error
  1259. exceeds some threshold, or simply print out diagnostic information. Using this technique we can quickly locate the cause of
  1260. numerical instability in a particular routine. The following example demonstrates this technique in a trivial algorithm
  1261. that deliberately introduces cancellation error:
  1262. [logged_adaptor]
  1263. When we examine program output we can clearly see that the diameter of the interval increases after each subtraction:
  1264. [logged_adaptor_output]
  1265. [endsect]
  1266. [section:debug_adaptor debug_adaptor]
  1267. `#include <boost/multiprecision/debug_adaptor.hpp>`
  1268. namespace boost{ namespace multiprecision{
  1269. template <Backend>
  1270. class debug_adaptor;
  1271. }} // namespaces
  1272. The `debug_adaptor` type is used in conjunction with `number` and some other backend type: it acts as a thin wrapper around
  1273. some other backend to class `number` and intercepts all operations on that object storing the result as a string within itself.
  1274. This type provides `numeric_limits` support whenever the template argument Backend does so.
  1275. This type is particularly useful when your debugger provides a good view of `std::string`: when this is the case
  1276. multiprecision values can easily be inspected in the debugger by looking at the `debug_value` member of `debug_adaptor`.
  1277. The down side of this approach is that runtimes are much slower when using this type. Set against that it can make
  1278. debugging very much easier, certainly much easier than sprinkling code with `printf` statements.
  1279. When used in conjunction with the Visual C++ debugger visualisers, the value of a multiprecision type that uses this
  1280. backend is displayed in the debugger just a builtin value would be, here we're inspecting a value of type
  1281. `number<debug_adaptor<cpp_dec_float<50> > >`:
  1282. [$../debugger1.png]
  1283. Otherwise you will need to expand out the view and look at the "debug_value" member:
  1284. [$../debugger2.png]
  1285. It works for all the backend types equally too, here it is inspecting a `number<debug_adaptor<gmp_rational> >`:
  1286. [$../debugger3.png]
  1287. [endsect]
  1288. [section:visualizers Visual C++ Debugger Visualizers]
  1289. Let's face it debugger multiprecision numbers is hard - simply because we can't easily inspect the value of the numbers.
  1290. Visual C++ provides a partial solution in the shape of "visualizers" which provide improved views of complex data structures,
  1291. these visualizers need to be added to the `[Visualizer]` section of `autoexp.dat` located in the `Common7/Packages/Debugger`
  1292. directory of your Visual Studio installation. The actual visualizer code is in the sandbox
  1293. [@https://svn.boost.org/svn/boost/sandbox/boost_docs/subprojects/DebuggerVisualizers/multiprecision.vis.txt here] - just cut and paste the code
  1294. into your `autoexp.dat` file.
  1295. [note These visualizers have only been tested with VC10, also given the ability of buggy visualizers to crash your Visual C++
  1296. debugger, make sure you back up `autoexp.dat` file before using these!!]
  1297. The first visualizer provides improved views of `debug_adaptor`:
  1298. [$../debugger1.png]
  1299. The next visualizer provides improved views of cpp_int: small numbers are displayed as actual values, while larger numbers are
  1300. displayed as an array of hexadecimal parts, with the most significant part first.
  1301. Here's what it looks like for small values:
  1302. [$../debugger4.png]
  1303. And for larger values:
  1304. [$../debugger5.png]
  1305. There is also a `~raw` child member that
  1306. lets you see the actual members of the class:
  1307. [$../debugger6.png]
  1308. The visualizer for `cpp_dec_float` shows the first few digits of the value in the preview field, and the full array of digits
  1309. when you expand the view. As before the `~raw` child gives you access to the actual data members:
  1310. [$../debugger7.png]
  1311. [endsect]
  1312. [endsect]
  1313. [section:conversions Constructing and Interconverting Between Number Types]
  1314. All of the number types that are based on `number` have certain conversion rules in common.
  1315. In particular:
  1316. * Any number type can be constructed (or assigned) from any builtin arithmetic type, as long
  1317. as the conversion isn't lossy (for example float to int conversion):
  1318. cpp_dec_float_50 df(0.5); // OK construction from double
  1319. cpp_int i(450); // OK constructs from signed int
  1320. cpp_int j = 3.14; // Error, lossy conversion.
  1321. * A number can be explicitly constructed from an arithmetic type, even when the conversion is lossy:
  1322. cpp_int i(3.14); // OK explicit conversion
  1323. i = static_cast<cpp_int>(3.14) // OK explicit conversion
  1324. i.assign(3.14); // OK, explicit assign and avoid a temporary from the cast above
  1325. i = 3.14; // Error, no implicit assignment operator for lossy conversion.
  1326. cpp_int j = 3.14; // Error, no implicit constructor for lossy conversion.
  1327. * A `number` can be converted to any built in type, via the `convert_to` member function:
  1328. mpz_int z(2);
  1329. int i = z.convert_to<int>(); // sets i to 2
  1330. * Conversions to rational numbers from floating-point ones are always allowed, and are exact and implicit
  1331. as long as the rational number uses an unbounded integer type. Please be aware that constructing a rational
  1332. number from an extended precision floating-point type with a large exponent range can effectively run the system
  1333. out of memory, as in the extreme case ['2[super max_exponent] / CHAR_BITS] bytes of storage may be required. This
  1334. does not represent a problem for built in floating-point types however, as the exponent range for these is rather
  1335. limited.
  1336. * Conversions to floating-point numbers from rational ones are rounded to nearest (less than 0.5ulp error)
  1337. as long as the floating-point number is binary, and the integer type used by the rational number is unbounded.
  1338. Additional conversions may be supported by particular backends.
  1339. * A `number` can be converted to any built in type, via an explicit conversion operator:
  1340. this functionality is only available on compilers supporting C++11's explicit conversion syntax.
  1341. mpz_int z(2);
  1342. int i = z; // Error, implicit conversion not allowed.
  1343. int j = static_cast<int>(z); // OK explicit conversion.
  1344. * Any number type can be ['explicitly] constructed (or assigned) from a `const char*` or a `std::string`:
  1345. // pi to 50 places from a string:
  1346. cpp_dec_float_50 df("3.14159265358979323846264338327950288419716939937510");
  1347. // Integer type will automatically detect "0x" and "0" prefixes and parse the string accordingly:
  1348. cpp_int i("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000");
  1349. // Invalid input always results in a std::runtime_error being thrown:
  1350. i = static_cast<cpp_int>("3.14");
  1351. // implicit conversions from strings are not allowed:
  1352. i = "23"; // Error, no assignment operator for implicit conversion from string
  1353. // assign member function, avoids having to create a temporary via a static_cast:
  1354. i.assign("23"); // OK
  1355. * Any number type will interoperate with the builtin types in arithmetic expressions as long as the conversions
  1356. are not lossy:
  1357. // pi to 50 places from a string:
  1358. cpp_dec_float_50 df = "3.14159265358979323846264338327950288419716939937510";
  1359. // Multiply by 2 - using an integer literal here is usually more efficient
  1360. // than constructing a temporary:
  1361. df *= 2;
  1362. // You can't mix integer types with floats though:
  1363. cpp_int i = 2;
  1364. i *= 3.14; // Error, no *= operator will be found.
  1365. * Any number type can be streamed to and from the C++ iostreams:
  1366. cpp_dec_float_50 df = "3.14159265358979323846264338327950288419716939937510";
  1367. // Now print at full precision:
  1368. std::cout << std::setprecision(std::numeric_limits<cpp_dec_float_50>::max_digits10)
  1369. << df << std::endl
  1370. cpp_int i = 1;
  1371. i <<= 256;
  1372. // Now print in hex format with prefix:
  1373. std::cout << std::hex << std::showbase << i << std::endl;
  1374. * Interconversions between number types of the same family are allowed and are implicit conversions if no
  1375. loss of precision is involved, and explicit if it is:
  1376. int128_t i128 = 0;
  1377. int266_t i256 = i128; // OK implicit widening conversion
  1378. i128_t = i256; // Error, no assignment operator found, narrowing conversion is explicit.
  1379. i128_t = static_cast<int128_t>(i256); // OK, explicit narrowing conversion.
  1380. mpz_int z = 0;
  1381. mpf_float f = z; // OK, GMP handles this conversion natively, and it's not lossy and therefore implicit.
  1382. mpf_float_50 f50 = 2;
  1383. f = f50; // OK, conversion from fixed to variable precision, f will have 50 digits precision.
  1384. f50 = f; // Error, conversion from variable to fixed precision is potentially lossy, explicit cast required.
  1385. * Some interconversions between number types are completely generic, and are always available, albeit the conversions are always ['explicit]:
  1386. cpp_int cppi(2);
  1387. // We can always convert between numbers of the same category -
  1388. // int to int, rational to rational, or float to float, so this is OK
  1389. // as long as we use an explicit conversion:
  1390. mpz_int z(cppi);
  1391. // We can always promote from int to rational, int to float, or rational to float:
  1392. cpp_rational cppr(cppi); // OK, int to rational
  1393. cpp_dec_float_50 df(cppi); // OK, int to float
  1394. df = static_cast<cpp_dec_float_50>(cppr); // OK, explicit rational to float conversion
  1395. // However narrowing and/or implicit conversions always fail:
  1396. cppi = df; // Compiler error, conversion not allowed
  1397. * Other interconversions may be allowed as special cases, whenever the backend allows it:
  1398. mpf_t m; // Native GMP type.
  1399. mpf_init_set_ui(m, 0); // set to a value;
  1400. mpf_float i(m); // copies the value of the native type.
  1401. More information on what additional types a backend supports conversions from are given in the tutorial for each backend.
  1402. The converting constructor will be implicit if the backend's converting constructor is also implicit, and explicit if the
  1403. backends converting constructor is also explicit.
  1404. [endsect]
  1405. [section:random Generating Random Numbers]
  1406. Random numbers are generated in conjunction with Boost.Random.
  1407. There is a single generator that supports generating random integers with large bit counts:
  1408. [@http://www.boost.org/doc/html/boost/random/independent_bits_engine.html `independent_bits_engine`].
  1409. This type can be used with either ['unbounded] integer types, or with ['bounded] (ie fixed precision) unsigned integers:
  1410. [random_eg1]
  1411. Program output is:
  1412. [random_eg1_out]
  1413. In addition, the generator adaptors [@http://www.boost.org/doc/html/boost/random/discard_block_engine.html `discard_block`],
  1414. [@http://www.boost.org/doc/html/boost/random/xor_combine_engine.html `xor_combine_engine`] and
  1415. [@http://www.boost.org/doc/html/boost/random/discrete_distribution.html `discrete_distribution`] can be used
  1416. with multiprecision types. Note that if you seed an `independent_bits_engine`, then you are actually seeding
  1417. the underlying generator, and should therefore provide a sequence of unsigned 32-bit values as the seed.
  1418. Alternatively we can generate integers in a given range using
  1419. [@http://www.boost.org/doc/html/boost/random/uniform_int_distribution.html `uniform_int_distribution`], this will
  1420. invoke the underlying engine multiple times to build up the required number of bits in the result:
  1421. [random_eg2]
  1422. [random_eg2_out]
  1423. It is also possible to use [@http://www.boost.org/doc/html/boost/random/uniform_int_distribution.html `uniform_int_distribution`]
  1424. with a multiprecision generator such as [@http://www.boost.org/doc/html/boost/random/independent_bits_engine.html `independent_bits_engine`].
  1425. Or to use [@http://www.boost.org/doc/html/boost/random/uniform_smallint.html `uniform_smallint`] or
  1426. [@http://www.boost.org/doc/html/boost/random/random_number_generator.html `random_number_generator`] with multiprecision types.
  1427. floating-point values in \[0,1) are most easily generated using [@http://www.boost.org/doc/html/boost/random/generate_canonical.html `generate_canonical`],
  1428. note that `generate_canonical` will call the generator multiple times to produce the requested number of bits, for example we can use
  1429. it with a regular generator like so:
  1430. [random_eg3]
  1431. [random_eg3_out]
  1432. Note however, the distributions do not invoke the generator multiple times to fill up the mantissa of a multiprecision floating-point type
  1433. with random bits. For these therefore, we should probably use a multiprecision generator (ie `independent_bits_engine`) in combination
  1434. with the distribution:
  1435. [random_eg4]
  1436. [random_eg4_out]
  1437. And finally, it is possible to use the floating-point generators [@http://www.boost.org/doc/html/boost/random/lagged_fibonacci_01_engine.html `lagged_fibonacci_01_engine`]
  1438. and [@http://www.boost.org/doc/html/boost/random/subtract_with_idp144360752.html `subtract_with_carry_01_engine`] directly with multiprecision floating-point types.
  1439. It's worth noting however, that there is a distinct lack of literature on generating high bit-count random numbers, and therefore a lack of "known good" parameters to
  1440. use with these generators in this situation. For this reason, these should probably be used for research purposes only:
  1441. [random_eg5]
  1442. [endsect]
  1443. [section:primetest Primality Testing]
  1444. The library implements a Miller-Rabin test for primality:
  1445. #include <boost/multiprecision/miller_rabin.hpp>
  1446. template <class Backend, expression_template_option ExpressionTemplates, class Engine>
  1447. bool miller_rabin_test(const number<Backend, ExpressionTemplates>& n, unsigned trials, Engine& gen);
  1448. template <class Backend, expression_template_option ExpressionTemplates, class Engine>
  1449. bool miller_rabin_test(const number<Backend, ExpressionTemplates>& n, unsigned trials);
  1450. These functions perform a Miller-Rabin test for primality, if the result is `false` then /n/ is definitely composite,
  1451. while if the result is true then n is probably prime. The probability to declare a composite n as probable prime is
  1452. at most 0.25[super trials]. Note that this does not allow a statement about the probability of n being actually
  1453. prime (for that, the prior probability would have to be known). The algorithm used performs some
  1454. trial divisions to exclude small prime factors, does one Fermat test to exclude many more composites, and then
  1455. uses the Miller-Rabin algorithm straight out of
  1456. Knuth Vol 2, which recommends 25 trials for a pretty strong likelihood that /n/ is prime.
  1457. The third optional argument is for a Uniform Random Number Generator from Boost.Random. When not provided the `mt19937`
  1458. generator is used. Note that when producing random primes then you should probably use a different random number generator
  1459. to produce candidate prime numbers for testing, than is used internally by `miller_rabin_test` for determining
  1460. whether the value is prime. It also helps of course to seed the generators with some source of randomness.
  1461. The following example searches for a prime `p` for which `(p-1)/2` is also probably prime:
  1462. [safe_prime]
  1463. [endsect]
  1464. [section:lits Literal Types and `constexpr` Support]
  1465. There are two kinds of `constexpr` support in this library:
  1466. * The more basic version requires only C++11 and allow the construction of some number types as literals.
  1467. * The more advanced support permits constexpr arithmetic and requires at least C++14
  1468. constexpr support, and for many operations C++2a support
  1469. [h4 Declaring numeric literals]
  1470. There are two backend types which are literals:
  1471. * __float128 (which requires GCC), and
  1472. * Instantiations of `cpp_int_backend` where the Allocator parameter is type `void`.
  1473. In addition, prior to C++14 the Checked parameter must be `boost::multiprecision::unchecked`.
  1474. For example:
  1475. using namespace boost::multiprecision;
  1476. constexpr float128 f = 0.1Q // OK, float128's are always literals in C++11
  1477. constexpr int128_t i = 0; // OK, fixed precision int128_t has no allocator.
  1478. constexpr uint1024_t j = 0xFFFFFFFF00000000uLL; // OK, fixed precision uint1024_t has no allocator.
  1479. constexpr checked_uint128_t k = 1; // OK from C++14 and later, not supported for C++11.
  1480. constexpr checked_uint128_t k = -1; // Error, as this would normally lead to a runtime failure (exception).
  1481. constexpr cpp_int l = 2; // Error, type is not a literal as it performs memory management.
  1482. There is also support for user defined-literals with __cpp_int - these are limited to unchecked, fixed precision `cpp_int`'s
  1483. which are specified in hexadecimal notation. The suffixes supported are:
  1484. [table
  1485. [[Suffix][Meaning]]
  1486. [[_cppi][Specifies a value of type: `number<cpp_int_backend<N,N,signed_magnitude,unchecked,void> >`, where N is chosen
  1487. to contain just enough digits to hold the number specified.]]
  1488. [[_cppui][Specifies a value of type: `number<cpp_int_backend<N,N,unsigned_magnitude,unchecked,void> >`, where N is chosen
  1489. to contain just enough digits to hold the number specified.]]
  1490. [[_cppi['N]][Specifies a value of type `number<cpp_int_backend<N,N,signed_magnitude,unchecked,void> >`.]]
  1491. [[_cppui['N]][Specifies a value of type `number<cpp_int_backend<N,N,signed_magnitude,unchecked,void> >`.]]
  1492. ]
  1493. In each case, use of these suffixes with hexadecimal values produces a `constexpr` result.
  1494. Examples:
  1495. //
  1496. // Any use of user defined literals requires that we import the literal-operators
  1497. // into current scope first:
  1498. using namespace boost::multiprecision::literals;
  1499. //
  1500. // To keep things simple in the example, we'll make our types used visible to this scope as well:
  1501. using namespace boost::multiprecision;
  1502. //
  1503. // The value zero as a number<cpp_int_backend<4,4,signed_magnitude,unchecked,void> >:
  1504. constexpr auto a = 0x0_cppi;
  1505. // The type of each constant has 4 bits per hexadecimal digit,
  1506. // so this is of type uint256_t (ie number<cpp_int_backend<256,256,unsigned_magnitude,unchecked,void> >):
  1507. constexpr auto b = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui;
  1508. //
  1509. // Smaller values can be assigned to larger values:
  1510. int256_t c = 0x1234_cppi; // OK
  1511. //
  1512. // However, this only works in constexpr contexts from C++14 onwards:
  1513. constexpr int256_t d = 0x1_cppi; // Compiler error in C++11, requires C++14
  1514. //
  1515. // Constants can be padded out with leading zeros to generate wider types:
  1516. constexpr uint256_t e = 0x0000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFF_cppui; // OK
  1517. //
  1518. // However, specific width types are best produced with specific-width suffixes,
  1519. // ones supported by default are `_cpp[u]i128`, `_cpp[u]i256`, `_cpp[u]i512`, `_cpp[u]i1024`.
  1520. //
  1521. constexpr int128_t f = 0x1234_cppi128; // OK, always produces an int128_t as the result.
  1522. constexpr uint1024_t g = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccc_cppui1024;
  1523. //
  1524. // If other specific width types are required, then there is a macro for generating the operators
  1525. // for these. The macro can be used at namespace scope only:
  1526. //
  1527. BOOST_MP_DEFINE_SIZED_CPP_INT_LITERAL(2048);
  1528. //
  1529. // Now we can create 2048-bit literals as well:
  1530. constexpr auto h = 0xff_cppi2048; // h is of type number<cpp_int_backend<2048,2048,signed_magnitude,unchecked,void> >
  1531. //
  1532. // Finally negative values are handled via the unary minus operator:
  1533. //
  1534. constexpr int1024_t i = -0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui1024;
  1535. //
  1536. // Which means this also works:
  1537. constexpr int1024_t j = -g; // OK: unary minus operator is constexpr.
  1538. [h4 constexpr arithmetic]
  1539. The front end of the library is all `constexpr` from C++14 and later. Currently there are only two
  1540. back end types that are `constexpr` aware: __float128 and __cpp_int. More backends will follow at a later date.
  1541. Provided the compiler is GCC, type __float128 support `constexpr` operations on all arithmetic operations from C++14, comparisons,
  1542. `abs`, `fabs`, `fpclassify`, `isnan`, `isinf`, `isfinite` and `isnormal` are also fully supported, but the transcendental functions are not.
  1543. The __cpp_int types support constexpr arithmetic, provided it is a fixed precision type with no allocator. It may also
  1544. be a checked integer: in which case a compiler error will be generated on overflow or undefined behaviour. In addition
  1545. the free functions `abs`, `swap`, `multiply`, `add`, `subtract`, `divide_qr`, `integer_modulus`, `powm`, `lsb`, `msb`,
  1546. `bit_test`, `bit_set`, `bit_unset`, `bit_flip`, `sqrt`, `gcd`, `lcm` are all supported. Use of __cpp_int in this way
  1547. requires either a C++2a compiler (one which supports `std::is_constant_evaluated()` - currently only gcc-9 or clang-9 or later),
  1548. or GCC-6 or later in C++14 mode.
  1549. Compilers other than GCC and without `std::is_constant_evaluated()` will support a very limited set of operations:
  1550. expect to hit roadblocks rather easily.
  1551. For example given:
  1552. [constexpr_circle]
  1553. We can now calculate areas and circumferences using all constexpr arithmetic:
  1554. [constexpr_circle_usage]
  1555. Note that these make use of the numeric constants from the Math library, which also happen to be `constexpr`.
  1556. For a more interesting example, in [@../../example/constexpr_float_arithmetic_examples.cpp constexpr_float_arithmetic_examples.cpp]
  1557. we define a simple class for `constexpr` polynomial arithmetic:
  1558. template <class T, unsigned Order>
  1559. struct const_polynomial;
  1560. Given this, we can use recurrence relations to calculate the coefficients for various orthogonal
  1561. polynomials - in the example we use the Hermite polynomials, only the constructor does any work -
  1562. it uses the recurrence relations to calculate the coefficient array:
  1563. [hermite_example]
  1564. Now we just need to define H[sub 0] and H[sub 1] as termination conditions for the recurrence:
  1565. [hermite_example2]
  1566. We can now declare H[sub 9] as a constexpr object, access the coefficients, and evaluate
  1567. at an abscissa value, all using `constexpr` arithmetic:
  1568. [hermite_example3]
  1569. Also since the coefficients to the Hermite polynomials are integers, we can also generate the Hermite
  1570. coefficients using (fixed precision) cpp_int's: see [@../../test/constexpr_test_cpp_int_6.cpp constexpr_test_cpp_int_6.cpp].
  1571. We can also generate factorials (and validate the result) like so:
  1572. [factorial_decl]
  1573. constexpr uint1024_t f1 = factorial(uint1024_t(31));
  1574. static_assert(f1 == 0x1956ad0aae33a4560c5cd2c000000_cppi);
  1575. Another example in [@../../test/constexpr_test_cpp_int_7.cpp constexpr_test_cpp_int_7.cpp] generates
  1576. a fresh multiprecision random number each time the file is compiled.
  1577. [endsect]
  1578. [section:import_export Importing and Exporting Data to and from `cpp_int` and `cpp_bin_float`]
  1579. Any integer number type that uses `cpp_int_backend` as it's implementation layer can import or export its bits via two non-member functions:
  1580. template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator,
  1581. expression_template_option ExpressionTemplates, class OutputIterator>
  1582. OutputIterator export_bits(
  1583. const number<const cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>& val,
  1584. OutputIterator out,
  1585. unsigned chunk_size,
  1586. bool msv_first = true);
  1587. template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator,
  1588. expression_template_option ExpressionTemplates, class Iterator>
  1589. number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>&
  1590. import_bits(
  1591. number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>& val,
  1592. Iterator i,
  1593. Iterator j,
  1594. unsigned chunk_size = 0,
  1595. bool msv_first = true);
  1596. These functions are designed for data-interchange with other storage formats, and since __cpp_bin_float uses __cpp_int internally,
  1597. by extension they can be used for floating-point numbers based on that backend as well (see example below).
  1598. Parameters and use are as follows:
  1599. template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator,
  1600. expression_template_option ExpressionTemplates, class OutputIterator>
  1601. OutputIterator export_bits(
  1602. const number<const cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>& val,
  1603. OutputIterator out,
  1604. unsigned chunk_size,
  1605. bool msv_first = true);
  1606. Exports the absolute value of `val` to OutputIterator `out`. The function will write `chunk_size` bits at a time
  1607. to the OutputIterator, and if `msv_first` is true, will write the most-significant block first. Byte and bit order
  1608. within each `chunk_size` block is always in the machines native format. Further, each block is stored in a
  1609. `boost::uintmax_t` when it's assigned to `*out`.
  1610. [note Unfortunately, the standard's OutputIterator concept provides no means of deducing the type to output since
  1611. `std::iterator_traits<OutputIteratorType>::value_type` is type `void`. This is why the bit count for each block
  1612. has to be specified manually. It may also result in compiler warnings about the value being narrowed.]
  1613. [tip If you're exporting to non-native byte layout, then use
  1614. [@http://www.boost.org/doc/libs/release/libs/endian/doc/index.html Boost.Endian]
  1615. to create a custom OutputIterator that reverses the byte order of each chunk prior to actually storing the result.]
  1616. template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator,
  1617. expression_template_option ExpressionTemplates, class ForwardIterator>
  1618. number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>&
  1619. import_bits(
  1620. number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>& val,
  1621. ForwardIterator i,
  1622. ForwardIterator j,
  1623. unsigned chunk_size = 0,
  1624. bool msv_first = true);
  1625. Imports bits from the iterator range ['\[i,j)] and stores them in `val` to produce an unsigned result (if the result
  1626. is to be signed you will need to handle that separately). When `msv_first` is true, takes `*i` as the most significant
  1627. chunk. Assumes there are `chunk_size` bits in each value read from the iterator range, and that these are in machine native
  1628. bit/byte order. When `chunk_size` is zero, then assumes that each chunk contains
  1629. `std::numeric_limits<std::iterator_traits<ForwardIterator>::value_type>::digits`, note that this will give the wrong result
  1630. if dereferencing the iterators leads to a signed-integer type, [*and] the sign bit is significant (be particularly careful
  1631. if you expect type `char` to contain 8-bit values, as by default it will extract only 7-bits at a time if `char` is signed).
  1632. As with exporting, if the external data is to be in a non-native byte order (within each chunk), then you will need to create an iterator adaptor
  1633. that presents it in native order (see [@http://www.boost.org/doc/libs/release/libs/endian/doc/index.html Boost.Endian]).
  1634. [note
  1635. Note that this function is optimized for the case where the data can be `memcpy`ed from the source to the integer - in this case both
  1636. iterators much be pointers, and everything must be little-endian.]
  1637. [h4 Examples]
  1638. [IE1]
  1639. [IE2]
  1640. [endsect] [/section:import_export Importing and Exporting Data to and from `cpp_int` and `cpp_bin_float`]
  1641. [section:rounding Rounding Rules for Conversions]
  1642. As a general rule, all conversions between unrelated types are performed using basic arithmetic operations, therefore
  1643. conversions are either exact, or follow the same rounding rules as arithmetic for the type in question.
  1644. The following table summarises the situation for conversions from native types:
  1645. [table
  1646. [[Backend][Rounding Rules]]
  1647. [[__cpp_int][Conversions from integer types are exact if the target has sufficient precision, otherwise they
  1648. truncate to the first 2^MaxBits bits (modulo arithmetic). Conversions from floating-point types
  1649. are truncating to the nearest integer.]]
  1650. [[__gmp_int][Conversions are performed by the GMP library except for conversion from `long double` which is truncating.]]
  1651. [[__tom_int][Conversions from floating-point types are truncating, all others are performed by libtommath and are exact.]]
  1652. [[__gmp_float][Conversions are performed by the GMP library except for conversion from `long double` which should be exact
  1653. provided the target type has as much precision as a `long double`.]]
  1654. [[__mpfr_float_backend][All conversions are performed by the underlying MPFR library.]]
  1655. [[__cpp_dec_float][All conversions are performed using basic arithmetic operations and are truncating.]]
  1656. [[__gmp_rational][See __gmp_int]]
  1657. [[__cpp_rational][See __cpp_int]]
  1658. [[__tommath_rational][See __tom_int]]
  1659. ]
  1660. [endsect] [/section:rounding Rounding Rules for Conversions]
  1661. [section:mixed Mixed Precision Arithmetic]
  1662. Mixed precision arithmetic is fully supported by the library.
  1663. There are two different forms:
  1664. * Where the operands are of different precision.
  1665. * Where the operands are of the same precision, but yield a higher precision result.
  1666. [h4 Mixing Operands of Differing Precision]
  1667. If the arguments to a binary operator are of different precision, then the operation is allowed
  1668. as long as there is an unambiguous implicit conversion from one argument type to the other.
  1669. In all cases the arithmetic is performed "as if" the lower precision type is promoted to the
  1670. higher precision type before applying the operator. However, particular backends may optimise
  1671. this and avoid actually creating a temporary if they are able to do so.
  1672. For example:
  1673. mpfr_float_50 a(2), b;
  1674. mpfr_float_100 c(3), d;
  1675. static_mpfr_float_50 e(5), f;
  1676. mpz_int i(20);
  1677. d = a * c; // OK, result of operand is an mpfr_float_100.
  1678. b = a * c; // Error, can't convert the result to an mpfr_float_50 as it will lose digits.
  1679. f = a * e; // Error, operator is ambiguous, result could be of either type.
  1680. f = e * i; // OK, unambiguous conversion from mpz_int to static_mpfr_float_50
  1681. [h4 Operands of the Same Precision]
  1682. Sometimes you want to apply an operator to two arguments of the same precision in
  1683. such a way as to obtain a result of higher precision. The most common situation
  1684. occurs with fixed precision integers, where you want to multiply two N-bit numbers
  1685. to obtain a 2N-bit result. This is supported in this library by the following
  1686. free functions:
  1687. template <class ResultType, class Source1 class Source2>
  1688. ResultType& add(ResultType& result, const Source1& a, const Source2& b);
  1689. template <class ResultType, class Source1 class Source2>
  1690. ResultType& subtract(ResultType& result, const Source1& a, const Source2& b);
  1691. template <class ResultType, class Source1 class Source2>
  1692. ResultType& multiply(ResultType& result, const Source1& a, const Source2& b);
  1693. These functions apply the named operator to the arguments ['a] and ['b] and store the
  1694. result in ['result], returning ['result]. In all cases they behave "as if"
  1695. arguments ['a] and ['b] were first promoted to type `ResultType` before applying the
  1696. operator, though particular backends may well avoid that step by way of an optimization.
  1697. The type `ResultType` must be an instance of class `number`, and the types `Source1` and `Source2`
  1698. may be either instances of class `number` or native integer types. The latter is an optimization
  1699. that allows arithmetic to be performed on native integer types producing an extended precision result.
  1700. For example:
  1701. [mixed_eg]
  1702. Produces the output:
  1703. [mixed_output]
  1704. [h4 Backends With Optimized Mixed Precision Arithmetic]
  1705. The following backends have at least some direct support for mixed precision arithmetic,
  1706. and therefore avoid creating unnecessary temporaries when using the interfaces above.
  1707. Therefore when using these types it's more efficient to use mixed precision arithmetic,
  1708. than it is to explicitly cast the operands to the result type:
  1709. __mpfr_float_backend, __mpf_float, __cpp_int.
  1710. [endsect] [/section:mixed Mixed Precision Arithmetic]
  1711. [section:gen_int Generic Integer Operations]
  1712. All of the [link boost_multiprecision.ref.number.integer_functions non-member integer operations] are overloaded for the
  1713. built in integer types in
  1714. `<boost/multiprecision/integer.hpp>`.
  1715. Where these operations require a temporary increase in precision (such as for `powm`), then
  1716. if no built in type is available, a __cpp_int of appropriate precision will be used.
  1717. Some of these functions are trivial, others use compiler intrinsics (where available) to ensure optimal evaluation.
  1718. The overloaded functions are:
  1719. template <class Integer, class I2>
  1720. Integer& multiply(Integer& result, const I2& a, const I2& b);
  1721. Multiplies two `I2` values, to produce a wider `Integer` result.
  1722. Returns `result = a * b` without overflow or loss of precision in the multiplication.
  1723. template <class Integer, class I2>
  1724. Integer& add(Integer& result, const I2& a, const I2& b);
  1725. Adds two `I2` values, to produce a wider `Integer` result.
  1726. Returns `result = a + b` without overflow or loss of precision in the addition.
  1727. template <class Integer, class I2>
  1728. Integer& subtract(Integer& result, const I2& a, const I2& b);
  1729. Subtracts two `I2` values, to produce a wider `Integer` result.
  1730. Returns `result = a - b` without overflow or loss of precision in the subtraction.
  1731. template <class Integer>
  1732. Integer powm(const Integer& b, const Integer& p, const Integer& m);
  1733. Returns b[super p] % m.
  1734. template <class Integer>
  1735. void divide_qr(const Integer& x, const Integer& y, Integer& q, Integer& r);
  1736. Sets `q = x / y` and `r = x % y`.
  1737. template <class Integer1, class Integer2>
  1738. Integer2 integer_modulus(const Integer1& x, Integer2 val);
  1739. Returns x % val;
  1740. template <class Integer>
  1741. unsigned lsb(const Integer& x);
  1742. Returns the (zero-based) index of the least significant bit of `x`.
  1743. Throws a `std::domain_error` if `x <= 0`.
  1744. template <class Integer>
  1745. unsigned msb(const Integer& x);
  1746. Returns the (zero-based) index of the most significant bit of `x`.
  1747. Throws a `std::domain_error` if `x <= 0`.
  1748. template <class Integer>
  1749. bool bit_test(const Integer& val, unsigned index);
  1750. Returns `true` if bit `index` is set in `val`.
  1751. template <class Integer>
  1752. Integer& bit_set(Integer& val, unsigned index);
  1753. Sets the `index` bit in `val`.
  1754. template <class Integer>
  1755. Integer& bit_unset(Integer& val, unsigned index);
  1756. Unsets the `index` bit in `val`.
  1757. template <class Integer>
  1758. Integer& bit_flip(Integer& val, unsigned index);
  1759. Flips the `index` bit in `val`.
  1760. template <class Integer>
  1761. Integer sqrt(const Integer& x);
  1762. template <class Integer>
  1763. Integer sqrt(const Integer& x, Integer& r);
  1764. Returns the integer square root `s` of x and sets `r` to the remainder ['x - s[super 2]].
  1765. template <class Engine>
  1766. bool miller_rabin_test(const number-or-expression-template-type& n, unsigned trials, Engine& gen);
  1767. bool miller_rabin_test(const number-or-expression-template-type& n, unsigned trials);
  1768. The regular Miller-Rabin functions in `<boost/multiprecision/miller_rabin.hpp>` are defined in terms of the above
  1769. generic operations, and so function equally well for built-in or __fundamental_types and multiprecision types.
  1770. [endsect] [/section:gen_int Generic Integer Operations]
  1771. [section:serial Boost.Serialization Support]
  1772. Support for serialization comes in two forms:
  1773. * Classes __number, __debug_adaptor, __logged_adaptor and __rational_adaptor have "pass through" serialization
  1774. support which requires the underlying backend to be serializable.
  1775. * Backends __cpp_int, __cpp_bin_float, __cpp_dec_float and __float128 have full support for Boost.Serialization.
  1776. [endsect] [/section:serialization Boost Serialization]
  1777. [section:limits Numeric Limits]
  1778. Boost.Multiprecision tries hard to implement `std::numeric_limits` for all types
  1779. as far as possible and meaningful because experience with Boost.Math has shown that this aids portability.
  1780. The [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf C++ standard library]
  1781. defines `std::numeric_limits` in section 18.3.2.
  1782. This in turn refers to the C standard
  1783. [@http://www.open-std.org/jtc1/sc22/wg11/docs/n507.pdf SC22/WG11 N507 DRAFT INTERNATIONAL ISO/IEC STANDARD
  1784. WD 10967-1]
  1785. Information technology Language independent arithmetic Part 1: Integer and floating-point arithmetic.
  1786. That C Standard in turn refers to
  1787. [@https://doi.org/10.1109/IEEESTD.1985.82928 IEEE754 IEEE Standard for Binary
  1788. Floating-Point Arithmetic]
  1789. There is a useful summary at
  1790. [@http://www.cplusplus.com/reference/limits/numeric_limits/ C++ reference].
  1791. The chosen backend often determines how completely `std::numeric_limits` is available.
  1792. Compiler options, processor type, and definition of macros or assembler instructions to control denormal numbers will alter
  1793. the values in the tables given below.
  1794. [warning GMP's `mpf_t` does not have a concept of overflow:
  1795. operations that lead to overflow eventually run of out of resources
  1796. and terminate with stack overflow (often after several seconds).]
  1797. [section:constants std::numeric_limits<> constants]
  1798. [h4 is_specialized]
  1799. `true` for all arithmetic types (integer, floating and fixed-point)
  1800. for which `std::numeric_limits<T>::numeric_limits` is specialized.
  1801. A typical test is
  1802. if (std::numeric_limits<T>::is_specialized == false)
  1803. {
  1804. std::cout << "type " << typeid(T).name() << " is not specialized for std::numeric_limits!" << std::endl;
  1805. // ...
  1806. }
  1807. Typically `numeric_limits<T>::is_specialized` is `true` for all `T` where the compile-time constant
  1808. members of `numeric_limits` are indeed known at compile time, and don't vary at runtime. For example
  1809. floating-point types with runtime-variable precision such as `mpfr_float` have no `numeric_limits`
  1810. specialization as it would be impossible to define all the members at compile time. In contrast
  1811. the precision of a type such as `mpfr_float_50` is known at compile time, and so it ['does] have a
  1812. `numeric_limits` specialization.
  1813. Note that not all the `std::numeric_limits` member constants and functions are meaningful for all user-defined types (UDT),
  1814. such as the decimal and binary multiprecision types provided here. More information on this is given in the sections below.
  1815. [h4 infinity]
  1816. For floating-point types, [infin] is defined wherever possible,
  1817. but clearly infinity is meaningless for __arbitrary_precision arithmetic backends,
  1818. and there is one floating-point type (GMP's `mpf_t`, see __mpf_float) which has no notion
  1819. of infinity or NaN at all.
  1820. A typical test whether infinity is implemented is
  1821. if(std::numeric_limits<T>::has_infinity)
  1822. {
  1823. std::cout << std::numeric_limits<T>::infinity() << std::endl;
  1824. }
  1825. and using tests like this is strongly recommended to improve portability.
  1826. If the backend is switched to a type that does not support infinity then,
  1827. without checks like this, there will be trouble.
  1828. [h4 is_signed]
  1829. `std::numeric_limits<T>::is_signed == true` if the type `T` is signed.
  1830. For built-in binary types, the sign is held in a single bit,
  1831. but for other types (cpp_dec_float and cpp_bin_float)
  1832. it may be a separate storage element, usually `bool`.
  1833. [h4 is_exact]
  1834. `std::numeric_limits<T>::is_exact == true` if type T uses exact representations.
  1835. This is defined as `true` for all integer types and `false` for floating-point types.
  1836. [@http://stackoverflow.com/questions/14203654/stdnumeric-limitsis-exact-what-is-a-usable-definition A usable definition]
  1837. has been discussed.
  1838. ISO/IEC 10967-1, Language independent arithmetic, noted by the C++ Standard defines
  1839. A floating-point type F shall be a finite subset of [real].
  1840. The important practical distinction is that all integers (up to `max()`) can be stored exactly.
  1841. [@http://en.wikipedia.org/wiki/Rational_number Rational]
  1842. types using two integer types are also exact.
  1843. Floating-point types [*cannot store all real values]
  1844. (those in the set of [real]) [*exactly].
  1845. For example, 0.5 can be stored exactly in a binary floating-point, but 0.1 cannot.
  1846. What is stored is the nearest representable real value, that is, rounded to nearest.
  1847. Fixed-point types (usually decimal) are also defined as exact, in that they only
  1848. store a [*fixed precision], so half cents or pennies (or less) cannot be stored.
  1849. The results of computations are rounded up or down,
  1850. just like the result of integer division stored as an integer result.
  1851. There are number of proposals to
  1852. [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3407.html
  1853. add Decimal floating-point Support to C++].
  1854. [@http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2849.pdf Decimal TR].
  1855. And also
  1856. [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3352.html
  1857. C++ Binary Fixed-Point Arithmetic].
  1858. [h4 is_bounded]
  1859. `std::numeric_limits<T>::is_bounded == true` if the set of values represented by the type `T` is finite.
  1860. This is `true` for all built-in integer, fixed and floating-point types,
  1861. and most multi-precision types.
  1862. It is only `false` for a few __arbitrary_precision types like `cpp_int`.
  1863. Rational and fixed-exponent representations are exact but not integer.
  1864. [h4 is_modulo]
  1865. `std::numeric_limits<T>::is_modulo` is defined as `true` if adding two positive values of type T
  1866. can yield a result less than either value.
  1867. `is_modulo == true` means that the type does not overflow, but, for example,
  1868. 'wraps around' to zero, when adding one to the `max()` value.
  1869. For most built-in integer types, `std::numeric_limits<>::is_modulo` is `true`.
  1870. `bool` is the only exception.
  1871. The modulo behaviour is sometimes useful,
  1872. but also can be unexpected, and sometimes undesired, behaviour.
  1873. Overflow of signed integers can be especially unexpected,
  1874. possibly causing change of sign.
  1875. Boost.Multiprecision integer type `cpp_int` is not modulo
  1876. because as an __arbitrary_precision types,
  1877. it expands to hold any value that the machine resources permit.
  1878. However fixed precision __cpp_int's may be modulo if they are unchecked
  1879. (i.e. they behave just like built in integers), but not if they are checked
  1880. (overflow causes an exception to be raised).
  1881. Built-in and multi-precision floating-point types are normally not modulo.
  1882. Where possible, overflow is to `std::numeric_limits<>::infinity()`,
  1883. provided `std::numeric_limits<>::has_infinity == true`.
  1884. [h4 radix]
  1885. Constant `std::numeric_limits<T>::radix` returns either 2 (for built-in and binary types)
  1886. or 10 (for decimal types).
  1887. [h4 digits]
  1888. The number of `radix` digits that be represented without change:
  1889. * for integer types, the number of [*non-sign bits] in the significand.
  1890. * for floating types, the number of [*radix digits] in the significand.
  1891. The values include any implicit bit, so for example, for the ubiquious
  1892. `double` using 64 bits
  1893. ([@http://en.wikipedia.org/wiki/Double_precision_floating-point_format IEEE binary64 ]),
  1894. `digits` == 53, even though there are only 52 actual bits of the significand stored in the representation.
  1895. The value of `digits` reflects the fact that there is one implicit bit which is always set to 1.
  1896. The Boost.Multiprecision binary types do not use an implicit bit, so the
  1897. `digits` member reflects exactly how many bits of precision were requested:
  1898. typedef number<cpp_bin_float<53, digit_base_2> > float64;
  1899. typedef number<cpp_bin_float<113, digit_base_2> > float128;
  1900. std::numeric_limits<float64>::digits == 53.
  1901. std::numeric_limits<float128>::digits == 113.
  1902. For the most common case of `radix == 2`,
  1903. `std::numeric_limits<T>::digits` is the number of bits in the representation,
  1904. not counting any sign bit.
  1905. For a decimal integer type, when `radix == 10`, it is the number of decimal digits.
  1906. [h4 digits10]
  1907. Constant `std::numeric_limits<T>::digits10` returns the number of
  1908. decimal digits that can be represented without change or loss.
  1909. For example, `numeric_limits<unsigned char>::digits10` is 2.
  1910. This somewhat inscrutable definition means that an `unsigned char`
  1911. can hold decimal values `0..99`
  1912. without loss of precision or accuracy, usually from truncation.
  1913. Had the definition been 3 then that would imply it could hold 0..999,
  1914. but as we all know, an 8-bit `unsigned char` can only hold 0..255,
  1915. and an attempt to store 256 or more will involve loss or change.
  1916. For bounded integers, it is thus [*one less] than number of decimal digits
  1917. you need to display the biggest integer `std::numeric_limits<T>::max()`.
  1918. This value can be used to predict the layout width required for
  1919. [digits10_1]
  1920. For example, `unsigned short` is often stored in 16 bits,
  1921. so the maximum value is 0xFFFF or 65535.
  1922. [digits10_2]
  1923. For bounded floating-point types,
  1924. if we create a `double` with a value with `digits10` (usually 15) decimal digits,
  1925. `1e15` or `1000000000000000` :
  1926. [digits10_3]
  1927. and we can increment this value to `1000000000000001`
  1928. as expected and show the difference too.
  1929. But if we try to repeat this with more than `digits10` digits,
  1930. [digits10_4]
  1931. then we find that when we add one it has no effect,
  1932. and display show that there is loss of precision. See
  1933. [@http://en.wikipedia.org/wiki/Loss_of_significance Loss of significance or cancellation error].
  1934. So `digits10` is the number of decimal digits [*guaranteed] to be correct.
  1935. For example, 'round-tripping' for `double`:
  1936. * If a decimal string with at most `digits10`( == 15) significant decimal digits
  1937. is converted to `double` and then converted back to the
  1938. same number of significant decimal digits,
  1939. then the final string will match the original 15 decimal digit string.
  1940. * If a `double` floating-point number is converted to a decimal string
  1941. with at least 17 decimal digits
  1942. and then converted back to `double`,
  1943. then the result will be binary identical to the original `double` value.
  1944. For most purposes, you will much more likely want
  1945. `std::numeric_limits<>::max_digits10`,
  1946. the number of decimal digits that ensure that a change of one least significant bit (ULP)
  1947. produces a different decimal digits string.
  1948. For the most common `double` floating-point type,`max_digits10` is `digits10+2`,
  1949. but you should use C++11 `max_digits10`
  1950. where possible (see [link boost_multiprecision.tut.limits.constants.max_digits10 below]).
  1951. [h4:max_digits10 max_digits10]
  1952. `std::numeric_limits<T>::max_digits10` was added for floating-point
  1953. because `digits10` decimal digits are insufficient to show
  1954. a least significant bit (ULP) change giving puzzling displays like
  1955. 0.666666666666667 != 0.666666666666667
  1956. from failure to 'round-trip', for example:
  1957. [max_digits10_2]
  1958. If you wish to ensure that a change of one least significant bit (ULP)
  1959. produces a different decimal digits string,
  1960. then `max_digits10` is the precision to use.
  1961. For example:
  1962. [max_digits10_3]
  1963. will display [pi] to the maximum possible precision using a `double`.
  1964. [max_digits10_4]
  1965. For integer types, `max_digits10` is implementation-dependent,
  1966. but is usually `digits10 + 2`.
  1967. This is the output field-width required for the maximum value of the type T
  1968. `std::numeric_limits<T>::max()` ['including a sign and a space].
  1969. So this will produce neat columns.
  1970. std::cout << std::setw(std::numeric_limits<int>::max_digits10) ...
  1971. The extra two or three least-significant digits are 'noisy' and may be junk,
  1972. but if you want to 'round-trip' - printing a value out as a decimal digit string and reading it back in -
  1973. (most commonly during serialization and de-serialization)
  1974. you must use `os.precision(std::numeric_limits<T>::max_digits10)`.
  1975. [note For Microsoft Visual Studio 2010,
  1976. `std::numeric_limits<float>::max_digits10` is wrongly defined as 8. It should be 9.
  1977. ]
  1978. [note For Microsoft Visual Studio before 2013 and the default floating-point
  1979. format, a small range of double-precision floating-point values with a
  1980. significand of approximately 0.0001 to 0.004 and exponent values of 1010 to
  1981. 1014 do not round-trip exactly being off by one least significant bit,
  1982. for probably every third value of the significand.
  1983. A workaround is using the scientific or exponential format `std::scientific`.
  1984. Other compilers also fail to implement round-tripping entirely fault-free, for example, see
  1985. [@https://www.exploringbinary.com/incorrectly-rounded-conversions-in-gcc-and-glibc/ Incorrectly Rounded Conversions in GCC and GLIBC].
  1986. For more details see
  1987. [@https://www.exploringbinary.com/incorrect-round-trip-conversions-in-visual-c-plus-plus/ Incorrect Round-Trip Conversions in Visual C++],
  1988. and references therein
  1989. and
  1990. [@https://arxiv.org/pdf/1310.8121.pdf Easy Accurate Reading and Writing of Floating-Point Numbers, Aubrey Jaffer (August 2018)].
  1991. Microsoft VS2017 and other recent compilers, now use the
  1992. [@https://doi.org/10.1145/3192366.3192369 Ryu fast float-to-string conversion by Ulf Adams]
  1993. algorithm, claimed to be both exact and fast for 32 and 64-bit floating-point numbers.
  1994. ] [/note]
  1995. [note BOOST_NO_CXX11_NUMERIC_LIMITS is a suitable feature-test macro to determine if
  1996. `std::numeric_limits<float>::max_digits10` is implemented on any platform.
  1997. ] [/note]
  1998. [note ['requires cxx11_numeric_limits] is a suitable test for use of `std::numeric_limits<float>::max_digits10`
  1999. to control if a target in a jamfile used by a Boost B2/bjam program is built, or not.
  2000. ] [/note]
  2001. If `max_digits10` is not available, you should use the
  2002. [@http://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF Kahan formula for floating-point type T].
  2003. In C++, the equations for what Kahan (on page 4) describes as 'at least' and 'at most' are:
  2004. static long double const log10Two = 0.30102999566398119521373889472449L; // log10(2.)
  2005. static_cast<int>(floor((significand_digits - 1) * log10Two)); // == digits10 - 'at least' .
  2006. static_cast<int>(ceil(1 + significand_digits * log10Two)); // == max_digits10 - 'at most'.
  2007. Unfortunately, these cannot be evaluated (at least by C++03) at [*compile-time].
  2008. So the following expression is often used instead.
  2009. max_digits10 = 2 + std::numeric_limits<T>::digits * 3010U/10000U;
  2010. // == 2 + std::numeric_limits<T>::digits for double and 64-bit long double.
  2011. // == 3 + std::numeric_limits<T>::digits for float, 80-bit long-double and __float128.
  2012. often the actual values are computed for the C limits macros:
  2013. #define FLT_MAXDIG10 (2+FLT_MANT_DIG * 3010U/10000U) // 9
  2014. #define DBL_MAXDIG10 (2+ (DBL_MANT_DIG * 3010U)/10000U) // 17
  2015. #define LDBL_MAXDIG10 (2+ (LDBL_MANT_DIG * 3010U)/10000U) // 17 for MSVC, 18 for others.
  2016. The factor 3010U/10000U is ['log[sub 10](2) = 0.3010]
  2017. that can be evaluated at compile-time using only `short unsigned int`s to be a desirable `const` or `constexpr` (and usually also `static`).
  2018. Boost macros allow this to be done portably, see
  2019. [@http://www.boost.org/doc/libs/1_58_0/libs/config/doc/html/boost_config/boost_macro_reference.html BOOST_CONSTEXPR_OR_CONST or BOOST_STATIC_CONSTEXPR].
  2020. (See also
  2021. [@http://www.loria.fr/~zimmerma/mca/mca-cup-0.5.9.pdf Richard P. Brent and Paul Zimmerman, Modern Computer Arithmetic]
  2022. Equation 3.8 on page 116).
  2023. [max_digits10_1] [/ example for using max_digits10]
  2024. [h4 round_style]
  2025. The rounding style determines how the result of floating-point operations
  2026. is treated when the result cannot be [*exactly represented] in the significand.
  2027. Various rounding modes may be provided:
  2028. * round to nearest up or down (default for floating-point types).
  2029. * round up (toward positive infinity).
  2030. * round down (toward negative infinity).
  2031. * round toward zero (integer types).
  2032. * no rounding (if decimal radix).
  2033. * rounding mode is not determinable.
  2034. For integer types, `std::numeric_limits<T>::round_style` is always towards zero, so
  2035. std::numeric_limits<T>::round_style == std::round_to_zero;
  2036. A decimal type, `cpp_dec_float` rounds in no particular direction,
  2037. which is to say it doesn't round at all.
  2038. And since there are several guard digits,
  2039. it's not really the same as truncation (round toward zero) either.
  2040. For floating-point types, it is normal to round to nearest.
  2041. std::numeric_limits<T>::round_style == std::round_to_nearest;
  2042. See function `std::numeric_limits<T>::round_error` for the maximum error (in ULP)
  2043. that rounding can cause.
  2044. [h4 has_denorm_loss]
  2045. `true` if a loss of precision is detected as a
  2046. [@http://en.wikipedia.org/wiki/Denormalization denormalization] loss,
  2047. rather than an inexact result.
  2048. Always `false` for integer types.
  2049. `false` for all types which do not have `has_denorm` == `std::denorm_present`.
  2050. [h4 denorm_style]
  2051. [@http://en.wikipedia.org/wiki/Denormal_number Denormalized values] are
  2052. representations with a variable number of exponent bits that can permit
  2053. gradual underflow, so that, if type T is `double`.
  2054. std::numeric_limits<T>::denorm_min() < std::numeric_limits<T>::min()
  2055. A type may have any of the following `enum float_denorm_style` values:
  2056. * `std::denorm_absent`, if it does not allow denormalized values.
  2057. (Always used for all integer and exact types).
  2058. * `std::denorm_present`, if the floating-point type allows denormalized values.
  2059. *`std::denorm_indeterminate`, if indeterminate at compile time.
  2060. [h4 Tinyness before rounding]
  2061. `bool std::numeric_limits<T>::tinyness_before`
  2062. `true` if a type can determine that a value is too small
  2063. to be represent as a normalized value before rounding it.
  2064. Generally true for `is_iec559` floating-point built-in types,
  2065. but false for integer types.
  2066. Standard-compliant IEEE 754 floating-point implementations may detect the floating-point underflow at three predefined moments:
  2067. # After computation of a result with absolute value smaller than
  2068. `std::numeric_limits<T>::min()`,
  2069. such implementation detects ['tinyness before rounding] (e.g. UltraSparc).
  2070. # After rounding of the result to `std::numeric_limits<T>::digits` bits,
  2071. if the result is tiny, such implementation detects ['tinyness after rounding]
  2072. (e.g. SuperSparc).
  2073. # If the conversion of the rounded tiny result to subnormal form
  2074. resulted in the loss of precision, such implementation detects ['denorm loss].
  2075. [endsect] [/section:constants std::numeric_limits<> Constants]
  2076. [section:functions `std::numeric_limits<>` functions]
  2077. [h4:max_function `max` function]
  2078. Function `(std::numeric_limits<T>::max)()` returns the largest finite value
  2079. that can be represented by the type T. If there is no such value (and
  2080. `numeric_limits<T>::bounded` is `false`) then returns `T()`.
  2081. For built-in types there is usually a corresponding MACRO value TYPE_MAX,
  2082. where TYPE is CHAR, INT, FLOAT etc.
  2083. Other types, including those provided by a typedef,
  2084. for example `INT64_T_MAX` for `int64_t`, may provide a macro definition.
  2085. To cater for situations where no `numeric_limits` specialization is available
  2086. (for example because the precision of the type varies at runtime),
  2087. packaged versions of this (and other functions) are provided using
  2088. #include <boost/math/tools/precision.hpp>
  2089. T = boost::math::tools::max_value<T>();
  2090. Of course, these simply use `(std::numeric_limits<T>::max)()` if available,
  2091. but otherwise 'do something sensible'.
  2092. [h4 lowest function]
  2093. Since C++11: `std::numeric_limits<T>::lowest()` is
  2094. * For integral types, the same as function `min()`.
  2095. * For floating-point types, generally the negative of `max()`
  2096. (but implementation-dependent).
  2097. [digits10_5]
  2098. [h4:min_function `min` function]
  2099. Function `(std::numeric_limits<T>::min)()` returns the minimum finite value
  2100. that can be represented by the type T.
  2101. For built-in types, there is usually a corresponding MACRO value TYPE_MIN,
  2102. where TYPE is CHAR, INT, FLOAT etc.
  2103. Other types, including those provided by a `typedef`,
  2104. for example, `INT64_T_MIN` for `int64_t`, may provide a macro definition.
  2105. For floating-point types,
  2106. it is more fully defined as the ['minimum positive normalized value].
  2107. See `std::numeric_limits<T>::denorm_min()` for the smallest denormalized value, provided
  2108. std::numeric_limits<T>::has_denorm == std::denorm_present
  2109. To cater for situations where no `numeric_limits` specialization is available
  2110. (for example because the precision of the type varies at runtime),
  2111. packaged versions of this (and other functions) are provided using
  2112. #include <boost/math/tools/precision.hpp>
  2113. T = boost::math::tools::min_value<T>();
  2114. Of course, these simply use `std::numeric_limits<T>::min()` if available.
  2115. [h4 denorm_min function]
  2116. Function `std::numeric_limits<T>::denorm_min()`
  2117. returns the smallest
  2118. [@http://en.wikipedia.org/wiki/Denormal_number denormalized value],
  2119. provided
  2120. std::numeric_limits<T>::has_denorm == std::denorm_present
  2121. [denorm_min_1]
  2122. The exponent is effectively reduced from -308 to -324
  2123. (though it remains encoded as zero and leading zeros appear in the significand,
  2124. thereby losing precision until the significand reaches zero).
  2125. [h4 round_error]
  2126. Function `std::numeric_limits<T>::round_error()` returns the maximum error
  2127. (in units of [@http://en.wikipedia.org/wiki/Unit_in_the_last_place ULP])
  2128. that can be caused by any basic arithmetic operation.
  2129. round_style == std::round_indeterminate;
  2130. The rounding style is indeterminable at compile time.
  2131. For floating-point types, when rounding is to nearest,
  2132. only half a bit is lost by rounding, and `round_error == 0.5`.
  2133. In contrast when rounding is towards zero, or plus/minus infinity,
  2134. we can loose up to one bit from rounding, and `round_error == 1`.
  2135. For integer types, rounding always to zero, so at worst almost one bit can be rounded,
  2136. so `round_error == 1`.
  2137. `round_error()` can be used with `std::numeric_limits<T>::epsilon()` to estimate
  2138. the maximum potential error caused by rounding. For typical floating-point types,
  2139. `round_error() = 1/2`, so half epsilon is the maximum potential error.
  2140. [round_error_1]
  2141. There are, of course, many occasions when much bigger loss of precision occurs,
  2142. for example, caused by
  2143. [@http://en.wikipedia.org/wiki/Loss_of_significance Loss of significance or cancellation error]
  2144. or very many iterations.
  2145. [h4:epsilon epsilon]
  2146. Function `std::numeric_limits<T>::epsilon()` is meaningful only for non-integral types.
  2147. It returns the difference between `1.0` and the next value representable
  2148. by the floating-point type T.
  2149. So it is a one least-significant-bit change in this floating-point value.
  2150. For `double` (`float_64t`) it is `2.2204460492503131e-016`
  2151. showing all possibly significant 17 decimal digits.
  2152. [epsilon_1]
  2153. We can explicitly increment by one bit using the function `boost::math::float_next()`
  2154. and the result is the same as adding `epsilon`.
  2155. [epsilon_2]
  2156. Adding any smaller value, like half `epsilon`, will have no effect on this value.
  2157. [epsilon_3]
  2158. So this cancellation error leaves the values equal, despite adding half `epsilon`.
  2159. To achieve greater portability over platform and floating-point type,
  2160. Boost.Math and Boost.Multiprecision provide a package of functions that
  2161. 'do something sensible' if the standard `numeric_limits` is not available.
  2162. To use these `#include <boost/math/tools/precision.hpp>`.
  2163. [epsilon_4]
  2164. [h5:FP_tolerance Tolerance for Floating-point Comparisons]
  2165. [@https://en.wikipedia.org/wiki/Machine_epsilon Machine epsilon [epsilon]]
  2166. is very useful to compute a tolerance when comparing floating-point values,
  2167. a much more difficult task than is commonly imagined.
  2168. The C++ standard specifies [@https://en.cppreference.com/w/cpp/types/numeric_limits/epsilon `std::numeric_limits<>::epsilon()`]
  2169. and Boost.Multiprecision implements this (where possible) for its program-defined types analogous to the
  2170. __fundamental floating-point types like `double` `float`.
  2171. For more information than you probably want (but still need) see
  2172. [@http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html What Every Computer Scientist Should Know About Floating-Point Arithmetic]
  2173. The naive test comparing the absolute difference between two values and a tolerance
  2174. does not give useful results if the values are too large or too small.
  2175. So Boost.Test uses an algorithm first devised by Knuth
  2176. for reliably checking if floating-point values are close enough.
  2177. See Donald. E. Knuth. The art of computer programming (vol II).
  2178. Copyright 1998 Addison-Wesley Longman, Inc., 0-201-89684-2.
  2179. Addison-Wesley Professional; 3rd edition. (The relevant equations are in paragraph 4.2.2, Eq. 36 and 37.)
  2180. See [@https://www.boost.org/doc/libs/release/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point/floating_points_comparison_theory.html Boost.Math floating_point comparison]
  2181. for more details.
  2182. See also:
  2183. [@http://adtmag.com/articles/2000/03/15/comparing-floats-how-to-determine-if-floating-quantities-are-close-enough-once-a-tolerance-has-been.aspx Alberto Squassia, Comparing floats]
  2184. [@http://adtmag.com/articles/2000/03/16/comparing-floats-how-to-determine-if-floating-quantities-are-close-enough-once-a-tolerance-has-been.aspx Alberto Squassia, Comparing floats code]
  2185. [@https://www.boost.org/doc/libs/release/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point.html Boost.Test Floating-Point_Comparison]
  2186. [tolerance_1]
  2187. used thus:
  2188. cd ./test
  2189. BOOST_CHECK_CLOSE_FRACTION(expected, calculated, tolerance);
  2190. (There is also a version BOOST_CHECK_CLOSE using tolerance as a [*percentage] rather than a fraction;
  2191. usually the fraction version is simpler to use).
  2192. [tolerance_2]
  2193. [h4:infinity Infinity - positive and negative]
  2194. For floating-point types only, for which
  2195. `std::numeric_limits<T>::has_infinity == true`,
  2196. function `std::numeric_limits<T>::infinity()`
  2197. provides an implementation-defined representation for [infin].
  2198. The 'representation' is a particular bit pattern reserved for infinity.
  2199. For IEEE754 system (for which `std::numeric_limits<T>::is_iec559 == true`)
  2200. [@http://en.wikipedia.org/wiki/IEEE_754-1985#Positive_and_negative_infinity positive and negative infinity]
  2201. are assigned bit patterns for all defined floating-point types.
  2202. Confusingly, the string resulting from outputting this representation, is also
  2203. implementation-defined. And the string that can be input to generate the representation is also implementation-defined.
  2204. For example, the output is `1.#INF` on Microsoft systems, but `inf` on most *nix platforms.
  2205. This implementation-defined-ness has hampered use of infinity (and NaNs)
  2206. but Boost.Math and Boost.Multiprecision work hard to provide a sensible representation
  2207. for [*all] floating-point types, not just the built-in types,
  2208. which with the use of suitable facets to define the input and output strings, makes it possible
  2209. to use these useful features portably and including Boost.Serialization.
  2210. [h4 Not-A-Number NaN]
  2211. [h5 Quiet_NaN]
  2212. For floating-point types only, for which
  2213. `std::numeric_limits<T>::has_quiet_NaN == true`,
  2214. function `std::numeric_limits<T>::quiet_NaN()`
  2215. provides an implementation-defined representation for NaN.
  2216. [@http://en.wikipedia.org/wiki/NaN NaNs] are values to indicate that the
  2217. result of an assignment or computation is meaningless.
  2218. A typical example is `0/0` but there are many others.
  2219. NaNs may also be used, to represent missing values: for example,
  2220. these could, by convention, be ignored in calculations of statistics like means.
  2221. Many of the problems with a representation for
  2222. [@http://en.wikipedia.org/wiki/NaN Not-A-Number] has hampered portable use,
  2223. similar to those with infinity.
  2224. [nan_1]
  2225. But using Boost.Math and suitable facets can permit portable use
  2226. of both NaNs and positive and negative infinity.
  2227. [facet_1]
  2228. [h5 Signaling NaN]
  2229. For floating-point types only, for which
  2230. `std::numeric_limits<T>::has_signaling_NaN == true`,
  2231. function `std::numeric_limits<T>::signaling_NaN()`
  2232. provides an implementation-defined representation for NaN that causes a hardware trap.
  2233. It should be noted however, that at least one implementation of this function causes a hardware
  2234. trap to be triggered simply by calling `std::numeric_limits<T>::signaling_NaN()`, and not only
  2235. by using the value returned.
  2236. [endsect] [/section:functions std::numeric_limits<> functions]
  2237. [/ Tables of values for numeric_limits for various built-in and cpp_bin_float types]
  2238. [include numeric_limits_32_tables.qbk]
  2239. [/include numeric_limits_64_tables.qbk]
  2240. [section:how_to_tell How to Determine the Kind of a Number From `std::numeric_limits`]
  2241. Based on the information above, one can see that different kinds of numbers can be
  2242. differentiated based on the information stored in `std::numeric_limits`. This is
  2243. in addition to the traits class [link boost_multiprecision.ref.number.traits_class_support
  2244. number_category] provided by this library.
  2245. [h4 Integer Types]
  2246. For an integer type T, all of the following conditions hold:
  2247. std::numeric_limits<T>::is_specialized == true
  2248. std::numeric_limits<T>::is_integer == true
  2249. std::numeric_limits<T>::is_exact == true
  2250. std::numeric_limits<T>::min_exponent == 0
  2251. std::numeric_limits<T>::max_exponent == 0
  2252. std::numeric_limits<T>::min_exponent10 == 0
  2253. std::numeric_limits<T>::max_exponent10 == 0
  2254. In addition the type is /signed/ if:
  2255. std::numeric_limits<T>::is_signed == true
  2256. If the type is arbitrary precision then:
  2257. std::numeric_limits<T>::is_bounded == false
  2258. Otherwise the type is bounded, and returns a non zero value
  2259. from:
  2260. std::numeric_limits<T>::max()
  2261. and has:
  2262. std::numeric_limits<T>::is_modulo == true
  2263. if the type implements modulo arithmetic on overflow.
  2264. [h4 Rational Types]
  2265. Rational types are just like integers except that:
  2266. std::numeric_limits<T>::is_integer == false
  2267. [h4 Fixed Precision Types]
  2268. There appears to be no way to tell these apart from rational types, unless they set:
  2269. std::numeric_limits<T>::is_exact == false
  2270. This is because these types are in essence a rational type with a fixed denominator.
  2271. [h4 floating-point Types]
  2272. For a floating-point type T, all of the following conditions hold:
  2273. std::numeric_limits<T>::is_specialized == true
  2274. std::numeric_limits<T>::is_integer == false
  2275. std::numeric_limits<T>::is_exact == false
  2276. std::numeric_limits<T>::min_exponent != 0
  2277. std::numeric_limits<T>::max_exponent != 0
  2278. std::numeric_limits<T>::min_exponent10 != 0
  2279. std::numeric_limits<T>::max_exponent10 != 0
  2280. In addition the type is /signed/ if:
  2281. std::numeric_limits<T>::is_signed == true
  2282. And the type may be decimal or binary depending on the value of:
  2283. std::numeric_limits<T>::radix
  2284. In general, there are no arbitrary precision floating-point types, and so:
  2285. std::numeric_limits<T>::is_bounded == false
  2286. [h4 Exact floating-point Types]
  2287. Exact floating-point types are a [@http://en.wikipedia.org/wiki/Field_%28mathematics%29 field]
  2288. composed of an arbitrary precision integer scaled by an exponent. Such types
  2289. have no division operator and are the same as floating-point types except:
  2290. std::numeric_limits<T>::is_exact == true
  2291. [h4 Complex Numbers]
  2292. For historical reasons, complex numbers do not specialize `std::numeric_limits`, instead you must
  2293. inspect `std::numeric_limits<typename T::value_type>`.
  2294. [endsect] [/section:how_to_tell How to Determine the Kind of a Number From `std::numeric_limits`]
  2295. [endsect] [/section:limits Numeric Limits]
  2296. [section:input_output Input Output]
  2297. [h4 Loopback testing]
  2298. ['Loopback] or ['round-tripping] refers to writing out a value as a decimal digit string using `std::iostream`,
  2299. usually to a `std::stringstream`, and then reading the string back in to another value,
  2300. and confirming that the two values are identical. A trivial example using `float` is:
  2301. float write; // Value to round-trip.
  2302. std::stringstream ss; // Read and write std::stringstream.
  2303. ss.precision(std::numeric_limits<T>::max_digits10); // Ensure all potentially significant bits are output.
  2304. ss.flags(std::ios_base::fmtflags(std::ios_base::scientific)); // Use scientific format.
  2305. ss << write; // Output to string.
  2306. float read; // Expected.
  2307. ss >> read; // Read decimal digits string from stringstream.
  2308. BOOST_CHECK_EQUAL(write, read); // Should be the same.
  2309. and this can be run in a loop for all possible values of a 32-bit float.
  2310. For other floating-point types `T`, including built-in `double`,
  2311. it takes far too long to test all values,
  2312. so a reasonable test strategy is to use a large number of random values.
  2313. T write;
  2314. std::stringstream ss;
  2315. ss.precision(std::numeric_limits<T>::max_digits10); // Ensure all potentially significant bits are output.
  2316. ss.flags(f); // Changed from default iostream format flags if desired.
  2317. ss << write; // Output to stringstream.
  2318. T read;
  2319. ss >> read; // Get read using operator>> from stringstream.
  2320. BOOST_CHECK_EQUAL(read, write);
  2321. read = static_cast<T>(ss.str()); // Get read by converting from decimal digits string representation of write.
  2322. BOOST_CHECK_EQUAL(read, write);
  2323. read = static_cast<T>(write.str(0, f)); // Get read using format specified when written.
  2324. BOOST_CHECK_EQUAL(read, write);
  2325. The test at
  2326. [@../../test/test_cpp_bin_float_io.cpp test_cpp_bin_float_io.cpp]
  2327. allows any floating-point type to be ['round_tripped] using a wide range of fairly random values.
  2328. It also includes tests compared a collection of
  2329. [@../../test/string_data.ipp stringdata] test cases in a file.
  2330. [h4 Comparing with output using Built-in types]
  2331. One can make some comparisons with the output of
  2332. <number<cpp_bin_float<53, digit_count_2> >
  2333. which has the same number of significant bits (53) as 64-bit double precision floating-point.
  2334. However, although most outputs are identical, there are differences on some platforms
  2335. caused by the implementation-dependent behaviours allowed by the C99 specification
  2336. [@http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf C99 ISO/IEC 9899:TC2],
  2337. incorporated by C++.
  2338. [:['"For e, E, f, F, g, and G conversions, if the number of significant decimal digits
  2339. is at most DECIMAL_DIG, then the result should be correctly rounded.
  2340. If the number of significant decimal digits is more than DECIMAL_DIG
  2341. but the source value is exactly representable with DECIMAL_DIG digits,
  2342. then the result should be an exact representation with trailing zeros.
  2343. Otherwise, the source value is bounded by two adjacent decimal strings L < U,
  2344. both having DECIMAL_DIG significant digits;
  2345. the value of the resultant decimal string D should satisfy L<= D <= U,
  2346. with the extra stipulation that the error should have a correct sign
  2347. for the current rounding direction."]]
  2348. So not only is correct rounding for the full number of digits not required,
  2349. but even if the *optional* recommended practice is followed,
  2350. then the value of these last few digits is unspecified
  2351. as long as the value is within certain bounds.
  2352. [note Do not expect the output from different platforms
  2353. to be [*identical], but `cpp_dec_float`, `cpp_bin_float` (and other backends) outputs should be
  2354. correctly rounded to the number of digits requested by the set precision and format.]
  2355. [h4 Macro BOOST_MP_MIN_EXPONENT_DIGITS]
  2356. [@http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf C99 Standard]
  2357. for [/e and E] format specifiers, 7.19.6 Formatted input/output functions requires:
  2358. \"The exponent always contains at least two digits,
  2359. and only as many more digits as necessary to represent the exponent.\"
  2360. So to conform to the C99 standard (incorporated by C++)
  2361. #define BOOST_MP_MIN_EXPONENT_DIGITS 2
  2362. Confusingly, Microsoft (and MinGW) do not conform to this standard and provide
  2363. [*at least three digits], for example `1e+001`.
  2364. So if you want the output to match that from
  2365. built-in floating-point types on compilers that use Microsofts runtime then use:
  2366. #define BOOST_MP_MIN_EXPONENT_DIGITS 3
  2367. Also useful to get the minimum exponent field width is
  2368. #define BOOST_MP_MIN_EXPONENT_DIGITS 1
  2369. producing a compact output like `2e+4`,
  2370. useful when conserving space is important.
  2371. Larger values are also supported, for example, value 4 for `2e+0004`
  2372. which may be useful to ensure that columns line up.
  2373. [endsect] [/section:input_output Input Output]
  2374. [section:hash Hash Function Support]
  2375. [hash1]
  2376. [hash2]
  2377. [hash3]
  2378. [hash4]
  2379. [endsect][/hash]
  2380. [section:eigen Eigen Interoperability]
  2381. This library provides the header:
  2382. #include <boost/multiprecision/eigen.hpp>
  2383. which defines the traits classes and functions that the Eigen library needs all user-defined number types to provide.
  2384. For example the following code performs quad-precision matrix solving on complex numbers:
  2385. [eigen_eg]
  2386. Which produces the following output:
  2387. [eigen_out]
  2388. [endsect]
  2389. [endsect]
  2390. [section:ref Reference]
  2391. [section:number number]
  2392. [h4 Synopsis]
  2393. namespace boost{ namespace multiprecision{
  2394. enum expression_template_option { et_on = 1, et_off = 0 };
  2395. template <class Backend> struct expression_template_default
  2396. { static const expression_template_option value = et_on; };
  2397. template <class Backend, expression_template_option ExpressionTemplates = expression_template_default<Backend>::value>
  2398. class number
  2399. {
  2400. public:
  2401. typedef Backend backend_type;
  2402. typedef typename component_type<self_type>::type value_type;
  2403. number();
  2404. number(see-below);
  2405. number& operator=(see-below);
  2406. number& assign(see-below);
  2407. // Member operators
  2408. number& operator+=(const ``['see-below]``&);
  2409. number& operator-=(const ``['see-below]``&);
  2410. number& operator*=(const ``['see-below]``&);
  2411. number& operator/=(const ``['see-below]``&);
  2412. number& operator++();
  2413. number& operator--();
  2414. number operator++(int);
  2415. number operator--(int);
  2416. number& operator%=(const ``['see-below]``&);
  2417. number& operator&=(const ``['see-below]``&);
  2418. number& operator|=(const ``['see-below]``&);
  2419. number& operator^=(const ``['see-below]``&);
  2420. number& operator<<=(const ``['integer-type]``&);
  2421. number& operator>>=(const ``['integer-type]``&);
  2422. // Use in Boolean context:
  2423. operator ``['convertible-to-bool-type]``()const;
  2424. // swap:
  2425. void swap(number& other);
  2426. // Sign:
  2427. bool is_zero()const;
  2428. int sign()const;
  2429. // string conversion:
  2430. std::string str()const;
  2431. // Generic conversion mechanism
  2432. template <class T>
  2433. T convert_to()const;
  2434. template <class T>
  2435. explicit operator T ()const;
  2436. // precision control:
  2437. static unsigned default_precision();
  2438. static void default_precision(unsigned digits10);
  2439. unsigned precision()const;
  2440. void precision(unsigned digits10);
  2441. // Comparison:
  2442. int compare(const number<Backend>& o)const;
  2443. template <class V>
  2444. typename enable_if<is_convertible<V, number<Backend, ExpressionTemplates> >, int>::type
  2445. compare(const V& o)const;
  2446. // real and imaginary parts:
  2447. value_type real()const;
  2448. value_type imag()const;
  2449. template <class T>
  2450. void real(const T& val);
  2451. template <class T>
  2452. void imag(const T& val);
  2453. // Access to the underlying implementation:
  2454. Backend& backend();
  2455. const Backend& backend()const;
  2456. };
  2457. // Non member operators:
  2458. ``['unmentionable-expression-template-type]`` operator+(const ``['see-below]``&);
  2459. ``['unmentionable-expression-template-type]`` operator-(const ``['see-below]``&);
  2460. ``['unmentionable-expression-template-type]`` operator+(const ``['see-below]``&, const ``['see-below]``&);
  2461. ``['unmentionable-expression-template-type]`` operator-(const ``['see-below]``&, const ``['see-below]``&);
  2462. ``['unmentionable-expression-template-type]`` operator*(const ``['see-below]``&, const ``['see-below]``&);
  2463. ``['unmentionable-expression-template-type]`` operator/(const ``['see-below]``&, const ``['see-below]``&);
  2464. // Integer only operations:
  2465. ``['unmentionable-expression-template-type]`` operator%(const ``['see-below]``&, const ``['see-below]``&);
  2466. ``['unmentionable-expression-template-type]`` operator&(const ``['see-below]``&, const ``['see-below]``&);
  2467. ``['unmentionable-expression-template-type]`` operator|(const ``['see-below]``&, const ``['see-below]``&);
  2468. ``['unmentionable-expression-template-type]`` operator^(const ``['see-below]``&, const ``['see-below]``&);
  2469. ``['unmentionable-expression-template-type]`` operator<<(const ``['see-below]``&, const ``['integer-type]``&);
  2470. ``['unmentionable-expression-template-type]`` operator>>(const ``['see-below]``&, const ``['integer-type]``&);
  2471. // Comparison operators:
  2472. bool operator==(const ``['see-below]``&, const ``['see-below]``&);
  2473. bool operator!=(const ``['see-below]``&, const ``['see-below]``&);
  2474. bool operator< (const ``['see-below]``&, const ``['see-below]``&);
  2475. bool operator> (const ``['see-below]``&, const ``['see-below]``&);
  2476. bool operator<=(const ``['see-below]``&, const ``['see-below]``&);
  2477. bool operator>=(const ``['see-below]``&, const ``['see-below]``&);
  2478. // Swap:
  2479. template <class Backend, expression_template_option ExpressionTemplates>
  2480. void swap(number<Backend, ExpressionTemplates>& a, number<Backend, ExpressionTemplates>& b);
  2481. // iostream support:
  2482. template <class Backend, expression_template_option ExpressionTemplates>
  2483. std::ostream& operator << (std::ostream& os, const number<Backend, ExpressionTemplates>& r);
  2484. std::ostream& operator << (std::ostream& os, const ``['unmentionable-expression-template-type]``& r);
  2485. template <class Backend, expression_template_option ExpressionTemplates>
  2486. std::istream& operator >> (std::istream& is, number<Backend, ExpressionTemplates>& r);
  2487. // Arithmetic with a higher precision result:
  2488. template <class ResultType, class Source1 class Source2>
  2489. ResultType& add(ResultType& result, const Source1& a, const Source2& b);
  2490. template <class ResultType, class Source1 class Source2>
  2491. ResultType& subtract(ResultType& result, const Source1& a, const Source2& b);
  2492. template <class ResultType, class Source1 class Source2>
  2493. ResultType& multiply(ResultType& result, const Source1& a, const Source2& b);
  2494. // min and max overloads:
  2495. ``['number]`` min (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2496. ``['number]`` max (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2497. // C99 Non-member function standard library support:
  2498. ``['unmentionable-expression-template-type]`` abs (const ``['number-or-expression-template-type]``&);
  2499. ``['unmentionable-expression-template-type]`` acos (const ``['number-or-expression-template-type]``&);
  2500. ``['number]`` acosh (const ``['number-or-expression-template-type]``&);
  2501. ``['unmentionable-expression-template-type]`` asin (const ``['number-or-expression-template-type]``&);
  2502. ``['number]`` asinh (const ``['number-or-expression-template-type]``&);
  2503. ``['unmentionable-expression-template-type]`` atan (const ``['number-or-expression-template-type]``&);
  2504. ``['unmentionable-expression-template-type]`` atan2 (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2505. ``['number]`` atanh (const ``['number-or-expression-template-type]``&);
  2506. ``['number]`` cbrt (const ``['number-or-expression-template-type]``&);
  2507. ``['unmentionable-expression-template-type]`` ceil (const ``['number-or-expression-template-type]``&);
  2508. ``['number]`` copysign (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2509. ``['unmentionable-expression-template-type]`` cos (const ``['number-or-expression-template-type]``&);
  2510. ``['unmentionable-expression-template-type]`` cosh (const ``['number-or-expression-template-type]``&);
  2511. ``['number]`` erf (const ``['number-or-expression-template-type]``&);
  2512. ``['number]`` erfc (const ``['number-or-expression-template-type]``&);
  2513. ``['unmentionable-expression-template-type]`` exp (const ``['number-or-expression-template-type]``&);
  2514. ``['unmentionable-expression-template-type]`` exp2 (const ``['number-or-expression-template-type]``&);
  2515. ``['number]`` expm1 (const ``['number-or-expression-template-type]``&);
  2516. ``['unmentionable-expression-template-type]`` fabs (const ``['number-or-expression-template-type]``&);
  2517. ``['unmentionable-expression-template-type]`` fdim (const ``['number-or-expression-template-type]``&);
  2518. ``['unmentionable-expression-template-type]`` floor (const ``['number-or-expression-template-type]``&);
  2519. ``['unmentionable-expression-template-type]`` fma (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2520. ``['unmentionable-expression-template-type]`` fmin (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2521. ``['unmentionable-expression-template-type]`` fmax (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2522. ``['unmentionable-expression-template-type]`` fmod (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2523. ``['unmentionable-expression-template-type]`` frexp (const ``['number-or-expression-template-type]``&, ``['integer-type]``*);
  2524. ``['unmentionable-expression-template-type]`` hypot (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2525. ``['integer-type]`` ilogb (const ``['number-or-expression-template-type]``&);
  2526. ``['unmentionable-expression-template-type]`` ldexp (const ``['number-or-expression-template-type]``&, ``['integer-type]``);
  2527. ``['number]`` lgamma (const ``['number-or-expression-template-type]``&);
  2528. long long llrint (const ``['number-or-expression-template-type]``&);
  2529. long long llround (const ``['number-or-expression-template-type]``&);
  2530. ``['unmentionable-expression-template-type]`` log (const ``['number-or-expression-template-type]``&);
  2531. ``['unmentionable-expression-template-type]`` log2 (const ``['number-or-expression-template-type]``&);
  2532. ``['unmentionable-expression-template-type]`` log10 (const ``['number-or-expression-template-type]``&);
  2533. ``['number]`` log1p (const ``['number-or-expression-template-type]``&);
  2534. ``['unmentionable-expression-template-type]`` logb (const ``['number-or-expression-template-type]``&);
  2535. long lrint (const ``['number-or-expression-template-type]``&);
  2536. long lround (const ``['number-or-expression-template-type]``&);
  2537. ``['unmentionable-expression-template-type]`` modf (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2538. ``['unmentionable-expression-template-type]`` nearbyint (const ``['number-or-expression-template-type]``&);
  2539. ``['number]`` nextafter (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2540. ``['number]`` nexttoward (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2541. ``['unmentionable-expression-template-type]`` pow (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2542. ``['unmentionable-expression-template-type]`` remainder (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2543. ``['unmentionable-expression-template-type]`` remquo (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&, int*);
  2544. ``['unmentionable-expression-template-type]`` rint (const ``['number-or-expression-template-type]``&);
  2545. ``['unmentionable-expression-template-type]`` round (const ``['number-or-expression-template-type]``&);
  2546. ``['unmentionable-expression-template-type]`` scalbn (const ``['number-or-expression-template-type]``&, ``['integer-type]``);
  2547. ``['unmentionable-expression-template-type]`` scalbln (const ``['number-or-expression-template-type]``&, ``['integer-type]``);
  2548. ``['unmentionable-expression-template-type]`` sin (const ``['number-or-expression-template-type]``&);
  2549. ``['unmentionable-expression-template-type]`` sinh (const ``['number-or-expression-template-type]``&);
  2550. ``['unmentionable-expression-template-type]`` sqrt (const ``['number-or-expression-template-type]``&);
  2551. ``['unmentionable-expression-template-type]`` tan (const ``['number-or-expression-template-type]``&);
  2552. ``['unmentionable-expression-template-type]`` tanh (const ``['number-or-expression-template-type]``&);
  2553. ``['number]`` tgamma (const ``['number-or-expression-template-type]``&);
  2554. ``['unmentionable-expression-template-type]`` trunc (const ``['number-or-expression-template-type]``&);
  2555. int fpclassify (const ``['number-or-expression-template-type]``&);
  2556. bool isfinite (const ``['number-or-expression-template-type]``&);
  2557. bool isinf (const ``['number-or-expression-template-type]``&);
  2558. bool isnan (const ``['number-or-expression-template-type]``&);
  2559. bool isnormal (const ``['number-or-expression-template-type]``&);
  2560. int signbit (const ``['number-or-expression-template-type]``&);
  2561. bool isgreater (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2562. bool isgreaterequal(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2563. bool isless (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2564. bool islessequal(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2565. bool islessgreater(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2566. bool isunordered(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2567. // Complex number functions:
  2568. ``['number<...>::value_type]`` real (const ``['number-or-expression-template-type]``&);
  2569. ``['number<...>::value_type]`` imag (const ``['number-or-expression-template-type]``&);
  2570. ``['number<...>::value_type]`` abs (const ``['number-or-expression-template-type]``&);
  2571. ``['number<...>::value_type]`` arg (const ``['number-or-expression-template-type]``&);
  2572. ``['number<...>::value_type]`` norm (const ``['number-or-expression-template-type]``&);
  2573. ``['number]`` conj (const ``['number-or-expression-template-type]``&);
  2574. ``['number]`` proj (const ``['number-or-expression-template-type]``&);
  2575. ``['number]`` polar (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2576. // Misc other common C library functions:
  2577. ``['unmentionable-expression-template-type]`` itrunc (const ``['number-or-expression-template-type]``&);
  2578. ``['unmentionable-expression-template-type]`` ltrunc (const ``['number-or-expression-template-type]``&);
  2579. ``['unmentionable-expression-template-type]`` lltrunc(const ``['number-or-expression-template-type]``&);
  2580. ``['unmentionable-expression-template-type]`` iround (const ``['number-or-expression-template-type]``&);
  2581. ``['number]`` changesign(const ``['number-or-expression-template-type]``&);
  2582. ``['number]`` copysign(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2583. // Traits support:
  2584. template <class T>
  2585. struct component_type;
  2586. template <class T>
  2587. struct number_category;
  2588. template <class T>
  2589. struct is_number;
  2590. template <class T>
  2591. struct is_number_expression;
  2592. // Integer specific functions:
  2593. ``['unmentionable-expression-template-type]`` gcd(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2594. ``['unmentionable-expression-template-type]`` lcm(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2595. ``['unmentionable-expression-template-type]`` pow(const ``['number-or-expression-template-type]``&, unsigned);
  2596. ``['unmentionable-expression-template-type]`` powm(const ``['number-or-expression-template-type]``& b, const ``['number-or-expression-template-type]``& p, const ``['number-or-expression-template-type]``& m);
  2597. ``['unmentionable-expression-template-type]`` sqrt(const ``['number-or-expression-template-type]``&);
  2598. template <class Backend, expression_template_option ExpressionTemplates>
  2599. number<Backend, EXpressionTemplates> sqrt(const ``['number-or-expression-template-type]``&, number<Backend, EXpressionTemplates>&);
  2600. template <class Backend, expression_template_option ExpressionTemplates>
  2601. void divide_qr(const ``['number-or-expression-template-type]``& x, const ``['number-or-expression-template-type]``& y,
  2602. number<Backend, ExpressionTemplates>& q, number<Backend, ExpressionTemplates>& r);
  2603. template <class Integer>
  2604. Integer integer_modulus(const ``['number-or-expression-template-type]``& x, Integer val);
  2605. unsigned lsb(const ``['number-or-expression-template-type]``& x);
  2606. unsigned msb(const ``['number-or-expression-template-type]``& x);
  2607. template <class Backend, class ExpressionTemplates>
  2608. bool bit_test(const number<Backend, ExpressionTemplates>& val, unsigned index);
  2609. template <class Backend, class ExpressionTemplates>
  2610. number<Backend, ExpressionTemplates>& bit_set(number<Backend, ExpressionTemplates>& val, unsigned index);
  2611. template <class Backend, class ExpressionTemplates>
  2612. number<Backend, ExpressionTemplates>& bit_unset(number<Backend, ExpressionTemplates>& val, unsigned index);
  2613. template <class Backend, class ExpressionTemplates>
  2614. number<Backend, ExpressionTemplates>& bit_flip(number<Backend, ExpressionTemplates>& val, unsigned index);
  2615. template <class Engine>
  2616. bool miller_rabin_test(const ``['number-or-expression-template-type]``& n, unsigned trials, Engine& gen);
  2617. bool miller_rabin_test(const ``['number-or-expression-template-type]``& n, unsigned trials);
  2618. // Rational number support:
  2619. typename component_type<``['number-or-expression-template-type]``>::type numerator (const ``['number-or-expression-template-type]``&);
  2620. typename component_type<``['number-or-expression-template-type]``>::type denominator(const ``['number-or-expression-template-type]``&);
  2621. }} // namespaces
  2622. namespace boost{ namespace math{
  2623. // Boost.Math interoperability functions:
  2624. int fpclassify (const ``['number-or-expression-template-type]``&, int);
  2625. bool isfinite (const ``['number-or-expression-template-type]``&, int);
  2626. bool isnan (const ``['number-or-expression-template-type]``&, int);
  2627. bool isinf (const ``['number-or-expression-template-type]``&, int);
  2628. bool isnormal (const ``['number-or-expression-template-type]``&, int);
  2629. }} // namespaces
  2630. // numeric_limits support:
  2631. namespace std{
  2632. template <class Backend, expression_template_option ExpressionTemplates>
  2633. struct numeric_limits<boost::multiprecision<Backend, ExpressionTemplates> >
  2634. {
  2635. /* Usual members here */
  2636. };
  2637. }
  2638. [h4 Description]
  2639. enum expression_template_option { et_on = 1, et_off = 0 };
  2640. This enumerated type is used to specify whether expression templates are turned on (et_on) or turned off (et_off).
  2641. template <class Backend> struct expression_template_default
  2642. { static const expression_template_option value = et_on; };
  2643. This traits class specifies the default expression template option to be used with a particular Backend type.
  2644. It defaults to `et_on`.
  2645. template <class Backend, expression_template_option ExpressionTemplates = expression_template_default<Backend>::value>
  2646. class number;
  2647. Class `number` has two template arguments:
  2648. [variablelist
  2649. [[Backend][The actual arithmetic back-end that does all the work.]]
  2650. [[ExpressionTemplates][A Boolean value: when `et_on`, then expression templates are enabled, otherwise when set to `et_off` they are disabled.
  2651. The default for this parameter is computed via the traits class `expression_template_default` whose member `value` defaults to `et_on` unless
  2652. the traits class is specialized for a particular backend.]]
  2653. ]
  2654. number();
  2655. number(see-below);
  2656. number& operator=(see-below);
  2657. number& assign(see-below);
  2658. Type `number` is default constructible, and both copy constructible and assignable from:
  2659. * Itself.
  2660. * An expression template which is the result of one of the arithmetic operators.
  2661. * Any builtin arithmetic type, as long as the result would not be lossy (for example float to integer conversion).
  2662. * Any type that the Backend is implicitly constructible or assignable from.
  2663. * An rvalue reference to another `number`. Move-semantics are used for construction if the backend also
  2664. supports rvalue reference construction. In the case of assignment, move semantics are always supported
  2665. when the argument is an rvalue reference irrespective of the backend.
  2666. * Any type in the same family, as long as no loss of precision is involved. For example from `int128_t` to `int256_t`,
  2667. or `cpp_dec_float_50` to `cpp_dec_float_100`.
  2668. Type `number` is explicitly constructible from:
  2669. * Any type mentioned above.
  2670. * A `std::string` or any type which is convertible to `const char*`.
  2671. * Any arithmetic type (including those that would result in lossy conversions).
  2672. * Any type in the same family, including those that result in loss of precision.
  2673. * Any type that the Backend is explicitly constructible from.
  2674. * Any pair of types for which a generic interconversion exists: that is from integer to integer, integer
  2675. to rational, integer to float, rational to rational, rational to float, or float to float.
  2676. The assign member function is available for any type for which an explicit converting constructor exists.
  2677. It is intended to be used where a temporary generated from an explicit assignment would be expensive, for example:
  2678. mpfr_float_50 f50;
  2679. mpfr_float_100 f100;
  2680. f50 = static_cast<mpfr_float_50>(f100); // explicit cast create a temporary
  2681. f50.assign(f100); // explicit call to assign create no temporary
  2682. In addition, if the type has multiple components (for example rational or complex number types), then there is a
  2683. two argument constructor:
  2684. number(arg1, arg2);
  2685. Where the two args must either be arithmetic types, or types that are convertible to the two components of `this`.
  2686. Finally, when the type has a variable precision, then there are constructors:
  2687. number(arg1, precision);
  2688. number(arg1, arg2, precision);
  2689. Where `precision` is an unsigned value, the 2 arg version is active for scalar types and/or copy-construction with specific precision, and the 3-arg version for complex types.
  2690. Likewise `assign` has a 2-arg overloaded, with the second argument being the precision.
  2691. number& operator+=(const ``['see-below]``&);
  2692. number& operator-=(const ``['see-below]``&);
  2693. number& operator*=(const ``['see-below]``&);
  2694. number& operator/=(const ``['see-below]``&);
  2695. number& operator++();
  2696. number& operator--();
  2697. number operator++(int);
  2698. number operator--(int);
  2699. // Integer only operations:
  2700. number& operator%=(const ``['see-below]``&);
  2701. number& operator&=(const ``['see-below]``&);
  2702. number& operator|=(const ``['see-below]``&);
  2703. number& operator^=(const ``['see-below]``&);
  2704. number& operator<<=(const ``['integer-type]``&);
  2705. number& operator>>=(const ``['integer-type]``&);
  2706. These operators all take their usual arithmetic meanings.
  2707. The arguments to these operators is either:
  2708. * Another `number<Backend, ExpressionTemplates>`.
  2709. * An expression template derived from `number<Backend>`.
  2710. * Any type implicitly convertible to `number<Backend, ExpressionTemplates>`, including some other instance of class `number`.
  2711. For the left and right shift operations, the argument must be a builtin
  2712. integer type with a positive value (negative values result in a `std::runtime_error` being thrown).
  2713. operator ``['convertible-to-bool-type]``()const;
  2714. Returns an ['unmentionable-type] that is usable in Boolean contexts (this allows `number` to be used in any
  2715. Boolean context - if statements, conditional statements, or as an argument to a logical operator - without
  2716. type `number` being convertible to type `bool`.
  2717. This operator also enables the use of `number` with any of the following operators:
  2718. `!`, `||`, `&&` and `?:`.
  2719. void swap(number& other);
  2720. Swaps `*this` with `other`.
  2721. bool is_zero()const;
  2722. Returns `true` is `*this` is zero, otherwise `false`.
  2723. int sign()const;
  2724. Returns a value less than zero if `*this` is negative, a value greater than zero if `*this` is positive, and zero
  2725. if `*this` is zero.
  2726. std::string str(unsigned precision, bool scientific = true)const;
  2727. Returns the number formatted as a string, with at least /precision/ digits, and in scientific format
  2728. if /scientific/ is true.
  2729. template <class T>
  2730. T convert_to()const;
  2731. template <class T>
  2732. explicit operator T ()const;
  2733. Provides a generic conversion mechanism to convert `*this` to type `T`. Type `T` may be any arithmetic type.
  2734. Optionally other types may also be supported by specific `Backend` types.
  2735. static unsigned default_precision();
  2736. static void default_precision(unsigned digits10);
  2737. unsigned precision()const;
  2738. void precision(unsigned digits10);
  2739. These functions are only available if the Backend template parameter supports runtime changes to precision. They get and set
  2740. the default precision and the precision of `*this` respectively.
  2741. int compare(const number<Backend, ExpressionTemplates>& o)const;
  2742. template <class V>
  2743. typename enable_if<is_convertible<V, number<Backend, ExpressionTemplates> >, int>::type
  2744. compare(const V& other)const;
  2745. Returns:
  2746. * A value less that 0 for `*this < other`
  2747. * A value greater that 0 for `*this > other`
  2748. * Zero for `*this == other`
  2749. value_type real()const;
  2750. value_type imag()const;
  2751. These return the real and imaginary parts respectively. If the number is not a complex type, then the imaginary part is always zero.
  2752. template <class T>
  2753. void real(const T& val);
  2754. template <class T>
  2755. void imag(const T& val);
  2756. These set the real and imaginary parts respectively of the number. If the number is not a complex type, then setting the real part
  2757. is equivalent to assignment, and attempting to set the imaginary part will result in a compile time error.
  2758. Backend& backend();
  2759. const Backend& backend()const;
  2760. Returns the underlying back-end instance used by `*this`.
  2761. [h4 Non-member operators]
  2762. // Non member operators:
  2763. ``['unmentionable-expression-template-type]`` operator+(const ``['see-below]``&);
  2764. ``['unmentionable-expression-template-type]`` operator-(const ``['see-below]``&);
  2765. ``['unmentionable-expression-template-type]`` operator+(const ``['see-below]``&, const ``['see-below]``&);
  2766. ``['unmentionable-expression-template-type]`` operator-(const ``['see-below]``&, const ``['see-below]``&);
  2767. ``['unmentionable-expression-template-type]`` operator*(const ``['see-below]``&, const ``['see-below]``&);
  2768. ``['unmentionable-expression-template-type]`` operator/(const ``['see-below]``&, const ``['see-below]``&);
  2769. // Integer only operations:
  2770. ``['unmentionable-expression-template-type]`` operator%(const ``['see-below]``&, const ``['see-below]``&);
  2771. ``['unmentionable-expression-template-type]`` operator&(const ``['see-below]``&, const ``['see-below]``&);
  2772. ``['unmentionable-expression-template-type]`` operator|(const ``['see-below]``&, const ``['see-below]``&);
  2773. ``['unmentionable-expression-template-type]`` operator^(const ``['see-below]``&, const ``['see-below]``&);
  2774. ``['unmentionable-expression-template-type]`` operator<<(const ``['see-below]``&, const ``['integer-type]``&);
  2775. ``['unmentionable-expression-template-type]`` operator>>(const ``['see-below]``&, const ``['integer-type]``&);
  2776. // Comparison operators:
  2777. bool operator==(const ``['see-below]``&, const ``['see-below]``&);
  2778. bool operator!=(const ``['see-below]``&, const ``['see-below]``&);
  2779. bool operator< (const ``['see-below]``&, const ``['see-below]``&);
  2780. bool operator> (const ``['see-below]``&, const ``['see-below]``&);
  2781. bool operator<=(const ``['see-below]``&, const ``['see-below]``&);
  2782. bool operator>=(const ``['see-below]``&, const ``['see-below]``&);
  2783. These operators all take their usual arithmetic meanings.
  2784. The arguments to these functions must contain at least one of the following:
  2785. * A `number`.
  2786. * An expression template type derived from `number`.
  2787. * Any type for which `number` has an implicit constructor - for example a builtin arithmetic type.
  2788. The return type of these operators is either:
  2789. * An ['unmentionable-type] expression template type when `ExpressionTemplates` is `true`.
  2790. * Type `number<Backend, et_off>` when `ExpressionTemplates` is `false`.
  2791. * Type `bool` if the operator is a comparison operator.
  2792. Finally note that the second argument to the left and right shift operations must be a builtin integer type,
  2793. and that the argument must be positive (negative arguments result in a `std::runtime_error` being thrown).
  2794. [h4 swap]
  2795. template <class Backend, ExpressionTemplates>
  2796. void swap(number<Backend, ExpressionTemplates>& a, number<Backend, ExpressionTemplates>& b);
  2797. Swaps `a` and `b`.
  2798. [h4 Iostream Support]
  2799. template <class Backend, expression_template_option ExpressionTemplates>
  2800. std::ostream& operator << (std::ostream& os, const number<Backend, ExpressionTemplates>& r);
  2801. template <class Unspecified...>
  2802. std::ostream& operator << (std::ostream& os, const unmentionable-expression-template& r);
  2803. template <class Backend, expression_template_option ExpressionTemplates>
  2804. inline std::istream& operator >> (std::istream& is, number<Backend, ExpressionTemplates>& r)
  2805. These operators provided formatted input-output operations on `number` types, and expression templates derived from them.
  2806. It's down to the back-end type to actually implement string conversion. However, the back-ends provided with
  2807. this library support all of the iostream formatting flags, field width and precision settings.
  2808. [h4 Arithmetic with a higher precision result]
  2809. template <class ResultType, class Source1 class Source2>
  2810. ResultType& add(ResultType& result, const Source1& a, const Source2& b);
  2811. template <class ResultType, class Source1 class Source2>
  2812. ResultType& subtract(ResultType& result, const Source1& a, const Source2& b);
  2813. template <class ResultType, class Source1 class Source2>
  2814. ResultType& multiply(ResultType& result, const Source1& a, const Source2& b);
  2815. These functions apply the named operator to the arguments ['a] and ['b] and store the
  2816. result in ['result], returning ['result]. In all cases they behave "as if"
  2817. arguments ['a] and ['b] were first promoted to type `ResultType` before applying the
  2818. operator, though particular backends may well avoid that step by way of an optimization.
  2819. The type `ResultType` must be an instance of class `number`, and the types `Source1` and `Source2`
  2820. may be either instances of class `number` or native integer types. The latter is an optimization
  2821. that allows arithmetic to be performed on native integer types producing an extended precision result.
  2822. [h4 Non-member standard library function support]
  2823. ``['unmentionable-expression-template-type]`` abs (const ``['number-or-expression-template-type]``&);
  2824. ``['unmentionable-expression-template-type]`` acos (const ``['number-or-expression-template-type]``&);
  2825. ``['number]`` acosh (const ``['number-or-expression-template-type]``&);
  2826. ``['unmentionable-expression-template-type]`` asin (const ``['number-or-expression-template-type]``&);
  2827. ``['number]`` asinh (const ``['number-or-expression-template-type]``&);
  2828. ``['unmentionable-expression-template-type]`` atan (const ``['number-or-expression-template-type]``&);
  2829. ``['unmentionable-expression-template-type]`` atan2 (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2830. ``['number]`` atanh (const ``['number-or-expression-template-type]``&);
  2831. ``['number]`` cbrt (const ``['number-or-expression-template-type]``&);
  2832. ``['unmentionable-expression-template-type]`` ceil (const ``['number-or-expression-template-type]``&);
  2833. ``['number]`` copysign (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2834. ``['unmentionable-expression-template-type]`` cos (const ``['number-or-expression-template-type]``&);
  2835. ``['unmentionable-expression-template-type]`` cosh (const ``['number-or-expression-template-type]``&);
  2836. ``['number]`` erf (const ``['number-or-expression-template-type]``&);
  2837. ``['number]`` erfc (const ``['number-or-expression-template-type]``&);
  2838. ``['unmentionable-expression-template-type]`` exp (const ``['number-or-expression-template-type]``&);
  2839. ``['unmentionable-expression-template-type]`` exp2 (const ``['number-or-expression-template-type]``&);
  2840. ``['number]`` expm1 (const ``['number-or-expression-template-type]``&);
  2841. ``['unmentionable-expression-template-type]`` fabs (const ``['number-or-expression-template-type]``&);
  2842. ``['unmentionable-expression-template-type]`` fdim (const ``['number-or-expression-template-type]``&);
  2843. ``['unmentionable-expression-template-type]`` floor (const ``['number-or-expression-template-type]``&);
  2844. ``['unmentionable-expression-template-type]`` fma (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2845. ``['unmentionable-expression-template-type]`` fmin (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2846. ``['unmentionable-expression-template-type]`` fmax (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2847. ``['unmentionable-expression-template-type]`` fmod (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2848. ``['unmentionable-expression-template-type]`` frexp (const ``['number-or-expression-template-type]``&, ``['integer-type]``*);
  2849. ``['unmentionable-expression-template-type]`` hypot (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2850. ``['integer-type]`` ilogb (const ``['number-or-expression-template-type]``&);
  2851. ``['unmentionable-expression-template-type]`` ldexp (const ``['number-or-expression-template-type]``&, ``['integer-type]``);
  2852. ``['number]`` lgamma (const ``['number-or-expression-template-type]``&);
  2853. long long llrint (const ``['number-or-expression-template-type]``&);
  2854. long long llround (const ``['number-or-expression-template-type]``&);
  2855. ``['unmentionable-expression-template-type]`` log (const ``['number-or-expression-template-type]``&);
  2856. ``['unmentionable-expression-template-type]`` log2 (const ``['number-or-expression-template-type]``&);
  2857. ``['unmentionable-expression-template-type]`` log10 (const ``['number-or-expression-template-type]``&);
  2858. ``['number]`` log1p (const ``['number-or-expression-template-type]``&);
  2859. ``['unmentionable-expression-template-type]`` logb (const ``['number-or-expression-template-type]``&);
  2860. long lrint (const ``['number-or-expression-template-type]``&);
  2861. long lround (const ``['number-or-expression-template-type]``&);
  2862. ``['unmentionable-expression-template-type]`` modf (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2863. ``['unmentionable-expression-template-type]`` nearbyint (const ``['number-or-expression-template-type]``&);
  2864. ``['number]`` nextafter (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2865. ``['number]`` nexttoward (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2866. ``['unmentionable-expression-template-type]`` pow (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2867. ``['unmentionable-expression-template-type]`` remainder (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2868. ``['unmentionable-expression-template-type]`` remquo (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&, int*);
  2869. ``['unmentionable-expression-template-type]`` rint (const ``['number-or-expression-template-type]``&);
  2870. ``['unmentionable-expression-template-type]`` round (const ``['number-or-expression-template-type]``&);
  2871. ``['unmentionable-expression-template-type]`` scalbn (const ``['number-or-expression-template-type]``&, ``['integer-type]``);
  2872. ``['unmentionable-expression-template-type]`` scalbln (const ``['number-or-expression-template-type]``&, ``['integer-type]``);
  2873. ``['unmentionable-expression-template-type]`` sin (const ``['number-or-expression-template-type]``&);
  2874. ``['unmentionable-expression-template-type]`` sinh (const ``['number-or-expression-template-type]``&);
  2875. ``['unmentionable-expression-template-type]`` sqrt (const ``['number-or-expression-template-type]``&);
  2876. ``['unmentionable-expression-template-type]`` tan (const ``['number-or-expression-template-type]``&);
  2877. ``['unmentionable-expression-template-type]`` tanh (const ``['number-or-expression-template-type]``&);
  2878. ``['number]`` tgamma (const ``['number-or-expression-template-type]``&);
  2879. ``['unmentionable-expression-template-type]`` trunc (const ``['number-or-expression-template-type]``&);
  2880. int fpclassify (const ``['number-or-expression-template-type]``&);
  2881. bool isfinite (const ``['number-or-expression-template-type]``&);
  2882. bool isinf (const ``['number-or-expression-template-type]``&);
  2883. bool isnan (const ``['number-or-expression-template-type]``&);
  2884. bool isnormal (const ``['number-or-expression-template-type]``&);
  2885. int signbit (const ``['number-or-expression-template-type]``&);
  2886. bool isgreater (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2887. bool isgreaterequal(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2888. bool isless (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2889. bool islessequal(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2890. bool islessgreater(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2891. bool isunordered(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2892. These functions all behave exactly as their standard library C++11 counterparts do: their argument is either an instance of `number` or
  2893. an expression template derived from it; If the argument is of type `number<Backend, et_off>` then that is also the return type,
  2894. otherwise the return type is an expression template unless otherwise stated.
  2895. The integer type arguments to `ldexp`, `frexp`, `scalbn` and `ilogb` may be either type `int`, or the actual
  2896. type of the exponent of the number type.
  2897. Complex number types support the following functions:
  2898. // Complex number functions:
  2899. ``['number<...>::value_type]`` real (const ``['number-or-expression-template-type]``&);
  2900. ``['number<...>::value_type]`` imag (const ``['number-or-expression-template-type]``&);
  2901. ``['number<...>::value_type]`` abs (const ``['number-or-expression-template-type]``&);
  2902. ``['number<...>::value_type]`` arg (const ``['number-or-expression-template-type]``&);
  2903. ``['number<...>::value_type]`` norm (const ``['number-or-expression-template-type]``&);
  2904. ``['number]`` conj (const ``['number-or-expression-template-type]``&);
  2905. ``['number]`` proj (const ``['number-or-expression-template-type]``&);
  2906. ``['number]`` polar (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2907. In addition the functions `real`, `imag`, `arg`, `norm`, `conj` and `proj` are overloaded for scalar (ie non-complex) types in the same
  2908. manner as `<complex>` and treat the argument as a value whose imaginary part is zero.
  2909. There are also some functions implemented for compatibility with the Boost.Math functions of the same name:
  2910. ``['unmentionable-expression-template-type]`` itrunc (const ``['number-or-expression-template-type]``&);
  2911. ``['unmentionable-expression-template-type]`` ltrunc (const ``['number-or-expression-template-type]``&);
  2912. ``['unmentionable-expression-template-type]`` lltrunc(const ``['number-or-expression-template-type]``&);
  2913. ``['unmentionable-expression-template-type]`` iround (const ``['number-or-expression-template-type]``&);
  2914. ``['number]`` changesign(const ``['number-or-expression-template-type]``&);
  2915. ``['number]`` copysign(const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  2916. All these functions are normally implemented by the Backend type. However, default versions are provided for Backend types that
  2917. don't have native support for these functions. Please note however, that this default support requires the precision of the type
  2918. to be a compile time constant - this means for example that the [gmp] MPF Backend will not work with these functions when that type is
  2919. used at variable precision.
  2920. Also note that with the exception of `abs` that these functions can only be used with floating-point Backend types (if any other types
  2921. such as fixed precision or complex types are added to the library later, then these functions may be extended to support those number types).
  2922. The precision of these functions is generally determined by the backend implementation. For example the precision
  2923. of these functions when used with __mpfr_float_backend is determined entirely by [mpfr]. When these functions use our own
  2924. implementations, the accuracy of the transcendental functions is generally a few epsilon. Note however, that the trigonometrical
  2925. functions incur the usual accuracy loss when reducing arguments by large multiples of [pi]. Also note that both __mpf_float
  2926. and __cpp_dec_float have a number of guard digits beyond their stated precision, so the error rates listed for these
  2927. are in some sense artificially low.
  2928. The following table shows the error rates we observe for these functions with various backend types, functions not listed
  2929. here are exact (tested on Win32 with VC++10, MPFR-3.0.0, MPIR-2.1.1):
  2930. [table
  2931. [[Function][mpfr_float_50][mpf_float_50][cpp_dec_float_50]]
  2932. [[sqrt][1eps][0eps][0eps]]
  2933. [[exp][1eps][0eps][0eps]]
  2934. [[log][1eps][0eps][0eps]]
  2935. [[log10][1eps][0eps][0eps]]
  2936. [[cos][700eps][0eps][0eps]]
  2937. [[sin][1eps][0eps][0eps]]
  2938. [[tan][0eps][0eps][0eps]]
  2939. [[acos][0eps][0eps][0eps]]
  2940. [[asin][0eps][0eps][0eps]]
  2941. [[atan][1eps][0eps][0eps]]
  2942. [[cosh][1045eps[footnote It's likely that the inherent error in the input values to our test cases are to blame here.]][0eps][0eps]]
  2943. [[sinh][2eps][0eps][0eps]]
  2944. [[tanh][1eps][0eps][0eps]]
  2945. [[pow][0eps][4eps][3eps]]
  2946. [[atan2][1eps][0eps][0eps]]
  2947. ]
  2948. [h4 Traits Class Support]
  2949. template <class T>
  2950. struct component_type;
  2951. If this is a type with multiple components (for example rational or complex types), then this trait has a single member
  2952. `type` that is the type of those components.
  2953. template <class T>
  2954. struct number_category;
  2955. A traits class that inherits from `mpl::int_<N>` where `N` is one of the enumerated values `number_kind_integer`, `number_kind_floating_point`,
  2956. `number_kind_rational`, `number_kind_fixed_point`, or `number_kind_unknown`. This traits class is specialized for any type that has
  2957. `std::numeric_limits` support as well as for classes in this library: which means it can be used for generic code that must work
  2958. with built in arithmetic types as well as multiprecision ones.
  2959. template <class T>
  2960. struct is_number;
  2961. A traits class that inherits from `mpl::true_` if T is an instance of `number<>`, otherwise from `mpl::false_`.
  2962. template <class T>
  2963. struct is_number_expression;
  2964. A traits class that inherits from `mpl::true_` if T is an expression template type derived from `number<>`, otherwise from `mpl::false_`.
  2965. [h4 Integer functions]
  2966. In addition to functioning with types from this library, these functions are also overloaded for built in integer
  2967. types if you include `<boost/multiprecision/integer.hpp>`. Further, when used with fixed precision types (whether
  2968. built in integers or multiprecision ones), the functions will promote to a wider type internally when the algorithm
  2969. requires it. Versions overloaded for built in integer types return that integer type rather than an expression
  2970. template.
  2971. ``['unmentionable-expression-template-type]`` gcd(const ``['number-or-expression-template-type]``& a, const ``['number-or-expression-template-type]``& b);
  2972. Returns the largest integer `x` that divides both `a` and `b`.
  2973. ``['unmentionable-expression-template-type]`` lcm(const ``['number-or-expression-template-type]``& a, const ``['number-or-expression-template-type]``& b);
  2974. Returns the smallest integer `x` that is divisible by both `a` and `b`.
  2975. ``['unmentionable-expression-template-type]`` pow(const ``['number-or-expression-template-type]``& b, unsigned p);
  2976. Returns ['b[super p]] as an expression template. Note that this function should be used with extreme care as the result can grow so
  2977. large as to take "effectively forever" to compute, or else simply run the host machine out of memory. This is the one function in
  2978. this category that is not overloaded for built in integer types, further, it's probably not a good idea to use it with
  2979. fixed precision `cpp_int`'s either.
  2980. ``['unmentionable-expression-template-type]`` powm(const ``['number-or-expression-template-type]``& b, const ``['number-or-expression-template-type]``& p, const ``['number-or-expression-template-type]``& m);
  2981. Returns ['b[super p] mod m] as an expression template. Fixed precision types are promoted internally to ensure accuracy.
  2982. ``['unmentionable-expression-template-type]`` sqrt(const ``['number-or-expression-template-type]``& a);
  2983. Returns the largest integer `x` such that `x * x < a`.
  2984. template <class Backend, expression_template_option ExpressionTemplates>
  2985. number<Backend, EXpressionTemplates> sqrt(const ``['number-or-expression-template-type]``& a, number<Backend, EXpressionTemplates>& r);
  2986. Returns the largest integer `x` such that `x * x < a`, and sets the remainder `r` such that `r = a - x * x`.
  2987. template <class Backend, expression_template_option ExpressionTemplates>
  2988. void divide_qr(const ``['number-or-expression-template-type]``& x, const ``['number-or-expression-template-type]``& y,
  2989. number<Backend, ExpressionTemplates>& q, number<Backend, ExpressionTemplates>& r);
  2990. Divides x by y and returns both the quotient and remainder. After the call `q = x / y` and `r = x % y`.
  2991. template <class Integer>
  2992. Integer integer_modulus(const ``['number-or-expression-template-type]``& x, Integer val);
  2993. Returns the absolute value of `x % val`.
  2994. unsigned lsb(const ``['number-or-expression-template-type]``& x);
  2995. Returns the (zero-based) index of the least significant bit that is set to 1.
  2996. Throws a `std::range_error` if the argument is <= 0.
  2997. unsigned msb(const ``['number-or-expression-template-type]``& x);
  2998. Returns the (zero-based) index of the most significant bit.
  2999. Throws a `std::range_error` if the argument is <= 0.
  3000. template <class Backend, class ExpressionTemplates>
  3001. bool bit_test(const number<Backend, ExpressionTemplates>& val, unsigned index);
  3002. Returns `true` if the bit at /index/ in /val/ is set.
  3003. template <class Backend, class ExpressionTemplates>
  3004. number<Backend, ExpressionTemplates>& bit_set(number<Backend, ExpressionTemplates>& val, unsigned index);
  3005. Sets the bit at /index/ in /val/, and returns /val/.
  3006. template <class Backend, class ExpressionTemplates>
  3007. number<Backend, ExpressionTemplates>& bit_unset(number<Backend, ExpressionTemplates>& val, unsigned index);
  3008. Unsets the bit at /index/ in /val/, and returns /val/.
  3009. template <class Backend, class ExpressionTemplates>
  3010. number<Backend, ExpressionTemplates>& bit_flip(number<Backend, ExpressionTemplates>& val, unsigned index);
  3011. Flips the bit at /index/ in /val/, and returns /val/.
  3012. template <class Engine>
  3013. bool miller_rabin_test(const ``['number-or-expression-template-type]``& n, unsigned trials, Engine& gen);
  3014. bool miller_rabin_test(const ``['number-or-expression-template-type]``& n, unsigned trials);
  3015. Tests to see if the number /n/ is probably prime - the test excludes the vast majority of composite numbers
  3016. by excluding small prime factors and performing a single Fermat test. Then performs /trials/ Miller-Rabin
  3017. tests. Returns `false` if /n/ is definitely composite, or `true` if /n/ is probably prime with the
  3018. probability of it being composite less than 0.25^trials. Fixed precision types are promoted internally
  3019. to ensure accuracy.
  3020. [h4 Rational Number Functions]
  3021. typename component_type<``['number-or-expression-template-type]``>::type numerator (const ``['number-or-expression-template-type]``&);
  3022. typename component_type<``['number-or-expression-template-type]``>::type denominator(const ``['number-or-expression-template-type]``&);
  3023. These functions return the numerator and denominator of a rational number respectively.
  3024. [h4 Boost.Math Interoperability Support]
  3025. namespace boost{ namespace math{
  3026. int fpclassify (const ``['number-or-expression-template-type]``&, int);
  3027. bool isfinite (const ``['number-or-expression-template-type]``&, int);
  3028. bool isnan (const ``['number-or-expression-template-type]``&, int);
  3029. bool isinf (const ``['number-or-expression-template-type]``&, int);
  3030. bool isnormal (const ``['number-or-expression-template-type]``&, int);
  3031. }} // namespaces
  3032. These floating-point classification functions behave exactly as their Boost.Math equivalents.
  3033. Other Boost.Math functions and templates may also be
  3034. specialized or overloaded to ensure interoperability.
  3035. [h4 std::numeric_limits support]
  3036. namespace std{
  3037. template <class Backend, ExpressionTemplates>
  3038. struct numeric_limits<boost::multiprecision<Backend, ExpressionTemplates> >
  3039. {
  3040. /* Usual members here */
  3041. };
  3042. }
  3043. Class template `std::numeric_limits` is specialized for all instantiations of `number` whose precision is known at compile time, plus those
  3044. types whose precision is unlimited (though it is much less useful in those cases). It is not specialized for types
  3045. whose precision can vary at compile time (such as `mpf_float`).
  3046. [endsect]
  3047. [section:cpp_int_ref cpp_int]
  3048. namespace boost{ namespace multiprecision{
  3049. typedef unspecified-type limb_type;
  3050. enum cpp_integer_type { signed_magnitude, unsigned_magnitude };
  3051. enum cpp_int_check_type { checked, unchecked };
  3052. template <unsigned MinDigits = 0,
  3053. unsigned MaxDits = 0,
  3054. cpp_integer_type SignType = signed_magnitude,
  3055. cpp_int_check_type Checked = unchecked,
  3056. class Allocator = std::allocator<limb_type> >
  3057. class cpp_int_backend;
  3058. //
  3059. // Expression templates default to et_off if there is no allocator:
  3060. //
  3061. template <unsigned MinDigits, unsigned MaxDigits, cpp_integer_type SignType, cpp_int_check_type Checked>
  3062. struct expression_template_default<cpp_int_backend<MinDigits, MaxDigits, SignType, Checked, void> >
  3063. { static const expression_template_option value = et_off; };
  3064. typedef number<cpp_int_backend<> > cpp_int; // arbitrary precision integer
  3065. typedef rational_adaptor<cpp_int_backend<> > cpp_rational_backend;
  3066. typedef number<cpp_rational_backend> cpp_rational; // arbitrary precision rational number
  3067. // Fixed precision unsigned types:
  3068. typedef number<cpp_int_backend<128, 128, unsigned_magnitude, unchecked, void> > uint128_t;
  3069. typedef number<cpp_int_backend<256, 256, unsigned_magnitude, unchecked, void> > uint256_t;
  3070. typedef number<cpp_int_backend<512, 512, unsigned_magnitude, unchecked, void> > uint512_t;
  3071. typedef number<cpp_int_backend<1024, 1024, unsigned_magnitude, unchecked, void> > uint1024_t;
  3072. // Fixed precision signed types:
  3073. typedef number<cpp_int_backend<128, 128, signed_magnitude, unchecked, void> > int128_t;
  3074. typedef number<cpp_int_backend<256, 256, signed_magnitude, unchecked, void> > int256_t;
  3075. typedef number<cpp_int_backend<512, 512, signed_magnitude, unchecked, void> > int512_t;
  3076. typedef number<cpp_int_backend<1024, 1024, signed_magnitude, unchecked, void> > int1024_t;
  3077. // Over again, but with checking enabled this time:
  3078. typedef number<cpp_int_backend<0, 0, signed_magnitude, checked> > checked_cpp_int;
  3079. typedef rational_adaptor<cpp_int_backend<0, 0, signed_magnitude, checked> > checked_cpp_rational_backend;
  3080. typedef number<checked_cpp_rational_backend> checked_cpp_rational;
  3081. // Checked fixed precision unsigned types:
  3082. typedef number<cpp_int_backend<128, 128, unsigned_magnitude, checked, void> > checked_uint128_t;
  3083. typedef number<cpp_int_backend<256, 256, unsigned_magnitude, checked, void> > checked_uint256_t;
  3084. typedef number<cpp_int_backend<512, 512, unsigned_magnitude, checked, void> > checked_uint512_t;
  3085. typedef number<cpp_int_backend<1024, 1024, unsigned_magnitude, checked, void> > checked_uint1024_t;
  3086. // Fixed precision signed types:
  3087. typedef number<cpp_int_backend<128, 128, signed_magnitude, checked, void> > checked_int128_t;
  3088. typedef number<cpp_int_backend<256, 256, signed_magnitude, checked, void> > checked_int256_t;
  3089. typedef number<cpp_int_backend<512, 512, signed_magnitude, checked, void> > checked_int512_t;
  3090. typedef number<cpp_int_backend<1024, 1024, signed_magnitude, checked, void> > checked_int1024_t;
  3091. }} // namespaces
  3092. Class template `cpp_int_backend` fulfils all of the requirements for a [link boost_multiprecision.ref.backendconc Backend] type.
  3093. Its members and non-member functions are deliberately not documented: these are considered implementation details that are subject
  3094. to change.
  3095. The template arguments are:
  3096. [variablelist
  3097. [[MinBits][Determines the number of Bits to store directly within the object before resorting to dynamic memory
  3098. allocation. When zero, this field is determined automatically based on how many bits can be stored
  3099. in union with the dynamic storage header: setting a larger value may improve performance as larger integer
  3100. values will be stored internally before memory allocation is required.]]
  3101. [[MaxBits][Determines the maximum number of bits to be stored in the type: resulting in a fixed precision type.
  3102. When this value is the same as MinBits, then the Allocator parameter is ignored, as no dynamic
  3103. memory allocation will ever be performed: in this situation the Allocator parameter should be set to
  3104. type `void`. Note that this parameter should not be used simply to prevent large memory
  3105. allocations, not only is that role better performed by the allocator, but fixed precision
  3106. integers have a tendency to allocate all of MaxBits of storage more often than one would expect.]]
  3107. [[SignType][Determines whether the resulting type is signed or not. Note that for
  3108. [@http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic arbitrary precision] types
  3109. this parameter must be `signed_magnitude`. For fixed precision
  3110. types then this type may be either `signed_magnitude` or `unsigned_magnitude`.]]
  3111. [[Checked][This parameter has two values: `checked` or `unchecked`. See the [link boost_multiprecision.tut.ints.cpp_int tutorial] for more information.]]
  3112. [[Allocator][The allocator to use for dynamic memory allocation, or type `void` if MaxBits == MinBits.]]
  3113. ]
  3114. The type of `number_category<cpp_int<Args...> >::type` is `mpl::int_<number_kind_integer>`.
  3115. More information on this type can be found in the [link boost_multiprecision.tut.ints.cpp_int tutorial].
  3116. [endsect]
  3117. [section:gmp_int_ref gmp_int]
  3118. namespace boost{ namespace multiprecision{
  3119. class gmp_int;
  3120. typedef number<gmp_int > mpz_int;
  3121. }} // namespaces
  3122. Class template `gmp_int` fulfils all of the requirements for a [link boost_multiprecision.ref.backendconc Backend] type.
  3123. Its members and non-member functions are deliberately not documented: these are considered implementation details that are subject
  3124. to change.
  3125. The type of `number_category<cpp_int<Args...> >::type` is `mpl::int_<number_kind_integer>`.
  3126. More information on this type can be found in the [link boost_multiprecision.tut.ints.gmp_int tutorial].
  3127. [endsect]
  3128. [section:tom_int_ref tom_int]
  3129. namespace boost{ namespace multiprecision{
  3130. class tommath_int;
  3131. typedef number<tommath_int > tom_int;
  3132. }} // namespaces
  3133. Class template `tommath_int` fulfils all of the requirements for a [link boost_multiprecision.ref.backendconc Backend] type.
  3134. Its members and non-member functions are deliberately not documented: these are considered implementation details that are subject
  3135. to change.
  3136. The type of `number_category<cpp_int<Args...> >::type` is `mpl::int_<number_kind_integer>`.
  3137. More information on this type can be found in the [link boost_multiprecision.tut.ints.tom_int tutorial].
  3138. [endsect]
  3139. [section:mpf_ref gmp_float]
  3140. namespace boost{ namespace multiprecision{
  3141. template <unsigned Digits10>
  3142. class gmp_float;
  3143. typedef number<gmp_float<50> > mpf_float_50;
  3144. typedef number<gmp_float<100> > mpf_float_100;
  3145. typedef number<gmp_float<500> > mpf_float_500;
  3146. typedef number<gmp_float<1000> > mpf_float_1000;
  3147. typedef number<gmp_float<0> > mpf_float;
  3148. }} // namespaces
  3149. Class template `gmp_float` fulfils all of the requirements for a [link boost_multiprecision.ref.backendconc Backend] type.
  3150. Its members and non-member functions are deliberately not documented: these are considered implementation details that are subject
  3151. to change.
  3152. The class takes a single template parameter - `Digits10` - which is the number of decimal digits precision the type
  3153. should support. When this parameter is zero, then the precision can be set at runtime via `number::default_precision`
  3154. and `number::precision`. Note that this type does not in any way change the GMP library's global state (for example
  3155. it does not change the default precision of the mpf_t data type), therefore you can safely mix this type with existing
  3156. code that uses GMP, and also mix `gmp_float`s of differing precision.
  3157. The type of `number_category<cpp_int<Args...> >::type` is `mpl::int_<number_kind_floating_point>`.
  3158. More information on this type can be found in the [link boost_multiprecision.tut.floats.gmp_float tutorial].
  3159. [endsect]
  3160. [section:mpfr_ref mpfr_float_backend]
  3161. namespace boost{ namespace multiprecision{
  3162. template <unsigned Digits10>
  3163. class mpfr_float_backend;
  3164. typedef number<mpfr_float_backend<50> > mpfr_float_50;
  3165. typedef number<mpfr_float_backend<100> > mpfr_float_100;
  3166. typedef number<mpfr_float_backend<500> > mpfr_float_500;
  3167. typedef number<mpfr_float_backend<1000> > mpfr_float_1000;
  3168. typedef number<mpfr_float_backend<0> > mpfr_float;
  3169. }} // namespaces
  3170. Class template `mpfr_float_backend` fulfils all of the requirements for a [link boost_multiprecision.ref.backendconc Backend] type.
  3171. Its members and non-member functions are deliberately not documented: these are considered implementation details that are subject
  3172. to change.
  3173. The class takes a single template parameter - `Digits10` - which is the number of decimal digits precision the type
  3174. should support. When this parameter is zero, then the precision can be set at runtime via `number::default_precision`
  3175. and `number::precision`. Note that this type does not in any way change the GMP or MPFR library's global state (for example
  3176. it does not change the default precision of the mpfr_t data type), therefore you can safely mix this type with existing
  3177. code that uses GMP or MPFR, and also mix `mpfr_float_backend`s of differing precision.
  3178. The type of `number_category<cpp_int<Args...> >::type` is `mpl::int_<number_kind_floating_point>`.
  3179. More information on this type can be found in the [link boost_multiprecision.tut.floats.mpfr_float tutorial].
  3180. [endsect]
  3181. [section:cpp_bin_float_ref cpp_bin_float]
  3182. namespace boost{ namespace multiprecision{
  3183. enum digit_base_type
  3184. {
  3185. digit_base_2 = 2,
  3186. digit_base_10 = 10
  3187. };
  3188. template <unsigned Digits, digit_base_type base = digit_base_10, class Allocator = void, class Exponent = int, ExponentMin = 0, ExponentMax = 0>
  3189. class cpp_bin_float;
  3190. typedef number<cpp_bin_float<50> > cpp_bin_float_50;
  3191. typedef number<cpp_bin_float<100> > cpp_bin_float_100;
  3192. typedef number<backends::cpp_bin_float<24, backends::digit_base_2, void, boost::int16_t, -126, 127>, et_off> cpp_bin_float_single;
  3193. typedef number<backends::cpp_bin_float<53, backends::digit_base_2, void, boost::int16_t, -1022, 1023>, et_off> cpp_bin_float_double;
  3194. typedef number<backends::cpp_bin_float<64, backends::digit_base_2, void, boost::int16_t, -16382, 16383>, et_off> cpp_bin_float_double_extended;
  3195. typedef number<backends::cpp_bin_float<113, backends::digit_base_2, void, boost::int16_t, -16382, 16383>, et_off> cpp_bin_float_quad;
  3196. typedef number<backends::cpp_bin_float<237, backends::digit_base_2, void, boost::int32_t, -262142, 262143>, et_off> cpp_bin_float_oct;
  3197. }} // namespaces
  3198. Class template `cpp_bin_float` fulfils all of the requirements for a [link boost_multiprecision.ref.backendconc Backend] type.
  3199. Its members and non-member functions are deliberately not documented: these are considered implementation details that are subject
  3200. to change.
  3201. The class takes six template parameters:
  3202. [variablelist
  3203. [[Digits][The number of digits precision the type
  3204. should support. This is normally expressed as base-10 digits, but that can be changed via the second template parameter.]]
  3205. [[base][An enumerated value (either `digit_base_10` or `digit_base_2`) that indicates whether `Digits` is base-10 or base-2]]
  3206. [[Allocator][The allocator used: defaults to type `void`, meaning all storage is within the class, and no dynamic
  3207. allocation is performed, but can be set to a standard library allocator if dynamic allocation makes more sense.]]
  3208. [[Exponent][A signed integer type to use as the type of the exponent - defaults to `int`.]]
  3209. [[ExponentMin][The smallest (most negative) permitted exponent, defaults to zero, meaning "define as small as possible
  3210. given the limitations of the type and our internal requirements".]]
  3211. [[ExponentMax][The largest (most positive) permitted exponent, defaults to zero, meaning "define as large as possible
  3212. given the limitations of the type and our internal requirements".]]
  3213. ]
  3214. The type of `number_category<cpp_bin_float<Args...> >::type` is `mpl::int_<number_kind_floating_point>`.
  3215. More information on this type can be found in the [link boost_multiprecision.tut.floats.cpp_bin_float tutorial].
  3216. [h4 Implementation Notes]
  3217. Internally, an N-bit `cpp_bin_float` is represented as an N-bit unsigned integer along with an exponent and a sign.
  3218. The integer part is normalized so that it's most significant bit is always 1. The decimal point is assumed to be
  3219. directly after the most significant bit of the integer part. The special values zero, infinity and NaN all have
  3220. the integer part set to zero, and the exponent to one of 3 special values above the maximum permitted exponent.
  3221. Multiplication is trivial: multiply the two N-bit integer mantissa's to obtain a 2N-bit number, then round and
  3222. adjust the sign and exponent.
  3223. Addition and subtraction proceed similarly - if the exponents are such that there is overlap between the two
  3224. values, then left shift the larger value to produce a number with between N and 2N bits, then perform integer
  3225. addition or subtraction, round, and adjust the exponent.
  3226. Division proceeds as follows: first scale the numerator by some power of 2 so that integer division will
  3227. produce either an N-bit or N+1 bit result plus a remainder. If we get an N bit result then the size of
  3228. twice the remainder compared to the denominator gives us the rounding direction. Otherwise we have one extra bit
  3229. in the result which we can use to determine rounding (in this case ties occur only if the remainder is zero and
  3230. the extra bit is a 1).
  3231. Square root uses integer square root in a manner analogous to division.
  3232. Decimal string to binary conversion proceeds as follows: first parse the digits to
  3233. produce an integer multiplied by a decimal exponent. Note that we stop parsing digits
  3234. once we have parsed as many as can possibly effect the result - this stops the integer
  3235. part growing too large when there are a very large number of input digits provided.
  3236. At this stage if the decimal exponent is positive then the result is an integer and we
  3237. can in principle simply multiply by 10^N to get an exact integer result. In practice
  3238. however, that could produce some very large integers. We also need to be able to divide
  3239. by 10^N in the event that the exponent is negative. Therefore calculation of the 10^N
  3240. values plus the multiplication or division are performed using limited precision
  3241. integer arithmetic, plus an exponent, and a track of the accumulated error. At the end of
  3242. the calculation we will either be able to round unambiguously, or the error will be such
  3243. that we can't tell which way to round. In the latter case we simply up the precision and try
  3244. again until we have an unambiguously rounded result.
  3245. Binary to decimal conversion proceeds very similarly to the above, our aim is to calculate
  3246. `mantissa * 2^shift * 10^E` where `E` is the decimal exponent and `shift` is calculated
  3247. so that the result is an N bit integer assuming we want N digits printed in the result.
  3248. As before we use limited precision arithmetic to calculate the result and up the
  3249. precision as necessary until the result is unambiguously correctly rounded. In addition
  3250. our initial calculation of the decimal exponent may be out by 1, so we have to correct
  3251. that and loop as well in the that case.
  3252. [endsect]
  3253. [section:cpp_dec_ref cpp_dec_float]
  3254. namespace boost{ namespace multiprecision{
  3255. template <unsigned Digits10, class ExponentType = boost::int32_t, class Allocator = void>
  3256. class cpp_dec_float;
  3257. typedef number<cpp_dec_float<50> > cpp_dec_float_50;
  3258. typedef number<cpp_dec_float<100> > cpp_dec_float_100;
  3259. }} // namespaces
  3260. Class template `cpp_dec_float` fulfils all of the requirements for a [link boost_multiprecision.ref.backendconc Backend] type.
  3261. Its members and non-member functions are deliberately not documented: these are considered implementation details that are subject
  3262. to change.
  3263. The class takes three template parameters:
  3264. [variablelist
  3265. [[Digits10][The number of decimal digits precision the type
  3266. should support. Note that this type does not normally perform any dynamic memory allocation, and as a result the `Digits10`
  3267. template argument should not be set too high or the class's size will grow unreasonably large.]]
  3268. [[ExponentType][A signed integer type that represents the exponent of the number]]
  3269. [[Allocator][The allocator used: defaults to type `void`, meaning all storage is within the class, and no dynamic
  3270. allocation is performed, but can be set to a standard library allocator if dynamic allocation makes more sense.]]
  3271. ]
  3272. The type of `number_category<cpp_dec_float<Args...> >::type` is `mpl::int_<number_kind_floating_point>`.
  3273. More information on this type can be found in the [link boost_multiprecision.tut.floats.cpp_dec_float tutorial].
  3274. [endsect]
  3275. [section:internals Internal Support Code]
  3276. There are some traits classes which authors of new backends should be aware of:
  3277. namespace boost{ namespace multiprecision{ namespace detail{
  3278. template<typename From, typename To>
  3279. struct is_explicitly_convertible;
  3280. }}}
  3281. Inherits from `boost::integral_constant<bool,true>` if type `From` has an explicit conversion from `To`.
  3282. For compilers that support C++11 SFINAE-expressions this trait should "just work". Otherwise it inherits
  3283. from `boost::is_convertible<From, To>::type`, and will need to be specialised for Backends that have
  3284. constructors marked as `explicit`.
  3285. template <class From, class To>
  3286. struct is_lossy_conversion
  3287. {
  3288. static const bool value = see below;
  3289. };
  3290. Member `value` is true if the conversion from `From` to `To` would result in a loss of precision, and `false` otherwise.
  3291. The default version of this trait simply checks whether the ['kind] of conversion (for example from a floating-point to an integer type)
  3292. is inherently lossy. Note that if either of the types `From` or `To` are of an unknown number category (because `number_category` is not
  3293. specialised for that type) then this trait will be `true`.
  3294. template<typename From, typename To>
  3295. struct is_restricted_conversion
  3296. {
  3297. static const bool value = see below;
  3298. };
  3299. Member `value` is `true` if `From` is only explicitly convertible to `To` and not implicitly convertible, or
  3300. if `is_lossy_conversion<From, To>::value` is `true`. Otherwise `false`.
  3301. Note that while this trait is the ultimate arbiter of which constructors are marked as `explicit` in class `number`,
  3302. authors of backend types should generally specialise one of the traits above, rather than this one directly.
  3303. template <class T>
  3304. is_signed_number;
  3305. template <class T>
  3306. is_unsigned_number;
  3307. These two traits inherit from either `mpl::true_` or `mpl::false_`, by default types are assumed to be signed unless
  3308. `is_unsigned_number` is specialized for that type.
  3309. [endsect]
  3310. [section:backendconc Backend Requirements]
  3311. The requirements on the `Backend` template argument to `number` are split up into
  3312. sections: compulsory and optional.
  3313. Compulsory requirements have no default implementation in the library, therefore if the feature
  3314. they implement is to be supported at all, then they must be implemented by the backend.
  3315. Optional requirements have default implementations that are called if the backend doesn't provide
  3316. it's own. Typically the backend will implement these to improve performance.
  3317. In the following tables, type B is the `Backend` template argument to `number`, `b` and `b2` are
  3318. a variables of type B, `pb` is a variable of type B*, `cb`, `cb2` and `cb3` are constant variables of type `const B`,
  3319. `rb` is a variable of type `B&&`, `a` and `a2` are variables of Arithmetic type,
  3320. `s` is a variable of type `const char*`, `ui` is a variable of type `unsigned`, `bb` is a variable of type `bool`,
  3321. `pa` is a variable of type pointer-to-arithmetic-type, `exp` is a variable of type `B::exp_type`,
  3322. `pexp` is a variable of type `B::exp_type*`, `i` is a variable of type `int`, `pi` pointer to a variable of type `int`,
  3323. B2 is another type that meets these requirements, b2 is a variable of type B2, `ss` is variable of type `std::streamsize`
  3324. and `ff` is a variable of type `std::ios_base::fmtflags`.
  3325. [table Compulsory Requirements on the Backend type.
  3326. [[Expression][Return Type][Comments][Throws]]
  3327. [[`B::signed_types`][`mpl::list<type-list>`][A list of signed integral types that can be assigned to type B. The types shall be
  3328. listed in order of size, smallest first, and shall terminate in the type that is `std::intmax_t`.][[space]]]
  3329. [[`B::unsigned_types`][`mpl::list<type-list>`][A list of unsigned integral types that can be assigned to type B. The types shall be
  3330. listed in order of size, smallest first, and shall terminate in the type that is `std::uintmax_t`.][[space]]]
  3331. [[`B::float_types`][`mpl::list<type-list>`][A list of floating-point types that can be assigned to type B.The types shall be
  3332. listed in order of size, smallest first, and shall terminate in type `long double`.][[space]]]
  3333. [[`B::exponent_type`][A signed integral type.][The type of the exponent of type B. This type is required only for floating-point types.][[space]]]
  3334. [[`B()`][ ][Default constructor.][[space]]]
  3335. [[`B(cb)`][ ][Copy Constructor.][[space]]]
  3336. [[`b = b`][`B&`][Assignment operator.][[space]]]
  3337. [[`b = a`][`B&`][Assignment from an Arithmetic type. The type of `a` shall be listed in one of the type lists
  3338. `B::signed_types`, `B::unsigned_types` or `B::float_types`.][[space]]]
  3339. [[`b = s`][`B&`][Assignment from a string.][Throws a `std::runtime_error` if the string could not be interpreted as a valid number.]]
  3340. [[`b.swap(b)`][`void`][Swaps the contents of its arguments.][`noexcept`]]
  3341. [[`cb.str(ss, ff)`][`std::string`][Returns the string representation of `b` with `ss` digits and formatted according to the flags set in `ff`.
  3342. If `ss` is zero, then returns as many digits as are required to reconstruct the original value.][[space]]]
  3343. [[`b.negate()`][`void`][Negates `b`.][[space]]]
  3344. [[`cb.compare(cb2)`][`int`][Compares `cb` and `cb2`, returns a value less than zero if `cb < cb2`, a value greater than zero if `cb > cb2` and zero
  3345. if `cb == cb2`.][`noexcept`]]
  3346. [[`cb.compare(a)`][`int`][Compares `cb` and `a`, returns a value less than zero if `cb < a`, a value greater than zero if `cb > a` and zero
  3347. if `cb == a`. The type of `a` shall be listed in one of the type lists
  3348. `B::signed_types`, `B::unsigned_types` or `B::float_types`.][[space]]]
  3349. [[`eval_add(b, cb)`][`void`][Adds `cb` to `b`.][[space]]]
  3350. [[`eval_subtract(b, cb)`][`void`][Subtracts `cb` from `b`.][[space]]]
  3351. [[`eval_multiply(b, cb)`][`void`][Multiplies `b` by `cb`.][[space]]]
  3352. [[`eval_divide(b, cb)`][`void`][Divides `b` by `cb`.]
  3353. [`std::overflow_error` if cb has the value zero, and `std::numeric_limits<number<B> >::has_infinity == false`]]
  3354. [[`eval_modulus(b, cb)`][`void`][Computes `b %= cb`, only required when `B` is an integer type.]
  3355. [`std::overflow_error` if cb has the value zero.]]
  3356. [[`eval_bitwise_and(b, cb)`][`void`][Computes `b &= cb`, only required when `B` is an integer type.][[space]]]
  3357. [[`eval_bitwise_or(b, cb)`][`void`][Computes `b |= cb`, only required when `B` is an integer type.][[space]]]
  3358. [[`eval_bitwise_xor(b, cb)`][`void`][Computes `b ^= cb`, only required when `B` is an integer type.][[space]]]
  3359. [[`eval_complement(b, cb)`][`void`][Computes the ones-complement of `cb` and stores the result in `b`, only required when `B` is an integer type.][[space]]]
  3360. [[`eval_left_shift(b, ui)`][`void`][Computes `b <<= ui`, only required when `B` is an integer type.][[space]]]
  3361. [[`eval_right_shift(b, ui)`][`void`][Computes `b >>= ui`, only required when `B` is an integer type.][[space]]]
  3362. [[`eval_convert_to(pa, cb)`][`void`][Converts `cb` to the type of `*pa` and store the result in `*pa`. Type `B` shall support
  3363. conversion to at least types `std::intmax_t`, `std::uintmax_t` and `long long`.
  3364. Conversion to other arithmetic types can then be synthesised using other operations.
  3365. Conversions to other types are entirely optional.][[space]]]
  3366. [[`eval_frexp(b, cb, pexp)`][`void`][Stores values in `b` and `*pexp` such that the value of `cb` is b * 2[super *pexp], only required when `B` is a floating-point type.][[space]]]
  3367. [[`eval_ldexp(b, cb, exp)`][`void`][Stores a value in `b` that is cb * 2[super exp], only required when `B` is a floating-point type.][[space]]]
  3368. [[`eval_frexp(b, cb, pi)`][`void`][Stores values in `b` and `*pi` such that the value of `cb` is b * 2[super *pi], only required when `B` is a floating-point type.]
  3369. [`std::runtime_error` if the exponent of cb is too large to be stored in an `int`.]]
  3370. [[`eval_ldexp(b, cb, i)`][`void`][Stores a value in `b` that is cb * 2[super i], only required when `B` is a floating-point type.][[space]]]
  3371. [[`eval_floor(b, cb)`][`void`][Stores the floor of `cb` in `b`, only required when `B` is a floating-point type.][[space]]]
  3372. [[`eval_ceil(b, cb)`][`void`][Stores the ceiling of `cb` in `b`, only required when `B` is a floating-point type.][[space]]]
  3373. [[`eval_sqrt(b, cb)`][`void`][Stores the square root of `cb` in `b`, only required when `B` is a floating-point type.][[space]]]
  3374. [[`boost::multiprecision::number_category<B>::type`][`mpl::int_<N>`][`N` is one of the values `number_kind_integer`, `number_kind_floating_point`, `number_kind_complex`, `number_kind_rational` or `number_kind_fixed_point`.
  3375. Defaults to `number_kind_floating_point`.][[space]]]
  3376. [[`eval_conj(b, cb)`][`void`][Sets `b` to the complex conjugate of `cb`. Required for complex types only - other types have a sensible default.][[space]]]
  3377. [[`eval_proj(b, cb)`][`void`][Sets `b` to the Riemann projection of `cb`. Required for complex types only - other types have a sensible default.][[space]]]
  3378. [[`eval_real(b, cb)`][`void`][Sets `b` to the real part of `cb`. Required for complex types only - other types have a sensible default.][[space]]]
  3379. [[`eval_imag(b, cb)`][`void`][Sets `b` to the imaginary of `cb`. Required for complex types only - other types have a sensible default.][[space]]]
  3380. [[`eval_set_real(b, a)`][`void`][Sets the real part of `b` to `cb`. Required for complex types only - other types have a sensible default.][[space]]]
  3381. [[`eval_set_imag(b, a)`][`void`][Sets the imaginary part of `b` to `cb`. Required for complex types only - other types have a sensible default.][[space]]]
  3382. ]
  3383. [table Optional Requirements on the Backend Type
  3384. [[Expression][Returns][Comments][Throws]]
  3385. [[['Construct and assign:]]]
  3386. [[`B(rb)`][`B`][Move constructor. Afterwards variable `rb` shall be in sane state, albeit with unspecified value.
  3387. Only destruction and assignment to the moved-from variable `rb` need be supported after the operation.][`noexcept`]]
  3388. [[`b = rb`][`B&`][Move-assign. Afterwards variable `rb` shall be in sane state, albeit with unspecified value.
  3389. Only destruction and assignment to the moved-from variable `rb` need be supported after the operation.][`noexcept`]]
  3390. [[`B(a)`][`B`][Direct construction from an arithmetic type. The type of `a` shall be listed in one of the type lists
  3391. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3392. When not provided, this operation is simulated using default-construction followed by assignment.][[space]]]
  3393. [[`B(b2)`][`B`][Copy constructor from a different back-end type. When not provided, a generic interconversion routine is used.
  3394. This constructor may be `explicit` if the corresponding frontend constructor should also be `explicit`.][[space]]]
  3395. [[`b = b2`][`b&`][Assignment operator from a different back-end type. When not provided, a generic interconversion routine is used.][[space]]]
  3396. [[`assign_components(b, a, a)`][`void`][Assigns to `b` the two components in the following arguments.
  3397. Only applies to rational and complex number types.
  3398. When not provided, arithmetic operations are used to synthesise the result from the two values.][[space]]]
  3399. [[`assign_components(b, b2, b2)`][`void`][Assigns to `b` the two components in the following arguments.
  3400. Only applies to rational and complex number types.
  3401. When not provided, arithmetic operations are used to synthesise the result from the two values.][[space]]]
  3402. [[['Comparisons:]]]
  3403. [[`eval_eq(cb, cb2)`][`bool`][Returns `true` if `cb` and `cb2` are equal in value.
  3404. When not provided, the default implementation returns `cb.compare(cb2) == 0`.][`noexcept`]]
  3405. [[`eval_eq(cb, a)`][`bool`][Returns `true` if `cb` and `a` are equal in value.
  3406. The type of `a` shall be listed in one of the type lists
  3407. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3408. When not provided, return the equivalent of `eval_eq(cb, B(a))`.][[space]]]
  3409. [[`eval_eq(a, cb)`][`bool`][Returns `true` if `cb` and `a` are equal in value.
  3410. The type of `a` shall be listed in one of the type lists
  3411. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3412. When not provided, the default version returns `eval_eq(cb, a)`.][[space]]]
  3413. [[`eval_lt(cb, cb2)`][`bool`][Returns `true` if `cb` is less than `cb2` in value.
  3414. When not provided, the default implementation returns `cb.compare(cb2) < 0`.][`noexcept`]]
  3415. [[`eval_lt(cb, a)`][`bool`][Returns `true` if `cb` is less than `a` in value.
  3416. The type of `a` shall be listed in one of the type lists
  3417. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3418. When not provided, the default implementation returns `eval_lt(cb, B(a))`.][[space]]]
  3419. [[`eval_lt(a, cb)`][`bool`][Returns `true` if `a` is less than `cb` in value.
  3420. The type of `a` shall be listed in one of the type lists
  3421. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3422. When not provided, the default implementation returns `eval_gt(cb, a)`.][[space]]]
  3423. [[`eval_gt(cb, cb2)`][`bool`][Returns `true` if `cb` is greater than `cb2` in value.
  3424. When not provided, the default implementation returns `cb.compare(cb2) > 0`.][`noexcept`]]
  3425. [[`eval_gt(cb, a)`][`bool`][Returns `true` if `cb` is greater than `a` in value.
  3426. The type of `a` shall be listed in one of the type lists
  3427. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3428. When not provided, the default implementation returns `eval_gt(cb, B(a))`.][[space]]]
  3429. [[`eval_gt(a, cb)`][`bool`][Returns `true` if `a` is greater than `cb` in value.
  3430. The type of `a` shall be listed in one of the type lists
  3431. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3432. When not provided, the default implementation returns `eval_lt(cb, a)`.][[space]]]
  3433. [[`eval_is_zero(cb)`][`bool`][Returns `true` if `cb` is zero, otherwise `false`. The default version of this function
  3434. returns `cb.compare(ui_type(0)) == 0`, where `ui_type` is `ui_type` is
  3435. `typename mpl::front<typename B::unsigned_types>::type`.][[space]]]
  3436. [[`eval_get_sign(cb)`][`int`][Returns a value < zero if `cb` is negative, a value > zero if `cb` is positive, and zero if `cb` is zero.
  3437. The default version of this function
  3438. returns `cb.compare(ui_type(0))`, where `ui_type` is `ui_type` is
  3439. `typename mpl::front<typename B::unsigned_types>::type`.][[space]]]
  3440. [[['Basic arithmetic:]]]
  3441. [[`eval_add(b, a)`][`void`][Adds `a` to `b`. The type of `a` shall be listed in one of the type lists
  3442. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3443. When not provided, the default version calls `eval_add(b, B(a))`][[space]]]
  3444. [[`eval_add(b, cb, cb2)`][`void`][Add `cb` to `cb2` and stores the result in `b`.
  3445. When not provided, does the equivalent of `b = cb; eval_add(b, cb2)`.][[space]]]
  3446. [[`eval_add(b, cb, a)`][`void`][Add `cb` to `a` and stores the result in `b`. The type of `a` shall be listed in one of the type lists
  3447. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3448. When not provided, does the equivalent of `eval_add(b, cb, B(a))`.][[space]]]
  3449. [[`eval_add(b, a, cb)`][`void`][Add `a` to `cb` and stores the result in `b`. The type of `a` shall be listed in one of the type lists
  3450. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3451. When not provided, does the equivalent of `eval_add(b, cb, a)`.][[space]]]
  3452. [[`eval_subtract(b, a)`][`void`][Subtracts `a` from `b`. The type of `a` shall be listed in one of the type lists
  3453. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3454. When not provided, the default version calls `eval_subtract(b, B(a))`][[space]]]
  3455. [[`eval_subtract(b, cb, cb2)`][`void`][Subtracts `cb2` from `cb` and stores the result in `b`.
  3456. When not provided, does the equivalent of `b = cb; eval_subtract(b, cb2)`.][[space]]]
  3457. [[`eval_subtract(b, cb, a)`][`void`][Subtracts `a` from `cb` and stores the result in `b`. The type of `a` shall be listed in one of the type lists
  3458. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3459. When not provided, does the equivalent of `eval_subtract(b, cb, B(a))`.][[space]]]
  3460. [[`eval_subtract(b, a, cb)`][`void`][Subtracts `cb` from `a` and stores the result in `b`. The type of `a` shall be listed in one of the type lists
  3461. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3462. When not provided, does the equivalent of `eval_subtract(b, cb, a); b.negate();`.][[space]]]
  3463. [[`eval_multiply(b, a)`][`void`][Multiplies `b` by `a`. The type of `a` shall be listed in one of the type lists
  3464. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3465. When not provided, the default version calls `eval_multiply(b, B(a))`][[space]]]
  3466. [[`eval_multiply(b, cb, cb2)`][`void`][Multiplies `cb` by `cb2` and stores the result in `b`.
  3467. When not provided, does the equivalent of `b = cb; eval_multiply(b, cb2)`.][[space]]]
  3468. [[`eval_multiply(b, cb, a)`][`void`][Multiplies `cb` by `a` and stores the result in `b`. The type of `a` shall be listed in one of the type lists
  3469. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3470. When not provided, does the equivalent of `eval_multiply(b, cb, B(a))`.][[space]]]
  3471. [[`eval_multiply(b, a, cb)`][`void`][Multiplies `a` by `cb` and stores the result in `b`. The type of `a` shall be listed in one of the type lists
  3472. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3473. When not provided, does the equivalent of `eval_multiply(b, cb, a)`.][[space]]]
  3474. [[`eval_multiply_add(b, cb, cb2)`][`void`][Multiplies `cb` by `cb2` and adds the result to `b`.
  3475. When not provided does the equivalent of creating a temporary `B t` and `eval_multiply(t, cb, cb2)` followed by
  3476. `eval_add(b, t)`.][[space]]]
  3477. [[`eval_multiply_add(b, cb, a)`][`void`][Multiplies `a` by `cb` and adds the result to `b`.
  3478. The type of `a` shall be listed in one of the type lists
  3479. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3480. When not provided does the equivalent of creating a temporary `B t` and `eval_multiply(t, cb, a)` followed by
  3481. `eval_add(b, t)`.][[space]]]
  3482. [[`eval_multiply_add(b, a, cb)`][`void`][Multiplies `a` by `cb` and adds the result to `b`.
  3483. The type of `a` shall be listed in one of the type lists
  3484. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3485. When not provided does the equivalent of `eval_multiply_add(b, cb, a)`.][[space]]]
  3486. [[`eval_multiply_subtract(b, cb, cb2)`][`void`][Multiplies `cb` by `cb2` and subtracts the result from `b`.
  3487. When not provided does the equivalent of creating a temporary `B t` and `eval_multiply(t, cb, cb2)` followed by
  3488. `eval_subtract(b, t)`.][[space]]]
  3489. [[`eval_multiply_subtract(b, cb, a)`][`void`][Multiplies `a` by `cb` and subtracts the result from `b`.
  3490. The type of `a` shall be listed in one of the type lists
  3491. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3492. When not provided does the equivalent of creating a temporary `B t` and `eval_multiply(t, cb, a)` followed by
  3493. `eval_subtract(b, t)`.][[space]]]
  3494. [[`eval_multiply_subtract(b, a, cb)`][`void`][Multiplies `a` by `cb` and subtracts the result from `b`.
  3495. The type of `a` shall be listed in one of the type lists
  3496. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3497. When not provided does the equivalent of `eval_multiply_subtract(b, cb, a)`.][[space]]]
  3498. [[`eval_multiply_add(b, cb, cb2, cb3)`][`void`][Multiplies `cb` by `cb2` and adds the result to `cb3` storing the result in `b`.
  3499. When not provided does the equivalent of `eval_multiply(b, cb, cb2)` followed by
  3500. `eval_add(b, cb3)`.
  3501. For brevity, only a version showing all arguments of type `B` is shown here, but you can replace up to any 2 of
  3502. `cb`, `cb2` and `cb3` with any type listed in one of the type lists
  3503. `B::signed_types`, `B::unsigned_types` or `B::float_types`.][[space]]]
  3504. [[`eval_multiply_subtract(b, cb, cb2, cb3)`][`void`][Multiplies `cb` by `cb2` and subtracts from the result `cb3` storing the result in `b`.
  3505. When not provided does the equivalent of `eval_multiply(b, cb, cb2)` followed by
  3506. `eval_subtract(b, cb3)`.
  3507. For brevity, only a version showing all arguments of type `B` is shown here, but you can replace up to any 2 of
  3508. `cb`, `cb2` and `cb3` with any type listed in one of the type lists
  3509. `B::signed_types`, `B::unsigned_types` or `B::float_types`.][[space]]]
  3510. [[`eval_divide(b, a)`][`void`][Divides `b` by `a`. The type of `a` shall be listed in one of the type lists
  3511. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3512. When not provided, the default version calls `eval_divide(b, B(a))`]
  3513. [`std::overflow_error` if `a` has the value zero, and `std::numeric_limits<number<B> >::has_infinity == false`]]
  3514. [[`eval_divide(b, cb, cb2)`][`void`][Divides `cb` by `cb2` and stores the result in `b`.
  3515. When not provided, does the equivalent of `b = cb; eval_divide(b, cb2)`.]
  3516. [`std::overflow_error` if `cb2` has the value zero, and `std::numeric_limits<number<B> >::has_infinity == false`]]
  3517. [[`eval_divide(b, cb, a)`][`void`][Divides `cb` by `a` and stores the result in `b`. The type of `a` shall be listed in one of the type lists
  3518. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3519. When not provided, does the equivalent of `eval_divide(b, cb, B(a))`.]
  3520. [`std::overflow_error` if `a` has the value zero, and `std::numeric_limits<number<B> >::has_infinity == false`]]
  3521. [[`eval_divide(b, a, cb)`][`void`][Divides `a` by `cb` and stores the result in `b`. The type of `a` shall be listed in one of the type lists
  3522. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3523. When not provided, does the equivalent of `eval_divide(b, B(a), cb)`.]
  3524. [`std::overflow_error` if cb has the value zero, and `std::numeric_limits<number<B> >::has_infinity == false`]]
  3525. [[`eval_increment(b)`][void][Increments the value of `b` by one.
  3526. When not provided, does the equivalent of `eval_add(b, static_cast<ui_type>(1u))`.
  3527. Where `ui_type` is `typename mpl::front<typename B::unsigned_types>::type`.][[space]]]
  3528. [[`eval_decrement(b)`][void][Decrements the value of `b` by one.
  3529. When not provided, does the equivalent of `eval_subtract(b, static_cast<ui_type>(1u))`.
  3530. Where `ui_type` is `typename mpl::front<typename B::unsigned_types>::type`.][[space]]]
  3531. [[['Integer specific operations:]]]
  3532. [[`eval_modulus(b, a)`][`void`][Computes `b %= cb`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3533. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3534. When not provided, the default version calls `eval_modulus(b, B(a))`]
  3535. [`std::overflow_error` if `a` has the value zero.]]
  3536. [[`eval_modulus(b, cb, cb2)`][`void`][Computes `cb % cb2` and stores the result in `b`, only required when `B` is an integer type.
  3537. When not provided, does the equivalent of `b = cb; eval_modulus(b, cb2)`.]
  3538. [`std::overflow_error` if `a` has the value zero.]]
  3539. [[`eval_modulus(b, cb, a)`][`void`][Computes `cb % a` and stores the result in `b`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3540. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3541. When not provided, does the equivalent of `eval_modulus(b, cb, B(a))`.]
  3542. [`std::overflow_error` if `a` has the value zero.]]
  3543. [[`eval_modulus(b, a, cb)`][`void`][Computes `cb % a` and stores the result in `b`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3544. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3545. When not provided, does the equivalent of `eval_modulus(b, B(a), cb)`.]
  3546. [`std::overflow_error` if `a` has the value zero.]]
  3547. [[`eval_bitwise_and(b, a)`][`void`][Computes `b &= cb`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3548. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3549. When not provided, the default version calls `eval_bitwise_and(b, B(a))`][[space]]]
  3550. [[`eval_bitwise_and(b, cb, cb2)`][`void`][Computes `cb & cb2` and stores the result in `b`, only required when `B` is an integer type.
  3551. When not provided, does the equivalent of `b = cb; eval_bitwise_and(b, cb2)`.][[space]]]
  3552. [[`eval_bitwise_and(b, cb, a)`][`void`][Computes `cb & a` and stores the result in `b`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3553. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3554. When not provided, does the equivalent of `eval_bitwise_and(b, cb, B(a))`.][[space]]]
  3555. [[`eval_bitwise_and(b, a, cb)`][`void`][Computes `cb & a` and stores the result in `b`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3556. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3557. When not provided, does the equivalent of `eval_bitwise_and(b, cb, a)`.][[space]]]
  3558. [[`eval_bitwise_or(b, a)`][`void`][Computes `b |= cb`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3559. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3560. When not provided, the default version calls `eval_bitwise_or(b, B(a))`][[space]]]
  3561. [[`eval_bitwise_or(b, cb, cb2)`][`void`][Computes `cb | cb2` and stores the result in `b`, only required when `B` is an integer type.
  3562. When not provided, does the equivalent of `b = cb; eval_bitwise_or(b, cb2)`.][[space]]]
  3563. [[`eval_bitwise_or(b, cb, a)`][`void`][Computes `cb | a` and stores the result in `b`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3564. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3565. When not provided, does the equivalent of `eval_bitwise_or(b, cb, B(a))`.][[space]]]
  3566. [[`eval_bitwise_or(b, a, cb)`][`void`][Computes `cb | a` and stores the result in `b`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3567. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3568. When not provided, does the equivalent of `eval_bitwise_or(b, cb, a)`.][[space]]]
  3569. [[`eval_bitwise_xor(b, a)`][`void`][Computes `b ^= cb`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3570. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3571. When not provided, the default version calls `eval_bitwise_xor(b, B(a))`][[space]]]
  3572. [[`eval_bitwise_xor(b, cb, cb2)`][`void`][Computes `cb ^ cb2` and stores the result in `b`, only required when `B` is an integer type.
  3573. When not provided, does the equivalent of `b = cb; eval_bitwise_xor(b, cb2)`.][[space]]]
  3574. [[`eval_bitwise_xor(b, cb, a)`][`void`][Computes `cb ^ a` and stores the result in `b`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3575. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3576. When not provided, does the equivalent of `eval_bitwise_xor(b, cb, B(a))`.][[space]]]
  3577. [[`eval_bitwise_xor(b, a, cb)`][`void`][Computes `a ^ cb` and stores the result in `b`, only required when `B` is an integer type. The type of `a` shall be listed in one of the type lists
  3578. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3579. When not provided, does the equivalent of `eval_bitwise_xor(b, cb, a)`.][[space]]]
  3580. [[`eval_left_shift(b, cb, ui)`][`void`][Computes `cb << ui` and stores the result in `b`, only required when `B` is an integer type.
  3581. When not provided, does the equivalent of `b = cb; eval_left_shift(b, a);`.][[space]]]
  3582. [[`eval_right_shift(b, cb, ui)`][`void`][Computes `cb >> ui` and stores the result in `b`, only required when `B` is an integer type.
  3583. When not provided, does the equivalent of `b = cb; eval_right_shift(b, a);`.][[space]]]
  3584. [[`eval_qr(cb, cb2, b, b2)`][`void`][Sets `b` to the result of `cb / cb2` and `b2` to the result of `cb % cb2`. Only required when `B` is an integer type.
  3585. The default version of this function is synthesised from other operations above.]
  3586. [`std::overflow_error` if `a` has the value zero.]]
  3587. [[`eval_integer_modulus(cb, ui)`][`unsigned`][Returns the result of `cb % ui`. Only required when `B` is an integer type.
  3588. The default version of this function is synthesised from other operations above.]
  3589. [`std::overflow_error` if `a` has the value zero.]]
  3590. [[`eval_lsb(cb)`][`unsigned`][Returns the index of the least significant bit that is set. Only required when `B` is an integer type.
  3591. The default version of this function is synthesised from other operations above.][[space]]]
  3592. [[`eval_msb(cb)`][`unsigned`][Returns the index of the most significant bit that is set. Only required when `B` is an integer type.
  3593. The default version of this function is synthesised from other operations above.][[space]]]
  3594. [[`eval_bit_test(cb, ui)`][`bool`][Returns true if `cb` has bit `ui` set. Only required when `B` is an integer type.
  3595. The default version of this function is synthesised from other operations above.][[space]]]
  3596. [[`eval_bit_set(b, ui)`][`void`][Sets the bit at index `ui` in `b`. Only required when `B` is an integer type.
  3597. The default version of this function is synthesised from other operations above.][[space]]]
  3598. [[`eval_bit_unset(b, ui)`][`void`][Unsets the bit at index `ui` in `b`. Only required when `B` is an integer type.
  3599. The default version of this function is synthesised from other operations above.][[space]]]
  3600. [[`eval_bit_flip(b, ui)`][`void`][Flips the bit at index `ui` in `b`. Only required when `B` is an integer type.
  3601. The default version of this function is synthesised from other operations above.][[space]]]
  3602. [[`eval_gcd(b, cb, cb2)`][`void`][Sets `b` to the greatest common divisor of `cb` and `cb2`. Only required when `B` is an integer type.
  3603. The default version of this function is synthesised from other operations above.][[space]]]
  3604. [[`eval_lcm(b, cb, cb2)`][`void`][Sets `b` to the least common multiple of `cb` and `cb2`. Only required when `B` is an integer type.
  3605. The default version of this function is synthesised from other operations above.][[space]]]
  3606. [[`eval_gcd(b, cb, a)`][`void`][Sets `b` to the greatest common divisor of `cb` and `cb2`. Only required when `B` is an integer type.
  3607. The type of `a` shall be listed in one of the type lists
  3608. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3609. The default version of this function calls `eval_gcd(b, cb, B(a))`.][[space]]]
  3610. [[`eval_lcm(b, cb, a)`][`void`][Sets `b` to the least common multiple of `cb` and `cb2`. Only required when `B` is an integer type.
  3611. The type of `a` shall be listed in one of the type lists
  3612. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3613. The default version of this function calls `eval_lcm(b, cb, B(a))`.][[space]]]
  3614. [[`eval_gcd(b, a, cb)`][`void`][Sets `b` to the greatest common divisor of `cb` and `a`. Only required when `B` is an integer type.
  3615. The type of `a` shall be listed in one of the type lists
  3616. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3617. The default version of this function calls `eval_gcd(b, cb, a)`.][[space]]]
  3618. [[`eval_lcm(b, a, cb)`][`void`][Sets `b` to the least common multiple of `cb` and `a`. Only required when `B` is an integer type.
  3619. The type of `a` shall be listed in one of the type lists
  3620. `B::signed_types`, `B::unsigned_types` or `B::float_types`.
  3621. The default version of this function calls `eval_lcm(b, cb, a)`.][[space]]]
  3622. [[`eval_powm(b, cb, cb2, cb3)`][`void`][Sets `b` to the result of ['(cb^cb2)%cb3].
  3623. The default version of this function is synthesised from other operations above.][[space]]]
  3624. [[`eval_powm(b, cb, cb2, a)`][`void`][Sets `b` to the result of ['(cb^cb2)%a].
  3625. The type of `a` shall be listed in one of the type lists
  3626. `B::signed_types`, `B::unsigned_types`.
  3627. The default version of this function is synthesised from other operations above.][[space]]]
  3628. [[`eval_powm(b, cb, a, cb2)`][`void`][Sets `b` to the result of ['(cb^a)%cb2].
  3629. The type of `a` shall be listed in one of the type lists
  3630. `B::signed_types`, `B::unsigned_types`.
  3631. The default version of this function is synthesised from other operations above.][[space]]]
  3632. [[`eval_powm(b, cb, a, a2)`][`void`][Sets `b` to the result of ['(cb^a)%a2].
  3633. The type of `a` shall be listed in one of the type lists
  3634. `B::signed_types`, `B::unsigned_types`.
  3635. The default version of this function is synthesised from other operations above.][[space]]]
  3636. [[`eval_integer_sqrt(b, cb, b2)`][`void`][Sets `b` to the largest integer which when squared is less than `cb`, also
  3637. sets `b2` to the remainder, ie to ['cb - b[super 2]].
  3638. The default version of this function is synthesised from other operations above.][[space]]]
  3639. [[['Sign manipulation:]]]
  3640. [[`eval_abs(b, cb)`][`void`][Set `b` to the absolute value of `cb`.
  3641. The default version of this functions assigns `cb` to `b`, and then calls `b.negate()` if
  3642. `eval_get_sign(cb) < 0`.][[space]]]
  3643. [[`eval_fabs(b, cb)`][`void`][Set `b` to the absolute value of `cb`.
  3644. The default version of this functions assigns `cb` to `b`, and then calls `b.negate()` if
  3645. `eval_get_sign(cb) < 0`.][[space]]]
  3646. [[['floating-point functions:]]]
  3647. [[`eval_fpclassify(cb)`][`int`][Returns one of the same values returned by `std::fpclassify`. Only required when `B` is an floating-point type.
  3648. The default version of this function will only test for zero `cb`.][[space]]]
  3649. [[`eval_trunc(b, cb)`][`void`][Performs the equivalent operation to `std::trunc` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3650. The default version of this function is synthesised from other operations above.][[space]]]
  3651. [[`eval_round(b, cb)`][`void`][Performs the equivalent operation to `std::round` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3652. The default version of this function is synthesised from other operations above.][[space]]]
  3653. [[`eval_exp(b, cb)`][`void`][Performs the equivalent operation to `std::exp` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3654. The default version of this function is synthesised from other operations above.][[space]]]
  3655. [[`eval_exp2(b, cb)`][`void`][Performs the equivalent operation to `std::exp2` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3656. The default version of this function is implemented in terms of `eval_pow`.][[space]]]
  3657. [[`eval_log(b, cb)`][`void`][Performs the equivalent operation to `std::log` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3658. The default version of this function is synthesised from other operations above.][[space]]]
  3659. [[`eval_log10(b, cb)`][`void`][Performs the equivalent operation to `std::log10` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3660. The default version of this function is synthesised from other operations above.][[space]]]
  3661. [[`eval_sin(b, cb)`][`void`][Performs the equivalent operation to `std::sin` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3662. The default version of this function is synthesised from other operations above.][[space]]]
  3663. [[`eval_cos(b, cb)`][`void`][Performs the equivalent operation to `std::cos` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3664. The default version of this function is synthesised from other operations above.][[space]]]
  3665. [[`eval_tan(b, cb)`][`void`][Performs the equivalent operation to `std::exp` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3666. The default version of this function is synthesised from other operations above.][[space]]]
  3667. [[`eval_asin(b, cb)`][`void`][Performs the equivalent operation to `std::asin` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3668. The default version of this function is synthesised from other operations above.][[space]]]
  3669. [[`eval_acos(b, cb)`][`void`][Performs the equivalent operation to `std::acos` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3670. The default version of this function is synthesised from other operations above.][[space]]]
  3671. [[`eval_atan(b, cb)`][`void`][Performs the equivalent operation to `std::atan` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3672. The default version of this function is synthesised from other operations above.][[space]]]
  3673. [[`eval_sinh(b, cb)`][`void`][Performs the equivalent operation to `std::sinh` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3674. The default version of this function is synthesised from other operations above.][[space]]]
  3675. [[`eval_cosh(b, cb)`][`void`][Performs the equivalent operation to `std::cosh` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3676. The default version of this function is synthesised from other operations above.][[space]]]
  3677. [[`eval_tanh(b, cb)`][`void`][Performs the equivalent operation to `std::tanh` on argument `cb` and stores the result in `b`. Only required when `B` is an floating-point type.
  3678. The default version of this function is synthesised from other operations above.][[space]]]
  3679. [[`eval_fmod(b, cb, cb2)`][`void`][Performs the equivalent operation to `std::fmod` on arguments `cb` and `cb2`, and store the result in `b`. Only required when `B` is an floating-point type.
  3680. The default version of this function is synthesised from other operations above.][[space]]]
  3681. [[`eval_modf(b, cb, pb)`][`void`][Performs the equivalent operation to `std::modf` on argument `cb`, and store the integer result in `*pb` and the fractional part in `b`.
  3682. Only required when `B` is an floating-point type.
  3683. The default version of this function is synthesised from other operations above.][[space]]]
  3684. [[`eval_pow(b, cb, cb2)`][`void`][Performs the equivalent operation to `std::pow` on arguments `cb` and `cb2`, and store the result in `b`. Only required when `B` is an floating-point type.
  3685. The default version of this function is synthesised from other operations above.][[space]]]
  3686. [[`eval_atan2(b, cb, cb2)`][`void`][Performs the equivalent operation to `std::atan` on arguments `cb` and `cb2`, and store the result in `b`. Only required when `B` is an floating-point type.
  3687. The default version of this function is synthesised from other operations above.][[space]]]
  3688. [[`eval_scalbn(b, cb, e)`][`void`][Scales value `cb` by ['r[super e]], where ['r] is the radix of the type. The default version of this function
  3689. is implemented in terms of eval_ldexp, consequently this function must be provided for types with a radix other than 2.]]
  3690. [[`eval_scalbln(b, cb, e)`][`void`][Calls `eval_scalbn(b, cb, e)`.]]
  3691. [[`eval_ilogb(cb)`][`B::exponent_type`][Returns the exponent ['e] of value `cb` such that ['1 <= cb*r[super -e] < r], where ['r] is the radix of type B.
  3692. The default version of this function is implemented in terms of eval_frexp, consequently this function must be provided for types with a radix other than 2.]]
  3693. [[`eval_remquo(b, cb, cb2, pi)`][`void`][Sets `b = cb - n * cb2` and stores `n` in `*pi`.]]
  3694. [[`eval_remquo(b, cb, a, pi)`][`void`][Default version converts a to type B and calls the overload above.]]
  3695. [[`eval_remquo(b, a, cb, pi)`][`void`][Default version converts a to type B and calls the overload above.]]
  3696. [[`eval_remainder(b, cb, cb2)`][`void`][Default version calls eval_remquo with a dummy final argument.]]
  3697. [[`eval_remainder(b, cb, a)`][`void`][Default version calls eval_remquo with a dummy final argument.]]
  3698. [[`eval_remainder(b, a, cb)`][`void`][Default version calls eval_remquo with a dummy final argument.]]
  3699. [[`eval_fdim(b, cb, cb2)`][`void`][Default version sets `b = cb - cb2` if `cb > cb2` and zero otherwise. Special cases are handled as in the C99 annex.]]
  3700. [[`eval_fdim(b, cb, a)`][`void`][Default version sets `b = cb - cb2` if `cb > cb2` and zero otherwise. Special cases are handled as in the C99 annex.]]
  3701. [[`eval_fdim(b, a, cb)`][`void`][Default version sets `b = cb - cb2` if `cb > cb2` and zero otherwise. Special cases are handled as in the C99 annex.]]
  3702. [[`eval_fmax(b, cb, cb2)`][`void`][Sets `b` to the larger of `cb` and `cb2`.]]
  3703. [[`eval_fmax(b, cb, a)`][`void`][Sets `b` to the larger of `cb` and `a`.]]
  3704. [[`eval_fmax(b, a, cb)`][`void`][Sets `b` to the larger of `cb` and `a`.]]
  3705. [[`eval_fmin(b, cb, cb2)`][`void`][Sets `b` to the smaller of `cb` and `cb2`.]]
  3706. [[`eval_fmin(b, cb, a)`][`void`][Sets `b` to the smaller of `cb` and `a`.]]
  3707. [[`eval_fmin(b, a, cb)`][`void`][Sets `b` to the smaller of `cb` and `a`.]]
  3708. [[`eval_hypot(b, cb, cb2)`][`void`][Sets `b` to the square root of the sum of the squares of `cb` and `cb2` without undue over or under flow.]]
  3709. [[`eval_hypot(b, cb, a)`][`void`][As above.]]
  3710. [[`eval_hypot(b, a, cb)`][`void`][As above.]]
  3711. [[`eval_logb(b, cb)`][`B::exponent_type`][Sets `b` to the exponent ['e] of value `cb` such that ['1 <= cb*r[super -b] < r], where ['r] is the radix of type B.
  3712. The default version of this function is implemented in terms of `eval_ilogb`.]]
  3713. [[`eval_nearbyint(b, cb)`][`void`][Calls `eval_round(b, cb)`.]]
  3714. [[`eval_rint(b, cb)`][`void`][Calls `eval_nearbyint(b, cb)`.]]
  3715. [[`eval_log2(b, cb)`][`void`][Sets `b` to the logarithm base 2 of `cb`.]]
  3716. [[['hashing:]]]
  3717. [[`hash_value(cb)`][`std::size_t`]
  3718. [Returns a hash value for the argument that is suitable for use with `std::hash` etc. If not provided then no automatic hashing support will be available for the number type.]]
  3719. ]
  3720. When the tables above place no ['throws] requirements on an operation, then it is up to each type modelling this concept to
  3721. decide when or whether throwing an exception is desirable. However, thrown exceptions should always either be the type, or
  3722. inherit from the type `std::runtime_error`. For example, a floating-point type might choose to throw `std::overflow_error`
  3723. whenever the result of an operation would be infinite, and `std::underflow_error` whenever it would round to zero.
  3724. [note
  3725. The non-member functions are all named with an "eval_" prefix to avoid conflicts with template classes of the same name -
  3726. in point of fact this naming convention shouldn't be necessary, but rather works around some compiler bugs.]
  3727. [h4 Overloadable Functions]
  3728. Some of the C99 math functions do not have `eval_` functions but must be overloaded directly: these functions
  3729. are either trivial or are forwarded to the Boost.Math implementations by default.
  3730. The full list of these functions is:
  3731. int sign (const ``['number-or-expression-template-type]``&);
  3732. int signbit (const ``['number-or-expression-template-type]``&);
  3733. ``['number]`` changesign (const ``['number-or-expression-template-type]``&);
  3734. ``['number]`` copysign (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  3735. ``['number]`` asinh (const ``['number-or-expression-template-type]``&);
  3736. ``['number]`` acosh (const ``['number-or-expression-template-type]``&);
  3737. ``['number]`` atanh (const ``['number-or-expression-template-type]``&);
  3738. ``['number]`` cbrt (const ``['number-or-expression-template-type]``&);
  3739. ``['number]`` erf (const ``['number-or-expression-template-type]``&);
  3740. ``['number]`` erfc (const ``['number-or-expression-template-type]``&);
  3741. ``['number]`` expm1 (const ``['number-or-expression-template-type]``&);
  3742. ``['number]`` log1p (const ``['number-or-expression-template-type]``&);
  3743. ``['number]`` tgamma (const ``['number-or-expression-template-type]``&);
  3744. ``['number]`` lgamma (const ``['number-or-expression-template-type]``&);
  3745. long lrint (const ``['number-or-expression-template-type]``&);
  3746. long long llrint (const ``['number-or-expression-template-type]``&);
  3747. ``['number]`` nextafter (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  3748. ``['number]`` nexttoward (const ``['number-or-expression-template-type]``&, const ``['number-or-expression-template-type]``&);
  3749. [endsect]
  3750. [section:headers Header File Structure]
  3751. [table Top level headers
  3752. [[Header][Contains]]
  3753. [[cpp_int.hpp][The `cpp_int` backend type.]]
  3754. [[gmp.hpp][Defines all [gmp] related backends.]]
  3755. [[miller_rabin.hpp][Miller Rabin primality testing code.]]
  3756. [[number.hpp][Defines the `number` backend, is included by all the backend headers.]]
  3757. [[mpfr.hpp][Defines the mpfr_float_backend backend.]]
  3758. [[random.hpp][Defines code to interoperate with Boost.Random.]]
  3759. [[rational_adaptor.hpp][Defines the `rational_adaptor` backend.]]
  3760. [[cpp_dec_float.hpp][Defines the `cpp_dec_float` backend.]]
  3761. [[tommath.hpp][Defines the `tommath_int` backend.]]
  3762. [[concepts/number_archetypes.hpp][Defines a backend concept archetypes for testing use.]]
  3763. ]
  3764. [table Implementation Headers
  3765. [[Header][Contains]]
  3766. [[cpp_int/add.hpp][Add and subtract operators for `cpp_int_backend`.]]
  3767. [[cpp_int/bitwise.hpp][Bitwise operators for `cpp_int_backend`.]]
  3768. [[cpp_int/checked.hpp][Helper functions for checked arithmetic for `cpp_int_backend`.]]
  3769. [[cpp_int/comparison.hpp][Comparison operators for `cpp_int_backend`.]]
  3770. [[cpp_int/cpp_int_config.hpp][Basic setup and configuration for `cpp_int_backend`.]]
  3771. [[cpp_int/divide.hpp][Division and modulus operators for `cpp_int_backend`.]]
  3772. [[cpp_int/limits.hpp][`numeric_limits` support for `cpp_int_backend`.]]
  3773. [[cpp_int/misc.hpp][Miscellaneous operators for `cpp_int_backend`.]]
  3774. [[cpp_int/multiply.hpp][Multiply operators for `cpp_int_backend`.]]
  3775. [[detail/big_lanczos.hpp][Lanczos support for Boost.Math integration.]]
  3776. [[detail/default_ops.hpp][Default versions of the optional backend non-member functions.]]
  3777. [[detail/generic_interconvert.hpp][Generic interconversion routines.]]
  3778. [[detail/number_base.hpp][All the expression template code, metaprogramming, and operator overloads for `number`.]]
  3779. [[detail/no_et_ops.hpp][The non-expression template operators.]]
  3780. [[detail/functions/constants.hpp][Defines constants used by the floating-point functions.]]
  3781. [[detail/functions/pow.hpp][Defines default versions of the power and exponential related floating-point functions.]]
  3782. [[detail/functions/trig.hpp][Defines default versions of the trigonometric related floating-point functions.]]
  3783. ]
  3784. [endsect]
  3785. [endsect]
  3786. [section:perf Performance Comparison]
  3787. [section:overhead The Overhead in the Number Class Wrapper]
  3788. Using a simple [@../../performance/arithmetic_backend.hpp backend class] that wraps any built in arithmetic type
  3789. we can measure the overhead involved in wrapping a type inside the `number` frontend, and the effect that turning
  3790. on expression templates has. The following table compares the performance between `double` and a `double` wrapped
  3791. inside class `number`:
  3792. [table
  3793. [[Type][Bessel Function Evaluation][Non-Central T Evaluation]]
  3794. [[`double`] [[*1.0 (0.016s)]] [[*1.0] (0.46s)]]
  3795. [[`number<arithmetic_backend<double>, et_off>`] [1.2 (0.019s)] [[*1.0](0.46s)]]
  3796. [[`number<arithmetic_backend<double>, et_on>`] [1.2 (0.019s)] [1.7 (0.79s)]]
  3797. ]
  3798. As you can see whether or not there is an overhead, and how large it is depends on the actual situation,
  3799. but the overhead is in any cases small. Expression templates generally add a greater overhead the
  3800. more complex the expression becomes due to the logic of figuring out how to best unpack and evaluate
  3801. the expression, but of course this is also the situation where you save more temporaries. For a
  3802. "trivial" backend like this, saving temporaries has no benefit, but for larger types it becomes
  3803. a bigger win.
  3804. The following table compares arithmetic using either `long long` or `number<arithmetic_backend<long long> >`
  3805. for the [@../../performance/voronoi_performance.cpp voronoi-diagram builder test]:
  3806. [table
  3807. [[Type][Relative time]]
  3808. [[`long long`][[*1.0](0.0823s)]]
  3809. [[`number<arithmetic_backend<long long>, et_off>`][1.05 (0.0875s)]]
  3810. ]
  3811. This test involves mainly creating a lot of temporaries and performing a small amount of arithmetic on them,
  3812. with very little difference in performance between the native and "wrapped" types.
  3813. The test code was compiled with Microsoft Visual Studio 2010 with all optimisations
  3814. turned on (/Ox), and used MPIR-2.3.0 and [tommath]-0.42.0. The tests were run on 32-bit
  3815. Windows Vista machine.
  3816. [endsect]
  3817. [section:realworld Floating-Point Real World Tests]
  3818. These tests test the total time taken to execute all of Boost.Math's test cases for these functions.
  3819. In each case the best performing library gets a relative score of 1, with the total execution time
  3820. given in brackets. The first three libraries listed are the various floating-point types provided
  3821. by this library, while for comparison, two popular C++ front-ends to [mpfr] ([mpfr_class] and [mpreal])
  3822. are also shown.
  3823. [table Bessel Function Performance
  3824. [[Library][50 Decimal Digits][100 Decimal Digits]]
  3825. [[mpfr_float] [1.2 (5.78s)] [1.2 (9.56s)]]
  3826. [[static_mpfr_float] [1.1 (5.47s)] [1.1 (9.09s)]]
  3827. [[mpf_float] [[*1.0] (4.82s)] [[*1.0](8.07s)]]
  3828. [[cpp_dec_float] [1.8 (8.54s)] [2.6 (20.66s)]]
  3829. [[[mpfr_class]] [1.3 (6.28s)] [1.2(10.06s)]]
  3830. [[[mpreal]] [2.0 (9.54s)] [1.7 (14.08s)]]
  3831. ]
  3832. [table Non-Central T Distribution Performance
  3833. [[Library][50 Decimal Digits]]
  3834. [[mpfr_float] [1.3 (263.27s)]]
  3835. [[static_mpfr_float] [1.2 (232.88s)]]
  3836. [[mpf_float] [[*1.0] (195.73s)]]
  3837. [[cpp_dec_float] [1.9 (366.38s)]]
  3838. [[[mpfr_class]] [1.5 (286.94s)]]
  3839. [[[mpreal]] [2.0 (388.70s)]]
  3840. ]
  3841. Test code was compiled with Microsoft Visual Studio 2010 with all optimisations
  3842. turned on (/Ox), and used MPIR-2.3.0 and [mpfr]-3.0.0. The tests were run on 32-bit
  3843. Windows Vista machine.
  3844. [endsect]
  3845. [section:int_real_world Integer Real World Tests]
  3846. The first set of [@../../performance/voronoi_performance.cpp tests] measure the times taken to
  3847. execute the multiprecision part of the Voronoi-diagram builder from Boost.Polygon. The tests
  3848. mainly create a large number of temporaries "just in case" multiprecision arithmetic is required,
  3849. for comparison, also included in the tests is Boost.Polygon's own partial-multiprecision integer
  3850. type which was custom written for this specific task:
  3851. [table
  3852. [[Integer Type][Relative Performance (Actual time in parenthesis)]]
  3853. [[polygon::detail::extended_int][1(0.138831s)]]
  3854. [[int256_t][1.19247(0.165551s)]]
  3855. [[int512_t][1.23301(0.17118s)]]
  3856. [[int1024_t][1.21463(0.168628s)]]
  3857. [[checked_int256_t][1.31711(0.182855s)]]
  3858. [[checked_int512_t][1.57413(0.218538s)]]
  3859. [[checked_int1024_t][1.36992(0.190187s)]]
  3860. [[cpp_int][1.63244(0.226632s)]]
  3861. [[mpz_int][5.42511(0.753172s)]]
  3862. [[tom_int][29.0793(4.03709s)]]
  3863. ]
  3864. Note how for this use case, any dynamic allocation is a performance killer.
  3865. The next [@../../performance/miller_rabin_performance.cpp tests] measure the time taken to generate 1000 128-bit
  3866. random numbers and test for primality using the Miller Rabin test. This is primarily a test of modular-exponentiation
  3867. since that is the rate limiting step:
  3868. [table
  3869. [[Integer Type][Relative Performance (Actual time in parenthesis)]]
  3870. [[cpp_int][5.25827(0.379597s)]]
  3871. [[cpp_int (no Expression templates)][5.15675(0.372268s)]]
  3872. [[cpp_int (128-bit cache)][5.10882(0.368808s)]]
  3873. [[cpp_int (256-bit cache)][5.50623(0.397497s)]]
  3874. [[cpp_int (512-bit cache)][4.82257(0.348144s)]]
  3875. [[cpp_int (1024-bit cache)][5.00053(0.360991s)]]
  3876. [[int1024_t][4.37589(0.315897s)]]
  3877. [[checked_int1024_t][4.52396(0.326587s)]]
  3878. [[mpz_int][1(0.0721905s)]]
  3879. [[mpz_int (no Expression templates)][1.0248(0.0739806s)]]
  3880. [[tom_int][2.60673(0.188181s)]]
  3881. [[tom_int (no Expression templates)][2.64997(0.191303s)]]
  3882. ]
  3883. It's interesting to note that expression templates have little effect here - perhaps because the actual expressions involved
  3884. are relatively trivial in this case - so the time taken for multiplication and division tends to dominate. Also note
  3885. how increasing the internal cache size used by `cpp_int` is quite effective in this case in cutting out memory allocations
  3886. altogether - cutting about a third off the total runtime. Finally the much quicker times from GMP and tommath are down to their
  3887. much better modular-exponentiation algorithms (GMP's is about 5x faster). That's an issue which needs to be addressed
  3888. in a future release for __cpp_int.
  3889. Test code was compiled with Microsoft Visual Studio 2010 with all optimisations
  3890. turned on (/Ox), and used MPIR-2.3.0 and [mpfr]-3.0.0. The tests were run on 32-bit
  3891. Windows Vista machine.
  3892. [endsect]
  3893. [section:float_performance Float Algorithm Performance]
  3894. Note that these tests are carefully designed to test performance of the underlying algorithms
  3895. and not memory allocation or variable copying. As usual, performance results should be taken
  3896. with a healthy dose of scepticism, and real-world performance may vary widely depending upon the
  3897. specifics of the program. In each table relative times are given first, with the best performer
  3898. given a score of 1. Total actual times are given in brackets, measured in seconds for 500000
  3899. operations.
  3900. [table Operator +
  3901. [[Backend][50 Bits][100 Bits][500 Bits]]
  3902. [[cpp_dec_float][[*1] (0.0575156s)][[*1] (0.0740086s)][[*1] (0.219073s)]]
  3903. [[gmp_float][2.45065 (0.14095s)][2.01398 (0.149052s)][1.09608 (0.240122s)]]
  3904. [[mpfr_float][2.6001 (0.149546s)][2.12079 (0.156957s)][1.09078 (0.23896s)]]
  3905. ]
  3906. [table Operator +(int)
  3907. [[Backend][50 Bits][100 Bits][500 Bits]]
  3908. [[cpp_dec_float][1.46115 (0.0855392s)][2.60353 (0.114398s)][3.62562 (0.264905s)]]
  3909. [[gmp_float][[*1] (0.0585424s)][[*1] (0.0439398s)][[*1] (0.0730648s)]]
  3910. [[mpfr_float][2.40441 (0.14076s)][3.2877 (0.144461s)][2.40379 (0.175632s)]]
  3911. ]
  3912. [table Operator +(unsigned long long)
  3913. [[Backend][50 Bits][100 Bits][500 Bits]]
  3914. [[cpp_dec_float][[*1] (0.118146s)][[*1] (0.144714s)][[*1] (0.315639s)]]
  3915. [[gmp_float][4.5555 (0.538213s)][3.83096 (0.554395s)][1.95079 (0.615745s)]]
  3916. [[mpfr_float][5.74477 (0.678719s)][4.85295 (0.702291s)][2.70354 (0.853342s)]]
  3917. ]
  3918. [table Operator +=(unsigned long long)
  3919. [[Backend][50 Bits][100 Bits][500 Bits]]
  3920. [[cpp_dec_float][[*1] (0.101188s)][[*1] (0.122394s)][[*1] (0.251975s)]]
  3921. [[gmp_float][5.199 (0.526079s)][4.39327 (0.537712s)][2.42151 (0.610159s)]]
  3922. [[mpfr_float][6.08318 (0.615547s)][5.18525 (0.634645s)][3.1022 (0.781677s)]]
  3923. ]
  3924. [table Operator -
  3925. [[Backend][50 Bits][100 Bits][500 Bits]]
  3926. [[cpp_dec_float][[*1] (0.0895163s)][[*1] (0.129248s)][1.5088 (0.374512s)]]
  3927. [[gmp_float][1.72566 (0.154474s)][1.22567 (0.158415s)][[*1] (0.248219s)]]
  3928. [[mpfr_float][1.83764 (0.164499s)][1.34284 (0.173559s)][1.00226 (0.248781s)]]
  3929. ]
  3930. [table Operator -(int)
  3931. [[Backend][50 Bits][100 Bits][500 Bits]]
  3932. [[cpp_dec_float][[*1] (0.105285s)][[*1] (0.142741s)][[*1] (0.278718s)]]
  3933. [[gmp_float][2.34437 (0.246828s)][1.28814 (0.183871s)][1.00731 (0.280754s)]]
  3934. [[mpfr_float][2.8032 (0.295136s)][2.09178 (0.298582s)][1.25213 (0.34899s)]]
  3935. ]
  3936. [table Operator -(unsigned long long)
  3937. [[Backend][50 Bits][100 Bits][500 Bits]]
  3938. [[cpp_dec_float][[*1] (0.13719s)][[*1] (0.184428s)][[*1] (0.344212s)]]
  3939. [[gmp_float][4.0804 (0.559791s)][3.06776 (0.565781s)][2.07736 (0.715053s)]]
  3940. [[mpfr_float][5.10114 (0.699828s)][3.88684 (0.716843s)][2.50074 (0.860784s)]]
  3941. ]
  3942. [table Operator -=(unsigned long long)
  3943. [[Backend][50 Bits][100 Bits][500 Bits]]
  3944. [[cpp_dec_float][[*1] (0.100984s)][[*1] (0.123148s)][[*1] (0.246181s)]]
  3945. [[gmp_float][5.68353 (0.573944s)][4.68636 (0.577116s)][2.6958 (0.663655s)]]
  3946. [[mpfr_float][6.19738 (0.625834s)][5.18544 (0.638577s)][3.18738 (0.784673s)]]
  3947. ]
  3948. [table Operator *
  3949. [[Backend][50 Bits][100 Bits][500 Bits]]
  3950. [[cpp_dec_float][1.03667 (0.284251s)][1.30576 (0.536527s)][1.44686 (4.81057s)]]
  3951. [[gmp_float][[*1] (0.274196s)][[*1] (0.410891s)][[*1] (3.32484s)]]
  3952. [[mpfr_float][1.24537 (0.341477s)][1.15785 (0.475749s)][1.1796 (3.92199s)]]
  3953. ]
  3954. [table Operator *(int)
  3955. [[Backend][50 Bits][100 Bits][500 Bits]]
  3956. [[cpp_dec_float][3.97453 (0.240262s)][9.91222 (0.463473s)][50.7926 (4.36527s)]]
  3957. [[gmp_float][[*1] (0.0604505s)][[*1] (0.0467577s)][[*1] (0.0859431s)]]
  3958. [[mpfr_float][2.56974 (0.155342s)][3.56312 (0.166603s)][3.22964 (0.277565s)]]
  3959. ]
  3960. [table Operator *(unsigned long long)
  3961. [[Backend][50 Bits][100 Bits][500 Bits]]
  3962. [[cpp_dec_float][[*1] (0.331877s)][1.01058 (0.586122s)][6.688 (4.7931s)]]
  3963. [[gmp_float][1.72433 (0.572266s)][[*1] (0.579987s)][[*1] (0.716672s)]]
  3964. [[mpfr_float][2.5553 (0.848047s)][1.74987 (1.0149s)][1.80403 (1.2929s)]]
  3965. ]
  3966. [table Operator *=(unsigned long long)
  3967. [[Backend][50 Bits][100 Bits][500 Bits]]
  3968. [[cpp_dec_float][[*1] (0.321397s)][1.00772 (0.574887s)][6.65946 (4.7468s)]]
  3969. [[gmp_float][1.77419 (0.570218s)][[*1] (0.570482s)][[*1] (0.712791s)]]
  3970. [[mpfr_float][2.62172 (0.842611s)][1.77691 (1.01369s)][1.77511 (1.26528s)]]
  3971. ]
  3972. [table Operator /
  3973. [[Backend][50 Bits][100 Bits][500 Bits]]
  3974. [[cpp_dec_float][2.96096 (4.00777s)][4.53244 (7.86435s)][6.11936 (51.5509s)]]
  3975. [[gmp_float][[*1] (1.35354s)][[*1] (1.73512s)][[*1] (8.42422s)]]
  3976. [[mpfr_float][1.30002 (1.75963s)][1.39045 (2.41261s)][1.66762 (14.0484s)]]
  3977. ]
  3978. [table Operator /(int)
  3979. [[Backend][50 Bits][100 Bits][500 Bits]]
  3980. [[cpp_dec_float][8.60726 (1.8181s)][15.4122 (3.67479s)][34.5119 (24.729s)]]
  3981. [[gmp_float][1.24394 (0.262756s)][[*1] (0.238433s)][[*1] (0.716536s)]]
  3982. [[mpfr_float][[*1] (0.211229s)][1.12178 (0.26747s)][1.02237 (0.732562s)]]
  3983. ]
  3984. [table Operator /(unsigned long long)
  3985. [[Backend][50 Bits][100 Bits][500 Bits]]
  3986. [[cpp_dec_float][2.10976 (1.97569s)][3.73601 (3.9133s)][11.3085 (25.4533s)]]
  3987. [[gmp_float][[*1] (0.936452s)][[*1] (1.04746s)][[*1] (2.25081s)]]
  3988. [[mpfr_float][1.3423 (1.257s)][1.51575 (1.58768s)][3.31513 (7.46175s)]]
  3989. ]
  3990. [table Operator /=(unsigned long long)
  3991. [[Backend][50 Bits][100 Bits][500 Bits]]
  3992. [[cpp_dec_float][2.17401 (1.96883s)][3.79591 (3.8965s)][11.2328 (25.2606s)]]
  3993. [[gmp_float][[*1] (0.905621s)][[*1] (1.0265s)][[*1] (2.24882s)]]
  3994. [[mpfr_float][1.37953 (1.24933s)][1.53073 (1.57129s)][3.30546 (7.43339s)]]
  3995. ]
  3996. [table Operator construct
  3997. [[Backend][50 Bits][100 Bits][500 Bits]]
  3998. [[cpp_dec_float][[*1] (0.00929804s)][[*1] (0.0268321s)][[*1] (0.0310685s)]]
  3999. [[gmp_float][30.8781 (0.287106s)][7.59969 (0.203916s)][6.51873 (0.202527s)]]
  4000. [[mpfr_float][23.5296 (0.218779s)][8.11058 (0.217624s)][7.16325 (0.222552s)]]
  4001. ]
  4002. [table Operator construct(unsigned)
  4003. [[Backend][50 Bits][100 Bits][500 Bits]]
  4004. [[cpp_dec_float][[*1] (0.0603971s)][[*1] (0.0735485s)][[*1] (0.116464s)]]
  4005. [[gmp_float][3.91573 (0.236498s)][2.88171 (0.211945s)][1.81075 (0.210887s)]]
  4006. [[mpfr_float][4.90052 (0.295977s)][4.01118 (0.295017s)][2.62005 (0.305141s)]]
  4007. ]
  4008. [table Operator construct(unsigned long long)
  4009. [[Backend][50 Bits][100 Bits][500 Bits]]
  4010. [[cpp_dec_float][[*1] (0.0610288s)][[*1] (0.0759005s)][[*1] (0.118511s)]]
  4011. [[gmp_float][8.26247 (0.504249s)][6.69042 (0.507806s)][4.32819 (0.51294s)]]
  4012. [[mpfr_float][10.1593 (0.620013s)][8.45884 (0.64203s)][5.51472 (0.653557s)]]
  4013. ]
  4014. [table Operator str
  4015. [[Backend][50 Bits][100 Bits][500 Bits]]
  4016. [[cpp_dec_float][2.95848 (0.0223061s)][3.33461 (0.033471s)][3.0159 (0.132732s)]]
  4017. [[gmp_float][[*1] (0.00753971s)][[*1] (0.0100374s)][[*1] (0.0440106s)]]
  4018. [[mpfr_float][1.25424 (0.00945658s)][1.24943 (0.012541s)][1.09428 (0.0481601s)]]
  4019. ]
  4020. Test code was compiled with Microsoft Visual Studio 2010 with all optimisations
  4021. turned on (/Ox), and used MPIR-2.3.0 and [mpfr]-3.0.0. The tests were run on 32-bit
  4022. Windows Vista machine.
  4023. [endsect]
  4024. [section:integer_performance Integer Algorithm Performance]
  4025. Note that these tests are carefully designed to test performance of the underlying algorithms
  4026. and not memory allocation or variable copying. As usual, performance results should be taken
  4027. with a healthy dose of scepticism, and real-world performance may vary widely depending upon the
  4028. specifics of the program. In each table relative times are given first, with the best performer
  4029. given a score of 1. Total actual times are given in brackets, measured in seconds for 500000
  4030. operations.
  4031. [table Operator +
  4032. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4033. [[cpp_int][1.23704 (0.0274266s)][1.09358 (0.0383278s)][1.26645 (0.0558828s)][1.32188 (0.0916899s)]]
  4034. [[cpp_int(fixed)][1.62044 (0.0359271s)][1.5277 (0.053543s)][1.73059 (0.076363s)][1.71537 (0.118983s)]]
  4035. [[gmp_int][1.87515 (0.0415741s)][1.21699 (0.042653s)][1.15599 (0.0510088s)][[*1] (0.0693631s)]]
  4036. [[tommath_int][[*1] (0.0221711s)][[*1] (0.035048s)][[*1] (0.0441255s)][1.04441 (0.0724435s)]]
  4037. ]
  4038. [table Operator +(int)
  4039. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4040. [[cpp_int][[*1] (0.0155377s)][[*1] (0.0209523s)][[*1] (0.0306377s)][[*1] (0.043125s)]]
  4041. [[cpp_int(fixed)][1.31904 (0.0204948s)][1.76211 (0.0369203s)][1.52941 (0.0468577s)][1.60412 (0.0691778s)]]
  4042. [[gmp_int][1.96204 (0.0304855s)][2.02569 (0.0424428s)][2.11505 (0.0648002s)][2.65993 (0.114709s)]]
  4043. [[tommath_int][14.0654 (0.218543s)][10.8239 (0.226786s)][7.76691 (0.23796s)][6.10039 (0.263079s)]]
  4044. ]
  4045. [table Operator +(unsigned long long)
  4046. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4047. [[cpp_int][[*1] (0.026624s)][[*1] (0.0291407s)][[*1] (0.0373209s)][[*1] (0.0464919s)]]
  4048. [[cpp_int(fixed)][1.31378 (0.034978s)][1.54897 (0.045138s)][1.53649 (0.0573431s)][1.27833 (0.0594319s)]]
  4049. [[gmp_int][25.5775 (0.680974s)][24.0117 (0.699717s)][19.5633 (0.730121s)][16.8939 (0.785432s)]]
  4050. [[tommath_int][19.4694 (0.518354s)][18.4246 (0.536907s)][14.7715 (0.551288s)][12.3637 (0.574812s)]]
  4051. ]
  4052. [table Operator +=(unsigned long long)
  4053. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4054. [[cpp_int][1.18405 (0.0196905s)][1.22304 (0.0206476s)][1.25861 (0.0217397s)][1.29525 (0.0220829s)]]
  4055. [[cpp_int(fixed)][[*1] (0.0166298s)][[*1] (0.0168822s)][[*1] (0.0172728s)][[*1] (0.0170492s)]]
  4056. [[gmp_int][39.9082 (0.663668s)][39.4584 (0.666147s)][38.5504 (0.665873s)][39.2231 (0.668722s)]]
  4057. [[tommath_int][30.6219 (0.509238s)][30.4135 (0.513447s)][30.9077 (0.533863s)][32.3086 (0.550835s)]]
  4058. ]
  4059. [table Operator -
  4060. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4061. [[cpp_int][1.06986 (0.0296064s)][[*1] (0.0381508s)][1.05932 (0.053186s)][1.1766 (0.0844721s)]]
  4062. [[cpp_int(fixed)][1.3304 (0.0368163s)][1.44506 (0.0551303s)][1.4431 (0.0724545s)][1.57255 (0.112898s)]]
  4063. [[gmp_int][1.48072 (0.0409761s)][1.19003 (0.0454007s)][1.0794 (0.0541942s)][[*1] (0.0717934s)]]
  4064. [[tommath_int][[*1] (0.0276731s)][1.10891 (0.0423057s)][[*1] (0.0502076s)][1.08479 (0.0778811s)]]
  4065. ]
  4066. [table Operator -(int)
  4067. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4068. [[cpp_int][[*1] (0.0147372s)][[*1] (0.0170001s)][[*1] (0.0232882s)][[*1] (0.0310734s)]]
  4069. [[cpp_int(fixed)][1.4267 (0.0210256s)][1.98887 (0.0338109s)][1.83788 (0.0428009s)][1.81269 (0.0563264s)]]
  4070. [[gmp_int][2.07504 (0.0305803s)][2.40928 (0.0409579s)][2.58711 (0.0602493s)][3.26438 (0.101435s)]]
  4071. [[tommath_int][13.5424 (0.199577s)][12.1793 (0.207048s)][9.28855 (0.216314s)][7.49327 (0.232842s)]]
  4072. ]
  4073. [table Operator -(unsigned long long)
  4074. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4075. [[cpp_int][[*1] (0.0277377s)][[*1] (0.0296807s)][[*1] (0.0372392s)][[*1] (0.0455855s)]]
  4076. [[cpp_int(fixed)][1.19867 (0.0332484s)][1.48639 (0.0441169s)][1.43253 (0.0533464s)][1.27697 (0.0582111s)]]
  4077. [[gmp_int][24.1794 (0.670683s)][22.9073 (0.679904s)][18.8758 (0.702922s)][16.5837 (0.755975s)]]
  4078. [[tommath_int][18.149 (0.503413s)][17.4116 (0.516787s)][14.0411 (0.52288s)][11.8237 (0.538987s)]]
  4079. ]
  4080. [table Operator -=(unsigned long long)
  4081. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4082. [[cpp_int][1.26896 (0.0203467s)][1.25722 (0.0206147s)][1.36108 (0.0225485s)][1.18351 (0.0226161s)]]
  4083. [[cpp_int(fixed)][[*1] (0.0160342s)][[*1] (0.0163971s)][[*1] (0.0165667s)][[*1] (0.0191094s)]]
  4084. [[gmp_int][41.1339 (0.659547s)][40.3982 (0.662411s)][39.925 (0.661425s)][34.636 (0.661874s)]]
  4085. [[tommath_int][31.1543 (0.499533s)][31.0303 (0.508806s)][30.7699 (0.509756s)][27.7054 (0.529434s)]]
  4086. ]
  4087. [table Operator *
  4088. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4089. [[cpp_int][1.11839 (0.0757577s)][1.61061 (0.207951s)][1.4501 (0.696912s)][1.72796 (2.64108s)]]
  4090. [[cpp_int(fixed)][1.01115 (0.0684934s)][1.28687 (0.166152s)][[*1] (0.480595s)][[*1] (1.52844s)]]
  4091. [[gmp_int][[*1] (0.0677384s)][[*1] (0.129113s)][1.09011 (0.523902s)][1.03374 (1.58s)]]
  4092. [[tommath_int][1.6322 (0.110562s)][2.71751 (0.350866s)][2.05222 (0.986288s)][2.0644 (3.15531s)]]
  4093. ]
  4094. [table Operator *(int)
  4095. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4096. [[cpp_int][1.01611 (0.0229536s)][1.12175 (0.0298152s)][1.16413 (0.0416439s)][1.31747 (0.0666043s)]]
  4097. [[cpp_int(fixed)][1.30215 (0.0294152s)][1.669 (0.0443606s)][1.72395 (0.0616701s)][1.88315 (0.095202s)]]
  4098. [[gmp_int][[*1] (0.0225897s)][[*1] (0.0265791s)][[*1] (0.0357725s)][[*1] (0.0505547s)]]
  4099. [[tommath_int][10.8281 (0.244603s)][10.1516 (0.26982s)][8.76424 (0.313519s)][8.04364 (0.406644s)]]
  4100. ]
  4101. [table Operator *(unsigned long long)
  4102. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4103. [[cpp_int][[*1] (0.0570721s)][[*1] (0.0856141s)][[*1] (0.143279s)][[*1] (0.252785s)]]
  4104. [[cpp_int(fixed)][1.10857 (0.0632686s)][1.2951 (0.110878s)][1.20827 (0.173121s)][1.18463 (0.299456s)]]
  4105. [[gmp_int][12.0605 (0.68832s)][8.13434 (0.696415s)][5.21762 (0.747577s)][3.11601 (0.787681s)]]
  4106. [[tommath_int][10.0524 (0.57371s)][7.33116 (0.627651s)][4.85202 (0.695193s)][3.35808 (0.848871s)]]
  4107. ]
  4108. [table Operator *=(unsigned long long)
  4109. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4110. [[cpp_int][111.27 (7.43118s)][67.7078 (7.34138s)][43.3851 (7.4075s)][25.3089 (7.55455s)]]
  4111. [[cpp_int(fixed)][[*1] (0.0667848s)][[*1] (0.108427s)][[*1] (0.170738s)][[*1] (0.298493s)]]
  4112. [[gmp_int][46.3718 (3.09693s)][28.4639 (3.08626s)][18.1719 (3.10264s)][10.5223 (3.14083s)]]
  4113. [[tommath_int][276.674 (18.4776s)][169.146 (18.34s)][108.491 (18.5236s)][63.3261 (18.9024s)]]
  4114. ]
  4115. [table Operator /
  4116. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4117. [[cpp_int][2.68035 (0.595251s)][2.04702 (0.707471s)][1.62314 (0.921536s)][1.43112 (1.38811s)]]
  4118. [[cpp_int(fixed)][[*1] (0.222079s)][[*1] (0.34561s)][[*1] (0.567748s)][[*1] (0.969945s)]]
  4119. [[gmp_int][3.79283 (0.842308s)][2.73668 (0.945824s)][1.86649 (1.05969s)][1.32141 (1.2817s)]]
  4120. [[tommath_int][13.2531 (2.94324s)][11.2054 (3.87271s)][9.83293 (5.58262s)][13.0164 (12.6252s)]]
  4121. ]
  4122. [table Operator /(int)
  4123. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4124. [[cpp_int][4.06026 (0.225473s)][3.45732 (0.340049s)][3.00195 (0.547957s)][2.80587 (0.978029s)]]
  4125. [[cpp_int(fixed)][2.43766 (0.135367s)][2.56264 (0.252052s)][2.44011 (0.445402s)][2.38009 (0.829617s)]]
  4126. [[gmp_int][[*1] (0.0555316s)][[*1] (0.0983563s)][[*1] (0.182534s)][[*1] (0.348566s)]]
  4127. [[tommath_int][35.9988 (1.99907s)][27.1024 (2.66569s)][21.8333 (3.98531s)][25.8066 (8.99528s)]]
  4128. ]
  4129. [table Operator /(unsigned long long)
  4130. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4131. [[cpp_int][1.50505 (0.705756s)][1.39347 (1.58556s)][2.63348 (3.57438s)][4.75451 (8.52733s)]]
  4132. [[cpp_int(fixed)][[*1] (0.468925s)][1.12378 (1.27869s)][2.29966 (3.12128s)][4.4844 (8.04288s)]]
  4133. [[gmp_int][2.17234 (1.01866s)][[*1] (1.13785s)][[*1] (1.35728s)][[*1] (1.79352s)]]
  4134. [[tommath_int][4.74612 (2.22557s)][2.70088 (3.07319s)][3.65634 (4.96268s)][6.79408 (12.1853s)]]
  4135. ]
  4136. [table Operator /=(unsigned long long)
  4137. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4138. [[cpp_int][1.76281 (0.0574966s)][1.76471 (0.0604224s)][1.56085 (0.0716403s)][1.31422 (0.124043s)]]
  4139. [[cpp_int(fixed)][[*1] (0.0326164s)][[*1] (0.0342393s)][[*1] (0.0458981s)][[*1] (0.0943852s)]]
  4140. [[gmp_int][20.2862 (0.661664s)][19.4043 (0.664389s)][14.4881 (0.664976s)][7.14238 (0.674135s)]]
  4141. [[tommath_int][32.9555 (1.07489s)][30.1525 (1.0324s)][22.8324 (1.04796s)][11.7456 (1.10861s)]]
  4142. ]
  4143. [table Operator %
  4144. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4145. [[cpp_int][1.8501 (0.364131s)][1.46527 (0.476653s)][1.27509 (0.689738s)][1.20064 (1.11769s)]]
  4146. [[cpp_int(fixed)][[*1] (0.196817s)][[*1] (0.325301s)][[*1] (0.540932s)][[*1] (0.930916s)]]
  4147. [[gmp_int][3.2533 (0.640305s)][2.15441 (0.700832s)][1.47898 (0.800029s)][1.07439 (1.00016s)]]
  4148. [[tommath_int][15.3501 (3.02116s)][12.1106 (3.9396s)][11.0689 (5.98752s)][13.5535 (12.6172s)]]
  4149. ]
  4150. [table Operator %(int)
  4151. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4152. [[cpp_int][1.82761 (0.104331s)][2.01496 (0.202512s)][2.10004 (0.389523s)][2.17252 (0.768097s)]]
  4153. [[cpp_int(fixed)][1.78851 (0.102099s)][1.96844 (0.197838s)][2.02956 (0.376451s)][2.07257 (0.73276s)]]
  4154. [[gmp_int][[*1] (0.057086s)][[*1] (0.100505s)][[*1] (0.185483s)][[*1] (0.353552s)]]
  4155. [[tommath_int][36.3018 (2.07233s)][26.3075 (2.64402s)][21.9525 (4.07183s)][25.6759 (9.07775s)]]
  4156. ]
  4157. [table Operator construct
  4158. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4159. [[cpp_int][1.40211 (0.0026854s)][[*1] (0.00278639s)][[*1] (0.00322813s)][[*1] (0.0027185s)]]
  4160. [[cpp_int(fixed)][[*1] (0.00191526s)][1.40721 (0.00392103s)][1.90346 (0.00614463s)][2.14621 (0.00583447s)]]
  4161. [[gmp_int][98.705 (0.189046s)][68.9726 (0.192184s)][58.8994 (0.190135s)][70.0525 (0.190438s)]]
  4162. [[tommath_int][105.602 (0.202255s)][74.1994 (0.206748s)][63.6455 (0.205456s)][76.8935 (0.209035s)]]
  4163. ]
  4164. [table Operator construct(unsigned)
  4165. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4166. [[cpp_int][1.73436 (0.00348927s)][[*1] (0.00263476s)][[*1] (0.0027009s)][[*1] (0.00318651s)]]
  4167. [[cpp_int(fixed)][[*1] (0.00201185s)][1.36851 (0.0036057s)][2.07362 (0.00560064s)][1.66856 (0.00531688s)]]
  4168. [[gmp_int][97.2414 (0.195635s)][76.3759 (0.201232s)][72.7396 (0.196462s)][63.8129 (0.20334s)]]
  4169. [[tommath_int][210.112 (0.422713s)][162.652 (0.42855s)][158.33 (0.427634s)][134.626 (0.428987s)]]
  4170. ]
  4171. [table Operator construct(unsigned long long)
  4172. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4173. [[cpp_int][2.34403 (0.00739542s)][1.66376 (0.00713834s)][1.22989 (0.0074969s)][1.23708 (0.00711417s)]]
  4174. [[cpp_int(fixed)][[*1] (0.00315501s)][[*1] (0.00429049s)][[*1] (0.00609561s)][[*1] (0.0057508s)]]
  4175. [[gmp_int][222.866 (0.703144s)][164.331 (0.705059s)][115.363 (0.70321s)][122.347 (0.703596s)]]
  4176. [[tommath_int][218.681 (0.689941s)][163.796 (0.702765s)][114.57 (0.698376s)][122.422 (0.704027s)]]
  4177. ]
  4178. [table Operator gcd
  4179. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4180. [[cpp_int][1.16358 (2.74442s)][1.39847 (8.11559s)][1.64677 (22.2518s)][1.95096 (64.4961s)]]
  4181. [[cpp_int(fixed)][[*1] (2.35859s)][1.30986 (7.60133s)][1.67681 (22.6577s)][2.0895 (69.0758s)]]
  4182. [[gmp_int][1.03392 (2.4386s)][[*1] (5.80319s)][[*1] (13.5124s)][[*1] (33.0586s)]]
  4183. [[tommath_int][5.25978 (12.4057s)][4.4619 (25.8932s)][4.15577 (56.1542s)][3.91192 (129.323s)]]
  4184. ]
  4185. [table Operator powm
  4186. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4187. [[cpp_int][2.50722 (2.91621s)][3.5561 (13.406s)][4.37066 (73.483s)][4.88831 (473.91s)]]
  4188. [[cpp_int(fixed)][1.93385 (2.24931s)][3.18107 (11.9922s)][4.20753 (70.7403s)][4.8158 (466.88s)]]
  4189. [[gmp_int][[*1] (1.16313s)][[*1] (3.76986s)][[*1] (16.8128s)][[*1] (96.9476s)]]
  4190. [[tommath_int][1.44081 (1.67584s)][1.8794 (7.08507s)][2.19115 (36.8394s)][2.17186 (210.557s)]]
  4191. ]
  4192. [table Operator str
  4193. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4194. [[cpp_int][1.17175 (0.00160006s)][1.41999 (0.00329476s)][1.40856 (0.00813784s)][1.52964 (0.0229767s)]]
  4195. [[cpp_int(fixed)][[*1] (0.00136554s)][[*1] (0.00232027s)][[*1] (0.00577741s)][1.14754 (0.0172372s)]]
  4196. [[gmp_int][1.50501 (0.00205515s)][1.52968 (0.00354926s)][1.01989 (0.0058923s)][[*1] (0.015021s)]]
  4197. [[tommath_int][12.2161 (0.0166816s)][16.9577 (0.0393463s)][18.7474 (0.108311s)][22.7368 (0.341528s)]]
  4198. ]
  4199. [table Operator |
  4200. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4201. [[cpp_int][[*1] (0.0301617s)][[*1] (0.0423404s)][[*1] (0.0522358s)][[*1] (0.0813156s)]]
  4202. [[cpp_int(fixed)][1.0638 (0.0320861s)][1.22566 (0.0518951s)][1.28515 (0.0671305s)][1.16118 (0.094422s)]]
  4203. [[gmp_int][1.76553 (0.0532514s)][1.51489 (0.0641408s)][1.70708 (0.0891706s)][1.77346 (0.14421s)]]
  4204. [[tommath_int][4.37637 (0.131999s)][3.46212 (0.146587s)][2.91875 (0.152463s)][4.19621 (0.341217s)]]
  4205. ]
  4206. [table Operator |(int)
  4207. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4208. [[cpp_int][[*1] (0.0289129s)][[*1] (0.0351119s)][[*1] (0.0406779s)][[*1] (0.0525891s)]]
  4209. [[cpp_int(fixed)][1.06091 (0.030674s)][1.25979 (0.0442336s)][1.36194 (0.0554009s)][1.37438 (0.0722772s)]]
  4210. [[gmp_int][4.92854 (0.142498s)][4.34687 (0.152627s)][3.71442 (0.151095s)][2.981 (0.156768s)]]
  4211. [[tommath_int][10.9847 (0.317598s)][9.37065 (0.329021s)][8.53651 (0.347248s)][11.2155 (0.589813s)]]
  4212. ]
  4213. [table Operator ^
  4214. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4215. [[cpp_int][[*1] (0.0305149s)][[*1] (0.04217s)][[*1] (0.0525977s)][[*1] (0.0816632s)]]
  4216. [[cpp_int(fixed)][1.01544 (0.0309861s)][1.24872 (0.0526585s)][1.26661 (0.066621s)][1.15965 (0.0947007s)]]
  4217. [[gmp_int][1.64675 (0.0502505s)][1.47181 (0.0620663s)][1.66038 (0.0873322s)][1.67895 (0.137108s)]]
  4218. [[tommath_int][4.30668 (0.131418s)][3.45859 (0.145849s)][2.91462 (0.153303s)][4.15538 (0.339342s)]]
  4219. ]
  4220. [table Operator ^(int)
  4221. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4222. [[cpp_int][1.01566 (0.0296088s)][[*1] (0.0356634s)][[*1] (0.0401898s)][[*1] (0.0514097s)]]
  4223. [[cpp_int(fixed)][[*1] (0.0291524s)][1.2393 (0.0441976s)][1.38556 (0.0556856s)][1.38899 (0.0714075s)]]
  4224. [[gmp_int][4.68027 (0.136441s)][4.15243 (0.14809s)][3.74237 (0.150405s)][3.0483 (0.156712s)]]
  4225. [[tommath_int][10.919 (0.318314s)][9.16311 (0.326788s)][8.62554 (0.346659s)][11.6212 (0.597442s)]]
  4226. ]
  4227. [table Operator &
  4228. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4229. [[cpp_int][1.0346 (0.0303431s)][[*1] (0.0427309s)][[*1] (0.0535587s)][1.06945 (0.0828084s)]]
  4230. [[cpp_int(fixed)][[*1] (0.0293284s)][1.10435 (0.04719s)][1.05262 (0.0563769s)][[*1] (0.0774309s)]]
  4231. [[gmp_int][1.86057 (0.0545675s)][1.58432 (0.0676995s)][1.69164 (0.0906018s)][1.86625 (0.144505s)]]
  4232. [[tommath_int][4.4157 (0.129506s)][3.60396 (0.154s)][2.95985 (0.158525s)][4.4032 (0.340944s)]]
  4233. ]
  4234. [table Operator &(int)
  4235. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4236. [[cpp_int][1.05874 (0.038946s)][[*1] (0.0483903s)][[*1] (0.063842s)][[*1] (0.100361s)]]
  4237. [[cpp_int(fixed)][[*1] (0.0367853s)][1.05827 (0.0512099s)][1.09114 (0.0696605s)][1.09432 (0.109826s)]]
  4238. [[gmp_int][3.92298 (0.144308s)][2.99447 (0.144903s)][2.228 (0.14224s)][1.42296 (0.142809s)]]
  4239. [[tommath_int][8.79208 (0.323419s)][7.02288 (0.339839s)][5.65271 (0.36088s)][6.27104 (0.629365s)]]
  4240. ]
  4241. [table Operator <<
  4242. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4243. [[cpp_int][[*1] (0.0248801s)][1.23196 (0.04s)][[*1] (0.0424149s)][[*1] (0.060157s)]]
  4244. [[cpp_int(fixed)][1.08931 (0.027102s)][1.40572 (0.0456418s)][1.3475 (0.0571542s)][1.24573 (0.0749397s)]]
  4245. [[gmp_int][1.05561 (0.0262636s)][[*1] (0.0324686s)][1.09914 (0.0466199s)][1.16315 (0.0699719s)]]
  4246. [[tommath_int][1.60497 (0.0399319s)][2.13048 (0.0691737s)][2.31219 (0.0980712s)][2.74695 (0.165248s)]]
  4247. ]
  4248. [table Operator >>
  4249. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4250. [[cpp_int][[*1] (0.0213349s)][1.02127 (0.0295019s)][[*1] (0.0327116s)][1.13168 (0.0433804s)]]
  4251. [[cpp_int(fixed)][1.13514 (0.0242181s)][1.16938 (0.0337803s)][1.46999 (0.0480859s)][1.60077 (0.061362s)]]
  4252. [[gmp_int][1.26614 (0.0270129s)][[*1] (0.0288873s)][1.42219 (0.0465221s)][[*1] (0.0383329s)]]
  4253. [[tommath_int][12.0066 (0.25616s)][10.2837 (0.297067s)][9.99696 (0.327017s)][16.0943 (0.616942s)]]
  4254. ]
  4255. Test code was compiled with Microsoft Visual Studio 2010 with all optimisations
  4256. turned on (/Ox), and used MPIR-2.3.0 and [mpfr]-3.0.0. The tests were run on 32-bit
  4257. Windows Vista machine.
  4258. Linux x86_64 results are broadly similar, except that libtommath performs much better there.
  4259. [endsect]
  4260. [section:rational_performance Rational Type Performance]
  4261. Note that these tests are carefully designed to test performance of the underlying algorithms
  4262. and not memory allocation or variable copying. As usual, performance results should be taken
  4263. with a healthy dose of scepticism, and real-world performance may vary widely depending upon the
  4264. specifics of the program. In each table relative times are given first, with the best performer
  4265. given a score of 1. Total actual times are given in brackets, measured in seconds for 500000
  4266. operations.
  4267. [table Operator +
  4268. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4269. [[cpp_rational][5.89417 (18.4116s)][6.87256 (47.4698s)][6.65008 (107.715s)][6.53801 (256.244s)]]
  4270. [[mpq_rational][[*1] (3.1237s)][[*1] (6.90715s)][[*1] (16.1975s)][[*1] (39.1929s)]]
  4271. ]
  4272. [table Operator +(int)
  4273. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4274. [[cpp_rational][3.62367 (2.46488s)][4.18291 (2.94603s)][4.726 (3.74866s)][6.1388 (5.56817s)]]
  4275. [[mpq_rational][[*1] (0.680215s)][[*1] (0.704303s)][[*1] (0.7932s)][[*1] (0.907046s)]]
  4276. ]
  4277. [table Operator +(unsigned long long)
  4278. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4279. [[cpp_rational][1.1527 (2.6378s)][1.31751 (3.09863s)][1.58996 (4.00714s)][2.15642 (5.75702s)]]
  4280. [[mpq_rational][[*1] (2.28837s)][[*1] (2.35189s)][[*1] (2.52028s)][[*1] (2.66971s)]]
  4281. ]
  4282. [table Operator +=(unsigned long long)
  4283. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4284. [[cpp_rational][1.18436 (2.7059s)][1.32279 (3.11099s)][1.61398 (4.05389s)][2.20048 (5.84623s)]]
  4285. [[mpq_rational][[*1] (2.2847s)][[*1] (2.35183s)][[*1] (2.51174s)][[*1] (2.6568s)]]
  4286. ]
  4287. [table Operator -
  4288. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4289. [[cpp_rational][5.81893 (18.3457s)][6.82209 (47.1928s)][6.64143 (107.498s)][6.51362 (255.137s)]]
  4290. [[mpq_rational][[*1] (3.15277s)][[*1] (6.91765s)][[*1] (16.1859s)][[*1] (39.1698s)]]
  4291. ]
  4292. [table Operator -(int)
  4293. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4294. [[cpp_rational][3.72441 (2.48756s)][4.27663 (2.98713s)][4.62109 (3.72114s)][6.17605 (5.56503s)]]
  4295. [[mpq_rational][[*1] (0.667908s)][[*1] (0.698479s)][[*1] (0.805252s)][[*1] (0.901066s)]]
  4296. ]
  4297. [table Operator -(unsigned long long)
  4298. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4299. [[cpp_rational][1.15627 (2.63239s)][1.32096 (3.12092s)][1.61044 (4.00106s)][2.19378 (5.7644s)]]
  4300. [[mpq_rational][[*1] (2.27663s)][[*1] (2.36262s)][[*1] (2.48445s)][[*1] (2.62761s)]]
  4301. ]
  4302. [table Operator -=(unsigned long long)
  4303. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4304. [[cpp_rational][1.1984 (2.73444s)][1.34141 (3.15698s)][1.64159 (4.06997s)][2.23017 (5.88108s)]]
  4305. [[mpq_rational][[*1] (2.28174s)][[*1] (2.35348s)][[*1] (2.47929s)][[*1] (2.63706s)]]
  4306. ]
  4307. [table Operator *
  4308. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4309. [[cpp_rational][5.4306 (32.5882s)][6.91805 (89.9436s)][6.94556 (207.307s)][6.88704 (492.151s)]]
  4310. [[mpq_rational][[*1] (6.00084s)][[*1] (13.0013s)][[*1] (29.8475s)][[*1] (71.4604s)]]
  4311. ]
  4312. [table Operator *(int)
  4313. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4314. [[cpp_rational][2.12892 (2.51376s)][2.47245 (3.07841s)][2.86832 (3.93619s)][3.94086 (6.02565s)]]
  4315. [[mpq_rational][[*1] (1.18077s)][[*1] (1.24508s)][[*1] (1.3723s)][[*1] (1.52902s)]]
  4316. ]
  4317. [table Operator *(unsigned long long)
  4318. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4319. [[cpp_rational][1.32254 (5.43565s)][1.56078 (6.73163s)][1.97701 (9.32522s)][2.85404 (15.1573s)]]
  4320. [[mpq_rational][[*1] (4.11002s)][[*1] (4.313s)][[*1] (4.71682s)][[*1] (5.31082s)]]
  4321. ]
  4322. [table Operator *=(unsigned long long)
  4323. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4324. [[cpp_rational][6.29806 (58.1188s)][6.30556 (59.5076s)][6.3385 (62.1007s)][6.55345 (67.6905s)]]
  4325. [[mpq_rational][[*1] (9.22804s)][[*1] (9.43733s)][[*1] (9.79739s)][[*1] (10.329s)]]
  4326. ]
  4327. [table Operator /
  4328. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4329. [[cpp_rational][4.4269 (66.8031s)][6.40103 (173.527s)][6.32347 (348.193s)][6.61148 (824.063s)]]
  4330. [[mpq_rational][[*1] (15.0903s)][[*1] (27.1093s)][[*1] (55.0637s)][[*1] (124.641s)]]
  4331. ]
  4332. [table Operator /(int)
  4333. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4334. [[cpp_rational][1.78772 (2.50984s)][2.10623 (3.10606s)][2.46986 (3.99358s)][3.37428 (5.96678s)]]
  4335. [[mpq_rational][[*1] (1.40393s)][[*1] (1.4747s)][[*1] (1.61693s)][[*1] (1.76831s)]]
  4336. ]
  4337. [table Operator /(unsigned long long)
  4338. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4339. [[cpp_rational][1.29695 (5.45454s)][1.55248 (6.85353s)][1.93237 (9.28765s)][2.75211 (14.8541s)]]
  4340. [[mpq_rational][[*1] (4.20568s)][[*1] (4.41458s)][[*1] (4.80635s)][[*1] (5.39734s)]]
  4341. ]
  4342. [table Operator /=(unsigned long long)
  4343. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4344. [[cpp_rational][6.19401 (58.4278s)][6.20135 (59.643s)][6.21327 (62.0338s)][6.40576 (67.6778s)]]
  4345. [[mpq_rational][[*1] (9.43295s)][[*1] (9.61774s)][[*1] (9.98407s)][[*1] (10.5652s)]]
  4346. ]
  4347. [table Operator construct
  4348. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4349. [[cpp_rational][[*1] (0.00978288s)][[*1] (0.0100574s)][[*1] (0.0101393s)][[*1] (0.0101847s)]]
  4350. [[mpq_rational][39.1516 (0.383015s)][38.3523 (0.385725s)][37.5812 (0.381048s)][37.6007 (0.382953s)]]
  4351. ]
  4352. [table Operator construct(unsigned)
  4353. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4354. [[cpp_rational][[*1] (0.0548151s)][[*1] (0.0557542s)][[*1] (0.055825s)][[*1] (0.0552808s)]]
  4355. [[mpq_rational][7.21073 (0.395257s)][7.1016 (0.395944s)][7.02046 (0.391917s)][7.16881 (0.396297s)]]
  4356. ]
  4357. [table Operator construct(unsigned long long)
  4358. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4359. [[cpp_rational][[*1] (0.0605156s)][[*1] (0.0616657s)][[*1] (0.0592056s)][[*1] (0.0603081s)]]
  4360. [[mpq_rational][35.1604 (2.12775s)][34.7575 (2.14335s)][35.7232 (2.11502s)][35.0437 (2.11342s)]]
  4361. ]
  4362. [table Operator str
  4363. [[Backend][128 Bits][256 Bits][512 Bits][1024 Bits]]
  4364. [[cpp_rational][5.48898 (0.0208949s)][8.49668 (0.0546688s)][10.107 (0.121897s)][10.5339 (0.310584s)]]
  4365. [[mpq_rational][[*1] (0.0038067s)][[*1] (0.00643413s)][[*1] (0.0120606s)][[*1] (0.0294843s)]]
  4366. ]
  4367. Test code was compiled with Microsoft Visual Studio 2010 with all optimisations
  4368. turned on (/Ox), and used MPIR-2.3.0 and [mpfr]-3.0.0. The tests were run on 32-bit
  4369. Windows Vista machine.
  4370. [endsect]
  4371. [endsect]
  4372. [section:map Roadmap]
  4373. * Support for atomic loads and stores is not currently supported, but will become more important as a larger number of floating point algorithms become parallelized.
  4374. [section:hist History]
  4375. [h4 Multiprecision-3.2.3 (Boost-1.72)]
  4376. * Big `constexpr` update allows __cpp_int and __float128 arithmetic to be fully `constexpr` with gcc and clang 9 or later,
  4377. or any compiler supporting `std::is_constant_evaluated()`.
  4378. [h4 Multiprecision-3.1.3 (Boost-1.71)]
  4379. * Support hexfloat io-formatting for float128.
  4380. * Fix various bugs in variable precision interconversions.
  4381. * Respect uppercase for '0x' prefix outputs.
  4382. * Incorporate some unit tests from the Etherium project.
  4383. * Fix some gcc warnings.
  4384. [h4 Multiprecision-3.1.2 (Boost-1.70)]
  4385. * Fix various conversion issues in the traits classes, check for compatibility with Boost.Optional.
  4386. * Prevent instantiation of `std::numeric_limits` on any old type when checking for convertibility. See [@https://github.com/boostorg/multiprecision/issues/98 #98].
  4387. * Update variable precision code to accound for arbitrary precision integers. See [@https://github.com/boostorg/multiprecision/issues/103 #103].
  4388. * Add support for XML serialization archives.
  4389. * Fix bug in fixed precision iostream formatting in `mpf_float` and `mpfr_float`. See [@https://github.com/boostorg/multiprecision/issues/113 #113].
  4390. * Add more overloads for special functions which are better handled natively by MPFR.
  4391. * Fixed bug in generic `exp` implementation which could cause invariant failure.
  4392. * Fixed generic conversion from float to integer to avoid undefined behaviour. See [@https://github.com/boostorg/multiprecision/issues/110 #110].
  4393. [h4 Multiprecision-3.1.1 (Boost-1.69)]
  4394. * Big update to better support variable precision types so that the precision of the result
  4395. is always the largest of all the arguments.
  4396. * Add support for allocators that are `final` in __cpp_int.
  4397. * Removed use of deprecated Boost.Endian in favour of Predef.
  4398. * Add support for `std::string_view`.
  4399. * Fixed minor bug in constant initialization. See [@https://github.com/boostorg/multiprecision/issues/67 #67].
  4400. * Make assignment of non-finite value to `cpp_int` a runtime errors. See [@https://github.com/boostorg/multiprecision/issues/58 #58].
  4401. * Added typedefs for `cpp_bin_float_oct` and `cpp_complex_oct`.
  4402. [h4 Multiprecision-3.1.0 (Boost-1.68)]
  4403. * Support added for complex multiprecision numbers.
  4404. * Changed conversion to unsigned integer types to be truncating to match standard defined behaviour.
  4405. * Correct bug in MPFR string formatting.
  4406. * Fix undefined behaviour in cpp_dec_float conversion from long long.
  4407. * Add support for Eigen interoperability.
  4408. * float128.hpp: Fix Intel on Windows build.
  4409. * Fix type used in temporaries when expanding expression templates containing mixed expressions.
  4410. * Fix infinite loop in gmp_float to fixed-point string conversion.
  4411. * Update the expression templates to issue static_asserts with better error messages when you try and do something unsupported.
  4412. * Fix bug in cpp_int where incrementing to zero doesn't set the sign correctly.
  4413. * Remove erroneous use of std::move, and rely on NVRO instead.
  4414. * Fix up support for changes to MPIR-3.0.
  4415. * Fix various conversion errors in cpp_bin_float when the exponent type is a `long long`, or else we're converting to
  4416. an integer that is wider than we are.
  4417. * Fix compatibility issue with GCC-8 caused by the introduction of `std::byte`.
  4418. [h4 Multiprecision-3.0.0 (Boost-1.67)]
  4419. * [*Breaking Change:] When converting a multiprecision integer to a narrower type, if the value is too large (or negative) to
  4420. fit in the smaller type, then the result is either the maximum (or minimum) value of the target type. This was always the intended
  4421. behaviour, but was somewhat haphazardly enforced before. If you really do want just the low order N bits of a value, then you will
  4422. need to mask these out prior to the case, for example: `static_cast<unsigned>(~static_cast<unsigned>(0) & my_value)`. Note that
  4423. technically (to avoid undefined behaviour) you should do the same thing with built in integer types too.
  4424. See [@https://svn.boost.org/trac/boost/ticket/13109 #13109].
  4425. * Fix bug in conversion of decimal to rational types (zero needs special handling), see [@https://svn.boost.org/trac/boost/ticket/13148 #13148].
  4426. * Fix conversion from cpp_bin_float to a wider built in integer type, see [@https://svn.boost.org/trac/boost/ticket/13301 #13301].
  4427. * Improve performance heurists used in cpp_bin_float exp function.
  4428. * Fix bug in floor/ceil and cpp_bin_float when the exponent type is wider than an int, see [@https://svn.boost.org/trac/boost/ticket/13264 #13264].
  4429. * Disable explicit conversion operator when the target type is already constructible from this type, see [@https://github.com/boostorg/multiprecision/issues/30 #30].
  4430. * Fix support for changes new to MPIR-3.0, see [@https://svn.boost.org/trac/boost/ticket/13124 #13124].
  4431. [h4 Multiprecision-2.3.2 (Boost-1.65)]
  4432. * Updated expression templates to store arithmetic literals directly in the expression template to prevent dangling references, see [@https://github.com/boostorg/multiprecision/issues/19 #19].
  4433. * Fix various issues with huge values and overflow in the trig, pow and exp functions, see [@https://github.com/boostorg/multiprecision/issues/24 #24].
  4434. * Fix error handling of checked cpp_int multiply that could cause some overflows to be missed.
  4435. [h4 Multiprecision-2.3.1 (Boost-1.64)]
  4436. * In `cpp_bin_float` prevent double rounding when converting to a denormalized float. See [@https://svn.boost.org/trac/boost/ticket/12527 #12527].
  4437. * Fix bug in integer sqrt for very small integers. See [@https://svn.boost.org/trac/boost/ticket/12559 #12559].
  4438. * Fix conversion to signed-zero in `cpp_bin_float`.
  4439. * Change `cpp_bin_float` rounding code to round at arbitrary location so we can use it for conversions, see [@https://svn.boost.org/trac/boost/ticket/12527 #12527].
  4440. * Improve performance of 128-bit bit-scan operations.
  4441. * Fix subtraction of very small quantities in `cpp_bin_float`. See: [@https://svn.boost.org/trac/boost/ticket/12580 #12580].
  4442. * Bring error handling into line with C99 Annex F. See [@https://svn.boost.org/trac/boost/ticket/12581 #12581].
  4443. * Fix bitwise export of trivial `cpp_int`'s, see [@https://svn.boost.org/trac/boost/ticket/12627 #12627].
  4444. * Fix `ilogb` (and code that uses it) to consistently return the smallest value of the exponent type when the argument is zero, see [@https://svn.boost.org/trac/boost/ticket/12625 #12625].
  4445. * Allow conversion from `__float128` to `cpp_bin_float`.
  4446. * Fix bug in left shift of `cpp_int` which would result in bit-loss, see [@https://svn.boost.org/trac/boost/ticket/12790 #12790].
  4447. * Fixed bugs in bounded but variable precision `cpp_int`'s caused by over-aggressive constexpr optimization, see [@https://svn.boost.org/trac/boost/ticket/12798 #12798].
  4448. [h4 Multiprecision-2.3.0 (Boost-1.63)]
  4449. * Added support for all the C99 math functions.
  4450. * Extended generic-interconversions to handle narrowing cases as well, changed convert_to member function and hence explicit
  4451. conversion operators to use the same conversion code as the explicit constructors.
  4452. * Fix IO stream parsing error, see [@https://svn.boost.org/trac/boost/ticket/12488 #12488].
  4453. * Make default constructed floating point types all zero for consistency, see [@https://svn.boost.org/trac/boost/ticket/12500 #12500].
  4454. * Fix conversion of cpp_bin_float to float/double/long double when the exponent value would overflow an int, see [@https://svn.boost.org/trac/boost/ticket/12512 #12512].
  4455. * Fix cpp_bin_float subtractions that yield signed-zeros, see [@https://svn.boost.org/trac/boost/ticket/12524 #12524].
  4456. * Fix ceil/trunc/round applied to cpp_bin_float and yielding a signed zero, see [@https://svn.boost.org/trac/boost/ticket/12525 #12525].
  4457. [h4 Multiprecision-2.2.8 (Boost-1.62)]
  4458. * Added support for hashing via `Boost.Hash` or `std::hash`.
  4459. * Fixed some arithmetic operations in cpp_bin_float and cpp_dec_float that should generate a NaN, see [@https://svn.boost.org/trac/boost/ticket/12157 #12157].
  4460. * Prevent inadvertant instantiation of variable-precision `mpfr_float_backend` with fixed allocation.
  4461. * Fixed division over/underflow in cpp_bin_float, see [@https://svn.boost.org/trac/boost/ticket/12167 #12167].
  4462. * Added support for signed-zeros throughout the library, including support for `signbit` and `copysign`, mpfr, float128, and cpp_bin_float types
  4463. should now respect signed-zeros correctly.
  4464. * Fixed bug in conversion of cpp_bin_float infinities to double etc, see [@https://svn.boost.org/trac/boost/ticket/12196 #12196].
  4465. * Fix add and subtract of cpp_bin_float when the exponents would overflow., see [@https://svn.boost.org/trac/boost/ticket/12198 #12198].
  4466. * Improve variable-precision support in mpfr and mpf backends, allow these types to be used with Boost.Math.
  4467. * Fixed bug in subtraction of signed infinities in cpp_bin_float, see [@https://svn.boost.org/trac/boost/ticket/12209 #12209].
  4468. * Fix result of sqrt(infinity) in cpp_bin_float (result should be infinity), see [@https://svn.boost.org/trac/boost/ticket/12227 #12227].
  4469. * Added workaround in gmp.hpp for recent MPIR releases which are not quite source-compatible with GMP on Windows x64.
  4470. * Allow `cpp_int` code to be used with /RTCc with MSVC.
  4471. * Fix conversion of `cpp_int` to signed integer when the result is INT_MIN, see [@https://svn.boost.org/trac/boost/ticket/12343 #12343].
  4472. * Update uBlas support to match latest uBlas code.
  4473. * Fix some errors present when building on big-endian machines (not all `cpp_int` constructors are available on non-little-endian machines).
  4474. * Fix fencepost error in rational to float conversion routines, see [@https://svn.boost.org/trac/boost/ticket/12327 #12327].
  4475. * Fix some Oracle C++ compiler compatibility issues.
  4476. * Add modf support to complete C90 compatibility.
  4477. * Fix self assignment bug in expression template code for expressions such as `a = a * a * a`, see [@https://svn.boost.org/trac/boost/ticket/12408 #12408].
  4478. * Fixed some compiler errors that occur when converting from `cpp_int` to `cpp_bin_float`.
  4479. [h4 Multiprecision-2.2.7 (Boost-1.61)]
  4480. * Fixed bug in stream input for integer types, see [@https://svn.boost.org/trac/boost/ticket/11857 #11857].
  4481. * Fixed some ambiguous conversions involving expression templates see [@https://svn.boost.org/trac/boost/ticket/11922 #11922].
  4482. * Add expression template aware min/max overloads see [@https://svn.boost.org/trac/boost/ticket/11149 #11149].
  4483. * Fix bug in right shifting negative small integers in cpp_int see [@https://svn.boost.org/trac/boost/ticket/11999 #11999].
  4484. * Use memmove for bitshifts in cpp_int when possible see [@https://svn.boost.org/trac/boost/ticket/9233 #9233].
  4485. * Use memcpy for data import into cpp_int where possible, see [@https://svn.boost.org/trac/boost/ticket/9235 #9235].
  4486. * Changed `cpp_bin_float.convert_to<double>()` to a function template rather than proceding via `long double` to avoid
  4487. double-rounding bug, see [@https://svn.boost.org/trac/boost/ticket/12039 #12039].
  4488. * Fixed conversions from NaN's and Infinities, see [@https://svn.boost.org/trac/boost/ticket/12112 #12112].
  4489. * Enabled better support for Clang on Windows.
  4490. * Fixed handling of NaN's and Infinities in basic arithmetic for cpp_dec_float and cpp_bin_float, see [@https://svn.boost.org/trac/boost/ticket/12090 #12090].
  4491. * Fixed fencepost error in cpp_bin_float subtraction.
  4492. * Fixed double-rounding in conversion to float routines for cpp_bin_float, see [@https://svn.boost.org/trac/boost/ticket/12039 #12039].
  4493. * Make float128 denorm aware, see [@https://svn.boost.org/trac/boost/ticket/12075 #12075].
  4494. * Allow the library and tests to be used without exception handling support, see [@https://svn.boost.org/trac/boost/ticket/12070 #12070].
  4495. * Fixed buggy comparison operator overloads for boost::rational.
  4496. * Added some workarounds for Oracle C++.
  4497. * Fixed some missing typecasts for cases where cpp_int's limb_type is wider than unsigned.
  4498. [h4 Multiprecision-2.2.6 (Boost-1.60)]
  4499. * Fixed result of Miller Rabin primality test for value 2, see [@https://svn.boost.org/trac/boost/ticket/11495 #11495].
  4500. * Improved initialization of cpp_int from very large strings of hex or octal digits, see [@https://svn.boost.org/trac/boost/ticket/11590 #11590].
  4501. * Fixed fmod behaviour for negative divisors, see [@https://svn.boost.org/trac/boost/ticket/11641 #11641].
  4502. * Fixed infinite division loop in cpp_int special case, see [@https://svn.boost.org/trac/boost/ticket/11648 #11648].
  4503. * Patched missing return statement in [@https://svn.boost.org/trac/boost/ticket/11762 #11762].
  4504. * Fixed mixed mode arithmetic compiler error in [@https://svn.boost.org/trac/boost/ticket/11764 #11764].
  4505. * Fixed over-aggressive use of noexcept in [@https://svn.boost.org/trac/boost/ticket/11826 #11826].
  4506. [h4 Multiprecision-2.2.5 (Boost-1.59)]
  4507. * Depricated boost/multiprecision/random.hpp as it's no longer needed, updated random examples to match.
  4508. * Fixed a bug in cpp_int's right shift operator when shifting negative values - semantics now gives the
  4509. same values as shifting 2's compliment integers, though not the same bit pattern.
  4510. * Fixed support for GCC-4.6.4 in C++0x mode by disabling conditional noexcept suppoprt for that compiler
  4511. see [@https://svn.boost.org/trac/boost/ticket/11402 #11402].
  4512. * Suppressed numerous compiler warnings.
  4513. [h4 Multiprecision-2.2.4 (Boost-1.58)]
  4514. * Changed `frexp` to always be non-expression template generating, see: [@https://svn.boost.org/trac/boost/ticket/10993 10993].
  4515. * Improved support of cpp_dec_float on Cygwin and other platforms with missing long double support, see [@https://svn.boost.org/trac/boost/ticket/10924 10924].
  4516. * Improved noexcept support and added some more tests, see [@https://svn.boost.org/trac/boost/ticket/10990 10990].
  4517. * Various workarounds applied for Intel-15.0 and Solaris-12.4 compilers.
  4518. [h4 Multiprecision-2.2.3 (Boost-1.57)]
  4519. * Changed rational to float conversions to exactly round to nearest, see [@https://svn.boost.org/trac/boost/ticket/10085 10085].
  4520. * Added improved generic float to rational conversions.
  4521. * Fixed rare bug in exponent function for __cpp_bin_float.
  4522. * Fixed various minor documentation issues.
  4523. [h4 Multiprecision-2.2.2 (Boost-1.56)]
  4524. * Change floating-point to rational conversions to be implicit, see [@https://svn.boost.org/trac/boost/ticket/10082 10082].
  4525. * Fix definition of checked_cpp_rational typedef.
  4526. [h4 Multiprecision-2.2.1]
  4527. * Fix bug in assignment from string in cpp_int, see [@https://svn.boost.org/trac/boost/ticket/9936 9936].
  4528. [h4 Multiprecision-2.2.0]
  4529. * Moved to Boost.Multiprecision specific version number - we have one breaking change in Boost-1.54
  4530. which makes this major version 2, plus two releases with new features since then.
  4531. * Added new __cpp_bin_float backend for binary floating-point.
  4532. * Added MSVC-specific #include for compiler intrinsics, see [@https://svn.boost.org/trac/boost/ticket/9336 9336].
  4533. * Fixed various typos in docs, see [@https://svn.boost.org/trac/boost/ticket/9432 9432].
  4534. * Fixed __gmp_rational to allow move-copy from an already copied-from object, see [@https://svn.boost.org/trac/boost/ticket/9497 9497].
  4535. * Added list of values for numeric_limits.
  4536. [h4 Boost-1.55]
  4537. * Added support for Boost.Serialization.
  4538. * Suppressed some GCC warnings. See [@https://svn.boost.org/trac/boost/ticket/8872 8872].
  4539. * Fixed bug in pow for large integer arguments. See [@https://svn.boost.org/trac/boost/ticket/8809 8809].
  4540. * Fixed bug in pow for calculation of 0[super N]. See [@https://svn.boost.org/trac/boost/ticket/8798 8798].
  4541. * Fixed bug in fixed precision cpp_int IO code that causes conversion to string to fail when the
  4542. bit count is very small (less than CHAR_BIT). See [@https://svn.boost.org/trac/boost/ticket/8745 8745].
  4543. * Fixed bug in cpp_int that causes left shift to fail when a fixed precision type would overflow.
  4544. See [@https://svn.boost.org/trac/boost/ticket/8741 8741].
  4545. * Fixed some cosmetic warnings from cpp_int. See [@https://svn.boost.org/trac/boost/ticket/8748 8748].
  4546. * Fixed calls to functions which are required to be macros in C99. See [@https://svn.boost.org/trac/boost/ticket/8732 8732].
  4547. * Fixed bug that causes construction from INT_MIN, LONG_MIN etc to fail in cpp_int. See [@https://svn.boost.org/trac/boost/ticket/8711 8711].
  4548. [h4 1.54]
  4549. * [*Breaking change] renamed `rational_adapter` to `rational_adaptor`.
  4550. * Add support for [mpfi].
  4551. * Add logged_adaptor.
  4552. * Add support for 128-bit floats via GCC's `__float128` or Intel's `_Quad` data types.
  4553. * Add support for user-defined literals in cpp_int, improve `constexpr` support.
  4554. * Fixed bug in integer division of `cpp_int` that results in incorrect sign of `cpp_int` when both arguments are small enough
  4555. to fit in a `double_limb_type`. See [@https://svn.boost.org/trac/boost/ticket/8126 8126].
  4556. * Fixed bug in subtraction of a single limb in `cpp_int` that results in incorrect value when the result should have a 0
  4557. in the last limb: [@https://svn.boost.org/trac/boost/ticket/8133 8133].
  4558. * Fixed bug in `cpp_int` where division of 0 by something doesn't get zero in the result: [@https://svn.boost.org/trac/boost/ticket/8160 8160].
  4559. * Fixed bug in some transcendental functions that caused incorrect return values when variables are reused, for example with
  4560. `a = pow(a, b)`. See [@https://svn.boost.org/trac/boost/ticket/8326 8326].
  4561. * Fixed some assignment operations in the mpfr and gmp backends to be safe if the target has been moved from: [@https://svn.boost.org/trac/boost/ticket/8326 8667].
  4562. * Fixed bug in `cpp_int` that gives incorrect answer for 0%N for large N: [@https://svn.boost.org/trac/boost/ticket/8670 8670].
  4563. * Fixed set_precision in mpfr backend so it doesn't trample over an existing value: [@https://svn.boost.org/trac/boost/ticket/8692 8692].
  4564. [h4 1.53]
  4565. * First Release.
  4566. * Fix bug in [@https://svn.boost.org/trac/boost/ticket/7878 cpp_int division].
  4567. * Fix issue [@https://svn.boost.org/trac/boost/ticket/7806 #7806].
  4568. [h4 Post review changes]
  4569. * Non-expression template operators further optimised with rvalue reference support.
  4570. * Many functions made `constexp`.
  4571. * Differentiate between explicit and implicit conversions in the number constructor.
  4572. * Removed "mp_" prefix from types.
  4573. * Allowed mixed precision arithmetic.
  4574. * Changed ExpressionTemplates parameter to class `number` to use enumerated values rather than true/false.
  4575. * Changed ExpressionTemplate parameter default value to use a traits class so that the default value depends on the backend used.
  4576. * Added support for fused-multiply-add/subtract with GMP support.
  4577. * Tweaked expression template unpacking to use fewer temporaries when the LHS also appears in the RHS.
  4578. * Refactored `cpp_int_backend` based on review comments with new template parameter structure.
  4579. * Added additional template parameter to `mpfr_float_backend` to allow stack-based allocation.
  4580. * Added section on mixed precision arithmetic, and added support for operations yielding a higher precision result
  4581. than either of the arguments.
  4582. * Added overloads of integer-specific functions for built in integer types.
  4583. [h4 Pre-review history]
  4584. *2011-2012, John Maddock adds an expression template enabled front end to Christopher's code,
  4585. and adds support for other backends.
  4586. * 2011, Christopher Kormanyos publishes the decimal floating-point code under the Boost
  4587. Software Licence. The code is published as: [@http://doi.acm.org/10.1145/1916461.1916469
  4588. "Algorithm 910: A Portable C++ Multiple-Precision
  4589. System for Special-Function Calculations"], in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM,
  4590. 2011.
  4591. * 2002-2011, Christopher Kormanyos develops the all C++ decimal arithmetic floating-point
  4592. code.
  4593. [endsect]
  4594. [section:todo TODO]
  4595. More a list of what ['could] be done, rather than what ['should] be done (which may be a much smaller list!).
  4596. * Add back-end support for libdecNumber.
  4597. * Add an adaptor back-end for complex number types.
  4598. * Add better multiplication routines (Karatsuba, FFT etc) to cpp_int_backend.
  4599. * Add assembly level routines to cpp_int_backend.
  4600. * Can ring types (exact floating-point types) be supported? The answer should be yes, but someone needs to write it,
  4601. the hard part is IO and binary-decimal conversion.
  4602. * Should there be a choice of rounding mode (probably MPFR specific)?
  4603. * We can reuse temporaries in multiple subtrees (temporary caching).
  4604. * cpp_dec_float should round to nearest.
  4605. * A 2's complement fixed precision int that uses exactly N bits and no more.
  4606. Things requested in review:
  4607. * The performances of mp_number<a_trivial_adaptor<float>, false>respect to
  4608. float and mp_number<a_trivial_adaptor<int>, false> and int should be
  4609. given to show the cost of using the generic interface (Mostly done, just need to update docs to the latest results).
  4610. * Should we provide min/max overloads for expression templates? (Not done - we can't overload functions declared in the std namespace :-( ).
  4611. * The rounding applied when converting must be documented (Done).
  4612. * Document why we don't abstract out addition/multiplication algorithms etc. (done - FAQ)
  4613. * Document why we don't use proto (compile times) (Done).
  4614. * We can reuse temporaries in multiple subtrees (temporary caching) Moved to TODO list.
  4615. * Emphasise in the docs that ET's may reorder operations (done 2012/10/31).
  4616. * Document what happens to small fixed precision cpp_int's (done 2012/10/31).
  4617. * The use of bool in template parameters could be improved by the use of
  4618. an enum class which will be more explicit. E.g `enum class expression_template {disabled, enabled};
  4619. enum class sign {unsigned, signed};` (Partly done 2012/09/15, done 2012/10/31).
  4620. * Each back-end should document the requirements it satisfies (not currently scheduled for inclusion: it's
  4621. deliberately an implementation detail, and "optional" requirements are optimisations which can't be detected
  4622. by the user). Not done: this is an implementation detail, the exact list of requirements satisfied is purely
  4623. an optimization, not something the user can detect.
  4624. * A backend for an overflow aware integers (done 2012/10/31).
  4625. * IIUC convert_to is used to emulate in c++98 compilers C++11 explicit
  4626. conversions. Could the explicit conversion operator be added on
  4627. compilers supporting it? (Done 2012/09/15).
  4628. * The front-end should make the differences between implicit and explicit
  4629. construction (Done 2012/09/15).
  4630. * The tutorial should add more examples concerning implicit or explicit
  4631. conversions. (Done 2012/09/15).
  4632. * The documentation must explain how move semantics helps in this domain
  4633. and what the backend needs to do to profit from this optimization. (Done 2012/09/15).
  4634. * The documentation should contain Throws specification on the mp_number
  4635. and backend requirements operations. (Done 2012/09/15).
  4636. * The library interface should use the noexcept (BOOST_NOEXCEPT, ...)
  4637. facilities (Done 2012/09/15).
  4638. * It is unfortunate that the generic mp_number front end can not make use
  4639. constexpr as not all the backends can ensure this (done - we can go quite a way).
  4640. * literals: The library doesn't provide some kind of literals. I think that the
  4641. mp_number class should provide a way to create literals if the backend
  4642. is able to. (Done 2012/09/15).
  4643. * The ExpresionTemplate parameter could be defaulted to a traits class for more sensible defaults (done 2012/09/20).
  4644. * In a = exp1 op exp2 where a occurs inside one of exp1 or exp2 then we can optimise and eliminate one more temporary (done 2012/09/20).
  4645. [h4 Pre-Review Comments]
  4646. * Make fixed precision orthogonal to Allocator type in cpp_int. Possible solution - add an additional MaxBits
  4647. template argument that defaults to 0 (meaning keep going till no more space/memory). Done.
  4648. * Can ring types (exact floating-point types) be supported? The answer should be yes, but someone needs to write it (Moved to TODO list).
  4649. * Should there be a choice of rounding mode (probably MPFR specific)? Moved to TODO list.
  4650. * Make the exponent type for cpp_dec_float a template parameter, maybe include support for big-integer exponents.
  4651. Open question - what should be the default - int32_t or int64_t? (done 2012/09/06)
  4652. * Document the size requirements of fixed precision ints (done 2012/09/15).
  4653. * Document std lib function accuracy (done 2012/09/15).
  4654. * Be a bit clearer on the effects of sign-magnitude representation of cpp_int - min == -max etc - done.
  4655. * Document cpp_dec_float precision, rounding, and exponent size (done 2012/09/06).
  4656. * Can we be clearer in the docs that mixed arithmetic doesn't work (no longer applicable as of 2012/09/06)?
  4657. * Document round functions behaviour better (they behave as in C++11) (added note 2012/09/06).
  4658. * Document limits on size of cpp_dec_float (done 2012/09/06).
  4659. * Add support for fused multiply add (and subtract). GMP mpz_t could use this (done 2012/09/20).
  4660. [endsect]
  4661. [section:faq FAQ]
  4662. [variablelist
  4663. [[Why do I get compiler errors when passing a `number` to a template function?]
  4664. [Most likely you are actually passing an expression template type to the function and
  4665. template-argument-deduction deduces the "wrong" type. Try casting the arguments
  4666. involving expressions to the actual number type, or as a last resort turning off
  4667. expression template support in the number type you are using.]]
  4668. [[When is expression template support a performance gain?]
  4669. [As a general rule, expression template support adds a small runtime overhead
  4670. creating and unpacking the expression templates, but greatly reduces the number of
  4671. temporaries created. So it's most effective in improving performance when the cost
  4672. of creating a temporary is high: for example when creating a temporary involves a memory
  4673. allocation. It is least effective (and may even be a dis-optimisation) when temporaries
  4674. are cheap: for example if the number type is basically a thin wrapper around a native
  4675. arithmetic type. In addition, since the library makes extensive use of thin inline wrapper
  4676. functions, turning on compiler optimization is essential to achieving high performance.]]
  4677. [[Do expression templates reorder operations?]
  4678. [Yes they do, sometimes quite radically so, if this is a concern then they should be turned
  4679. off for the number type you are using.]]
  4680. [[I can't construct my number type from ['some other type], but the docs indicate that the conversion should be allowed, what's up?]
  4681. [Some conversions are ['explicit], that includes construction from a string, or constructing from any type
  4682. that may result in loss of precision (for example constructing an integer type from a float).]]
  4683. [[Why do I get an exception thrown (or the program crash due to an uncaught exception) when using the bitwise operators on a checked `cpp_int`?]
  4684. [Bitwise operations on negative values (or indeed any signed integer type) are unspecified by the standard. As a result
  4685. any attempt to carry out a bitwise operation on a negative checked-integer will result in a `std::range_error` being thrown.]]
  4686. [[Why do I get compiler errors when trying to use the complement operator?]
  4687. [Use of the complement operator on signed types is problematic as the result is unspecified by the standard, and is further
  4688. complicated by the fact that most extended precision integer types use a sign-magnitude representation rather than the 2's
  4689. complement one favored by most native integer types. As a result the complement operator is deliberately disabled for
  4690. checked `cpp_int`'s. Unchecked `cpp_int`'s give the same valued result as a 2's complement type would, but not the same bit-pattern.]]
  4691. [[Why can't I negate an unsigned type?]
  4692. [The unary negation operator is deliberately disabled for unsigned integer types as its use would almost always be a programming error.]]
  4693. [[Why doesn't the library use proto?]
  4694. [A very early version of the library did use proto, but compile times became too slow
  4695. for the library to be usable. Since the library only required a tiny fraction of what
  4696. proto has to offer anyway, a lightweight expression template mechanism was used instead.
  4697. Compile times are still too slow...]]
  4698. [[Why not abstract out addition/multiplication algorithms?]
  4699. [This was deemed not to be practical: these algorithms are intimately
  4700. tied to the actual data representation used.]]
  4701. [[How do I choose between Boost.Multiprecision `cpp_bin_50` and `cpp_dec_50`?]
  4702. [Unless you have a specific reason to choose `cpp_dec_`, then the default choice should be `cpp_bin_`,
  4703. for example using the convenience `typedefs` like `boost::multiprecision::cpp_bin_50` or `boost::multiprecision::cpp_bin_100`.
  4704. In general, both work well and give the same results and at roughly the same speed with `cpp_dec_50` sometimes faster.
  4705. `cpp_dec_` was developed first paving the way for `cpp_bin_`.
  4706. `cpp_dec_` has several guard digits and is not rounded at all, using 'brute force' to get the promised number of decimal digits correct,
  4707. but making it difficult to reason about precision and computational uncertainty, for example see [*https://svn.boost.org/trac10/ticket/12133].
  4708. It also has a fast but imprecise division operator giving surprising results sometimes, see [*https://svn.boost.org/trac10/ticket/11178].
  4709. `cpp_bin_` is correctly/exactly rounded making it possible to reason about both the precision and rounding of the results.]]
  4710. ] [/variablelist]
  4711. [endsect]
  4712. [section:ack Acknowledgements]
  4713. This library would not have happened without:
  4714. * Christopher Kormanyos' C++ decimal number code.
  4715. * Paul Bristow for patiently testing, and commenting on the library.
  4716. * All the folks at GMP, MPFR and libtommath, for providing the "guts" that makes this library work.
  4717. * [@http://www-cs-faculty.stanford.edu/~uno/taocp.html "The Art Of Computer Programming"],
  4718. Donald E. Knuth, Volume 2: Seminumerical Algorithms, Third Edition
  4719. (Reading, Massachusetts: Addison-Wesley, 1997), xiv+762pp. ISBN 0-201-89684-2
  4720. [endsect]
  4721. [endsect]
  4722. [section:indexes Indexes]
  4723. '''
  4724. <index type="function_name">
  4725. <title>Function Index</title>
  4726. </index>
  4727. <index type="class_name">
  4728. <title>Class Index</title>
  4729. </index>
  4730. <index type="typedef_name">
  4731. <title>Typedef Index</title>
  4732. </index>
  4733. <index/>
  4734. '''
  4735. [endsect]