Ticket #8798: boost_multiprecision_power_demo.cpp

File boost_multiprecision_power_demo.cpp, 2.7 KB (added by Jan Bouwer <JBouwer@…>, 9 years ago)

Demonstration of the error

Line 
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
13const double errorMargin = 0.00001;
14
15template<class T>
16void 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
25template<class T>
26void 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
36void test_boost_mp()
37{
38 using namespace boost::multiprecision;
39
40using T = cpp_dec_float_50;
41assert( pow(T(0), T(2)) == 0 ); // Failure
42 test_power_int<checked_cpp_int>(0, 0);
43 test_power_int<checked_cpp_int>(0, 1);
44 test_power_int<checked_cpp_int>(0, 2);
45 test_power_int<checked_cpp_int>(1, 0);
46 test_power_int<checked_cpp_int>(1, 1);
47 test_power_int<checked_cpp_int>(1, 2);
48 test_power_int<checked_cpp_int>(2, 0);
49 test_power_int<checked_cpp_int>(2, 1);
50 test_power_int<checked_cpp_int>(2, 2);
51 test_power_float<cpp_dec_float_50>(0, 0);
52 test_power_float<cpp_dec_float_50>(0, 1);
53 test_power_float<cpp_dec_float_50>(0, 2);
54 test_power_float<cpp_dec_float_50>(1, 0);
55 test_power_float<cpp_dec_float_50>(1, 1);
56 test_power_float<cpp_dec_float_50>(1, 2);
57 test_power_float<cpp_dec_float_50>(2, 0);
58 test_power_float<cpp_dec_float_50>(2, 1);
59 test_power_float<cpp_dec_float_50>(2, 2);
60 test_power_float<cpp_dec_float_100>(0, 0);
61 test_power_float<cpp_dec_float_100>(0, 1);
62 test_power_float<cpp_dec_float_100>(0, 2);
63 test_power_float<cpp_dec_float_100>(1, 0);
64 test_power_float<cpp_dec_float_100>(1, 1);
65 test_power_float<cpp_dec_float_100>(1, 2);
66 test_power_float<cpp_dec_float_100>(2, 0);
67 test_power_float<cpp_dec_float_100>(2, 1);
68 test_power_float<cpp_dec_float_100>(2, 2);
69 test_power_float<number<cpp_dec_float<0, long long>>>(0, 0);
70 test_power_float<number<cpp_dec_float<0, long long>>>(0, 1);
71 test_power_float<number<cpp_dec_float<0, long long>>>(0, 2);
72 test_power_float<number<cpp_dec_float<0, long long>>>(1, 0);
73 test_power_float<number<cpp_dec_float<0, long long>>>(1, 1);
74 test_power_float<number<cpp_dec_float<0, long long>>>(1, 2);
75 test_power_float<number<cpp_dec_float<0, long long>>>(2, 0);
76 test_power_float<number<cpp_dec_float<0, long long>>>(2, 1);
77 test_power_float<number<cpp_dec_float<0, long long>>>(2, 2);
78}