| 1 | /*
|
|---|
| 2 | Compiled with:
|
|---|
| 3 | > gcc -I<BOOST_HOME>/include -c -g -o test.o test.cpp
|
|---|
| 4 | > gcc -o test test.o -lgmp -lstdc++
|
|---|
| 5 |
|
|---|
| 6 | Output:
|
|---|
| 7 | > ./test
|
|---|
| 8 | Input: 0.840188, 0.394383, 0.783099
|
|---|
| 9 | Output A: 0
|
|---|
| 10 | Input: 0.79844, 0.911647, 0.197551
|
|---|
| 11 | Output B: 1.25764e-58
|
|---|
| 12 | */
|
|---|
| 13 |
|
|---|
| 14 | #include <iostream>
|
|---|
| 15 | #include <cstdlib>
|
|---|
| 16 | #include <boost/multiprecision/gmp.hpp>
|
|---|
| 17 |
|
|---|
| 18 | namespace b_mp = boost::multiprecision;
|
|---|
| 19 | typedef b_mp::number<b_mp::gmp_float<0>, b_mp::et_off> test_float_type;
|
|---|
| 20 |
|
|---|
| 21 | // get random value between 0 and 1
|
|---|
| 22 | void set_rand(test_float_type* v, bool with_division) {
|
|---|
| 23 | for (int i = 0; i < 3; i++) {
|
|---|
| 24 | v[i] = with_division ?
|
|---|
| 25 | test_float_type(double(std::rand()) / double(RAND_MAX)) :
|
|---|
| 26 | test_float_type(std::rand()) / test_float_type(RAND_MAX);
|
|---|
| 27 | }
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | // test distributive property
|
|---|
| 31 | int main() {
|
|---|
| 32 | test_float_type v[3], a1, a2;
|
|---|
| 33 |
|
|---|
| 34 | set_rand(v, true);
|
|---|
| 35 | std::cerr << "Input: " << v[0] << ", " << v[1] << ", " << v[2] << std::endl;
|
|---|
| 36 | a1 = v[0] * v[1] + v[0] * v[2];
|
|---|
| 37 | a2 = v[0] * (v[1] + v[2]);
|
|---|
| 38 | std::cerr << "Output A: " << a1 - a2 << std::endl;
|
|---|
| 39 |
|
|---|
| 40 | set_rand(v, false);
|
|---|
| 41 | std::cerr << "Input: " << v[0] << ", " << v[1] << ", " << v[2] << std::endl;
|
|---|
| 42 | a1 = v[0] * v[1] + v[0] * v[2];
|
|---|
| 43 | a2 = v[0] * (v[1] + v[2]);
|
|---|
| 44 | std::cerr << "Output B: " << a1 - a2 << std::endl;
|
|---|
| 45 |
|
|---|
| 46 | return 0;
|
|---|
| 47 | }
|
|---|