Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / multiprecision / test / test_constants.cpp
1 // Copyright John Maddock 2012.
2
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #ifdef _MSC_VER
9 #define _SCL_SECURE_NO_WARNINGS
10 #endif
11
12 #if !defined(TEST_MPF_50) && !defined(TEST_CPP_DEC_FLOAT) && !defined(TEST_MPFR_50)
13 #define TEST_MPF_50
14 #define TEST_CPP_DEC_FLOAT
15 #define TEST_MPFR_50
16
17 #ifdef _MSC_VER
18 #pragma message("CAUTION!!: No backend type specified so testing everything.... this will take some time!!")
19 #endif
20 #ifdef __GNUC__
21 #pragma warning "CAUTION!!: No backend type specified so testing everything.... this will take some time!!"
22 #endif
23
24 #endif
25
26 #if defined(TEST_MPF_50)
27 #include <boost/multiprecision/gmp.hpp>
28 #endif
29 #if defined(TEST_MPFR_50)
30 #include <boost/multiprecision/mpfr.hpp>
31 #endif
32 #ifdef TEST_CPP_DEC_FLOAT
33 #include <boost/multiprecision/cpp_dec_float.hpp>
34 #endif
35
36 #include "test.hpp"
37
38 static const char* ln2 =
39     "0.693147180559945309417232121458176568075500134360255254120680009493393621969694"
40     "71560586332699641868754200148102057068573368552023575813055703267075163507596193"
41     "07275708283714351903070386238916734711233501153644979552391204751726815749320651"
42     "55524734139525882950453007095326366642654104239157814952043740430385500801944170"
43     "64167151864471283996817178454695702627163106454615025720740248163777338963855069"
44     "52606683411372738737229289564935470257626520988596932019650585547647033067936544"
45     "32547632744951250406069438147104689946506220167720424524529612687946546193165174"
46     "68139267250410380254625965686914419287160829380317271436778265487756648508567407"
47     "76484514644399404614226031930967354025744460703080960850474866385231381816767514"
48     "38667476647890881437141985494231519973548803751658612753529166100071053558249879"
49     "41472950929311389715599820565439287170007218085761025236889213244971389320378439"
50     "35308877482597017155910708823683627589842589185353024363421436706118923678919237"
51     "23146723217205340164925687274778234453534764811494186423867767744060695626573796"
52     "00867076257199184734022651462837904883062033061144630073719489002743643965002580"
53     "93651944304119115060809487930678651588709006052034684297361938412896525565396860"
54     "22194122924207574321757489097706752687115817051137009158942665478595964890653058"
55     "46025866838294002283300538207400567705304678700184162404418833232798386349001563"
56     "12188956065055315127219939833203075140842609147900126516824344389357247278820548"
57     "62715527418772430024897945401961872339808608316648114909306675193393128904316413"
58     "70681397776498176974868903887789991296503619270710889264105230924783917373501229"
59     "84242049956893599220660220465494151061391878857442455775102068370308666194808964"
60     "12186807790208181588580001688115973056186676199187395200766719214592236720602539"
61     "59543654165531129517598994005600036651356756905124592682574394648316833262490180"
62     "38242408242314523061409638057007025513877026817851630690255137032340538021450190"
63     "15374029509942262995779647427138157363801729873940704242179972266962979939312706"
64     "93574724049338653087975872169964512944649188377115670167859880498183889678413493"
65     "83140140731664727653276359192335112333893387095132090592721854713289754707978913"
66     "84445466676192702885533423429899321803769154973340267546758873236778342916191810"
67     "43011609169526554785973289176354555674286387746398710191243175425588830120677921"
68     "02803412068797591430812833072303008834947057924965910058600123415617574132724659"
69     "43068435465211135021544341539955381856522750221424566440006276183303206472725721"
70     "97515290827856842132079598863896727711955221881904660395700977470651261950527893"
71     "22960889314056254334425523920620303439417773579455921259019925591148440242390125"
72     "54259003129537051922061506434583787873002035414421785758013236451660709914383145"
73     "00498589668857722214865288216941812704886075897220321666312837832915676307498729"
74     "85746389282693735098407780493950049339987626475507031622161390348452994249172483"
75     "73406136622638349368111684167056925214751383930638455371862687797328895558871634"
76     "429756244755392366369488877823890174981027";
77 static const char* pi =
78     "3.141592653589793238462643383279502884197169399375105820974944592307816406286208"
79     "99862803482534211706798214808651328230664709384460955058223172535940812848111745"
80     "02841027019385211055596446229489549303819644288109756659334461284756482337867831"
81     "65271201909145648566923460348610454326648213393607260249141273724587006606315588"
82     "17488152092096282925409171536436789259036001133053054882046652138414695194151160"
83     "94330572703657595919530921861173819326117931051185480744623799627495673518857527"
84     "24891227938183011949129833673362440656643086021394946395224737190702179860943702"
85     "77053921717629317675238467481846766940513200056812714526356082778577134275778960"
86     "91736371787214684409012249534301465495853710507922796892589235420199561121290219"
87     "60864034418159813629774771309960518707211349999998372978049951059731732816096318"
88     "59502445945534690830264252230825334468503526193118817101000313783875288658753320"
89     "83814206171776691473035982534904287554687311595628638823537875937519577818577805"
90     "32171226806613001927876611195909216420198938095257201065485863278865936153381827"
91     "96823030195203530185296899577362259941389124972177528347913151557485724245415069"
92     "59508295331168617278558890750983817546374649393192550604009277016711390098488240"
93     "12858361603563707660104710181942955596198946767837449448255379774726847104047534"
94     "64620804668425906949129331367702898915210475216205696602405803815019351125338243"
95     "00355876402474964732639141992726042699227967823547816360093417216412199245863150"
96     "30286182974555706749838505494588586926995690927210797509302955321165344987202755"
97     "96023648066549911988183479775356636980742654252786255181841757467289097777279380"
98     "00816470600161452491921732172147723501414419735685481613611573525521334757418494"
99     "68438523323907394143334547762416862518983569485562099219222184272550254256887671"
100     "79049460165346680498862723279178608578438382796797668145410095388378636095068006"
101     "42251252051173929848960841284886269456042419652850222106611863067442786220391949"
102     "45047123713786960956364371917287467764657573962413890865832645995813390478027590"
103     "09946576407895126946839835259570982582262052248940772671947826848260147699090264"
104     "01363944374553050682034962524517493996514314298091906592509372216964615157098583"
105     "87410597885959772975498930161753928468138268683868942774155991855925245953959431"
106     "04997252468084598727364469584865383673622262609912460805124388439045124413654976"
107     "27807977156914359977001296160894416948685558484063534220722258284886481584560285"
108     "06016842739452267467678895252138522549954666727823986456596116354886230577456498"
109     "03559363456817432411251507606947945109659609402522887971089314566913686722874894"
110     "05601015033086179286809208747609178249385890097149096759852613655497818931297848"
111     "21682998948722658804857564014270477555132379641451523746234364542858444795265867"
112     "82105114135473573952311342716610213596953623144295248493718711014576540359027993"
113     "44037420073105785390621983874478084784896833214457138687519435064302184531910484"
114     "81005370614680674919278191197939952061419663428754440643745123718192179998391015"
115     "91956181467514269123974894090718649423196";
116 static const char* e =
117     "2.718281828459045235360287471352662497757247093699959574966967627724076630353547"
118     "59457138217852516642742746639193200305992181741359662904357290033429526059563073"
119     "81323286279434907632338298807531952510190115738341879307021540891499348841675092"
120     "44761460668082264800168477411853742345442437107539077744992069551702761838606261"
121     "33138458300075204493382656029760673711320070932870912744374704723069697720931014"
122     "16928368190255151086574637721112523897844250569536967707854499699679468644549059"
123     "87931636889230098793127736178215424999229576351482208269895193668033182528869398"
124     "49646510582093923982948879332036250944311730123819706841614039701983767932068328"
125     "23764648042953118023287825098194558153017567173613320698112509961818815930416903"
126     "51598888519345807273866738589422879228499892086805825749279610484198444363463244"
127     "96848756023362482704197862320900216099023530436994184914631409343173814364054625"
128     "31520961836908887070167683964243781405927145635490613031072085103837505101157477"
129     "04171898610687396965521267154688957035035402123407849819334321068170121005627880"
130     "23519303322474501585390473041995777709350366041699732972508868769664035557071622"
131     "68447162560798826517871341951246652010305921236677194325278675398558944896970964"
132     "09754591856956380236370162112047742722836489613422516445078182442352948636372141"
133     "74023889344124796357437026375529444833799801612549227850925778256209262264832627"
134     "79333865664816277251640191059004916449982893150566047258027786318641551956532442"
135     "58698294695930801915298721172556347546396447910145904090586298496791287406870504"
136     "89585867174798546677575732056812884592054133405392200011378630094556068816674001"
137     "69842055804033637953764520304024322566135278369511778838638744396625322498506549"
138     "95886234281899707733276171783928034946501434558897071942586398772754710962953741"
139     "52111513683506275260232648472870392076431005958411661205452970302364725492966693"
140     "81151373227536450988890313602057248176585118063036442812314965507047510254465011"
141     "72721155519486685080036853228183152196003735625279449515828418829478761085263981"
142     "39559900673764829224437528718462457803619298197139914756448826260390338144182326"
143     "25150974827987779964373089970388867782271383605772978824125611907176639465070633"
144     "04527954661855096666185664709711344474016070462621568071748187784437143698821855"
145     "96709591025968620023537185887485696522000503117343920732113908032936344797273559"
146     "55277349071783793421637012050054513263835440001863239914907054797780566978533580"
147     "48966906295119432473099587655236812859041383241160722602998330535370876138939639"
148     "17795745401613722361878936526053815584158718692553860616477983402543512843961294"
149     "60352913325942794904337299085731580290958631382683291477116396337092400316894586"
150     "36060645845925126994655724839186564209752685082307544254599376917041977780085362"
151     "73094171016343490769642372229435236612557250881477922315197477806056967253801718"
152     "07763603462459278778465850656050780844211529697521890874019660906651803516501792"
153     "50461950136658543663271254963990854914420001457476081930221206602433009641270489"
154     "43903971771951806990869986066365832322787";
155
156 template <class T>
157 inline bool is_mpfr(const T&)
158 {
159    return false;
160 }
161 #if defined(TEST_MPFR_50)
162 template <unsigned N, boost::multiprecision::expression_template_option ET>
163 inline bool is_mpfr(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<N>, ET>&)
164 {
165    return true;
166 }
167 #endif
168
169 template <class T>
170 void test()
171 {
172    typedef typename T::backend_type backend_type;
173    T                                num, expect;
174    num.backend() = boost::multiprecision::default_ops::get_constant_pi<backend_type>();
175    expect        = static_cast<T>(pi);
176    BOOST_CHECK_CLOSE_FRACTION(num, expect, std::numeric_limits<T>::epsilon() * (is_mpfr(num) ? 1200 : 2));
177    num.backend() = boost::multiprecision::default_ops::get_constant_ln2<backend_type>();
178    expect        = static_cast<T>(ln2);
179    BOOST_CHECK_CLOSE_FRACTION(num, expect, std::numeric_limits<T>::epsilon() * (is_mpfr(num) ? 30 : 1));
180    num.backend() = boost::multiprecision::default_ops::get_constant_e<backend_type>();
181    expect        = static_cast<T>(e);
182    BOOST_CHECK_CLOSE_FRACTION(num, expect, std::numeric_limits<T>::epsilon() * (is_mpfr(num) ? 2 : 1));
183 }
184
185 int main()
186 {
187 #ifdef TEST_MPFR_50
188    test<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<2000> > >();
189 #endif
190 #ifdef TEST_CPP_DEC_FLOAT
191    test<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<2000> > >();
192 #endif
193 #ifdef TEST_MPF_50
194    test<boost::multiprecision::number<boost::multiprecision::gmp_float<2000> > >();
195 #endif
196    return boost::report_errors();
197 }