/* Compiled with: > gcc -I/include -c -g -o test.o test.cpp > gcc -o test test.o -lgmp -lstdc++ Output: > ./test Input: 0.840188, 0.394383, 0.783099 Output A: 0 Input: 0.79844, 0.911647, 0.197551 Output B: 1.25764e-58 */ #include #include #include namespace b_mp = boost::multiprecision; typedef b_mp::number, b_mp::et_off> test_float_type; // get random value between 0 and 1 void set_rand(test_float_type* v, bool with_division) { for (int i = 0; i < 3; i++) { v[i] = with_division ? test_float_type(double(std::rand()) / double(RAND_MAX)) : test_float_type(std::rand()) / test_float_type(RAND_MAX); } } // test distributive property int main() { test_float_type v[3], a1, a2; set_rand(v, true); std::cerr << "Input: " << v[0] << ", " << v[1] << ", " << v[2] << std::endl; a1 = v[0] * v[1] + v[0] * v[2]; a2 = v[0] * (v[1] + v[2]); std::cerr << "Output A: " << a1 - a2 << std::endl; set_rand(v, false); std::cerr << "Input: " << v[0] << ", " << v[1] << ", " << v[2] << std::endl; a1 = v[0] * v[1] + v[0] * v[2]; a2 = v[0] * (v[1] + v[2]); std::cerr << "Output B: " << a1 - a2 << std::endl; return 0; }