| 1 | //
|
|---|
| 2 | // boost_multiprecision_power_demo.cpp
|
|---|
| 3 | //
|
|---|
| 4 | // Created by Jan Bouwer on 8/7/13.
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 | #include <cassert>
|
|---|
| 8 | #include <cmath>
|
|---|
| 9 | #include <iostream>
|
|---|
| 10 | #include <boost/multiprecision/cpp_dec_float.hpp>
|
|---|
| 11 | #include <boost/multiprecision/cpp_int.hpp>
|
|---|
| 12 |
|
|---|
| 13 | const double errorMargin = 0.00001;
|
|---|
| 14 |
|
|---|
| 15 | template<class T>
|
|---|
| 16 | void test_power_int(double b, int e)
|
|---|
| 17 | {
|
|---|
| 18 | const T xb(b), xe(e);
|
|---|
| 19 |
|
|---|
| 20 | using namespace std;
|
|---|
| 21 | cout << "pow(" << b << ", " << e << ") = " << pow(xb, e)
|
|---|
| 22 | << (double(pow(xb, e)) - pow(b, e) < errorMargin ? "" : " - Unexpected!") << endl;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | template<class T>
|
|---|
| 26 | void test_power_float(double b, double e)
|
|---|
| 27 | {
|
|---|
| 28 | const T xb(b), xe(e);
|
|---|
| 29 |
|
|---|
| 30 | pow(xb, xe);
|
|---|
| 31 | using namespace std;
|
|---|
| 32 | cout << "pow(" << b << ", " << e << ") = " << pow(xb, xe)
|
|---|
| 33 | << (double(pow(xb, xe)) - pow(b, e) < errorMargin ? "" : " - Unexpected!") << endl;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | void test_boost_mp()
|
|---|
| 37 | {
|
|---|
| 38 | using namespace boost::multiprecision;
|
|---|
| 39 |
|
|---|
| 40 | test_power_int<checked_cpp_int>(0, 0);
|
|---|
| 41 | test_power_int<checked_cpp_int>(0, 1);
|
|---|
| 42 | test_power_int<checked_cpp_int>(0, 2);
|
|---|
| 43 | test_power_int<checked_cpp_int>(1, 0);
|
|---|
| 44 | test_power_int<checked_cpp_int>(1, 1);
|
|---|
| 45 | test_power_int<checked_cpp_int>(1, 2);
|
|---|
| 46 | test_power_int<checked_cpp_int>(2, 0);
|
|---|
| 47 | test_power_int<checked_cpp_int>(2, 1);
|
|---|
| 48 | test_power_int<checked_cpp_int>(2, 2);
|
|---|
| 49 | test_power_float<cpp_dec_float_50>(0, 0);
|
|---|
| 50 | test_power_float<cpp_dec_float_50>(0, 1);
|
|---|
| 51 | test_power_float<cpp_dec_float_50>(0, 2);
|
|---|
| 52 | test_power_float<cpp_dec_float_50>(1, 0);
|
|---|
| 53 | test_power_float<cpp_dec_float_50>(1, 1);
|
|---|
| 54 | test_power_float<cpp_dec_float_50>(1, 2);
|
|---|
| 55 | test_power_float<cpp_dec_float_50>(2, 0);
|
|---|
| 56 | test_power_float<cpp_dec_float_50>(2, 1);
|
|---|
| 57 | test_power_float<cpp_dec_float_50>(2, 2);
|
|---|
| 58 | test_power_float<cpp_dec_float_100>(0, 0);
|
|---|
| 59 | test_power_float<cpp_dec_float_100>(0, 1);
|
|---|
| 60 | test_power_float<cpp_dec_float_100>(0, 2);
|
|---|
| 61 | test_power_float<cpp_dec_float_100>(1, 0);
|
|---|
| 62 | test_power_float<cpp_dec_float_100>(1, 1);
|
|---|
| 63 | test_power_float<cpp_dec_float_100>(1, 2);
|
|---|
| 64 | test_power_float<cpp_dec_float_100>(2, 0);
|
|---|
| 65 | test_power_float<cpp_dec_float_100>(2, 1);
|
|---|
| 66 | test_power_float<cpp_dec_float_100>(2, 2);
|
|---|
| 67 | test_power_float<number<cpp_dec_float<0, long long>>>(0, 0);
|
|---|
| 68 | test_power_float<number<cpp_dec_float<0, long long>>>(0, 1);
|
|---|
| 69 | test_power_float<number<cpp_dec_float<0, long long>>>(0, 2);
|
|---|
| 70 | test_power_float<number<cpp_dec_float<0, long long>>>(1, 0);
|
|---|
| 71 | test_power_float<number<cpp_dec_float<0, long long>>>(1, 1);
|
|---|
| 72 | test_power_float<number<cpp_dec_float<0, long long>>>(1, 2);
|
|---|
| 73 | test_power_float<number<cpp_dec_float<0, long long>>>(2, 0);
|
|---|
| 74 | test_power_float<number<cpp_dec_float<0, long long>>>(2, 1);
|
|---|
| 75 | test_power_float<number<cpp_dec_float<0, long long>>>(2, 2);
|
|---|
| 76 | }
|
|---|