int main(int argc, char*[]) { #ifdef BOOST_FLOAT32_C if(argc > 1000) instantiate(BOOST_FLOAT32_C(1.23)); #endif }
int main() { std::cout.setf(std::ios::showpoint); // show all significant trailing zeros. long double p = 1.L; //std::cout.precision(std::numeric_limits<long double>::digits10); std::cout << "pi = " << p << std::endl; //[cstdfloat_example_3 //`Ensure that all possibly significant digits (17) including trailing zeros are shown. std::cout.precision(std::numeric_limits<boost::float64_t>::max_digits10); std::cout.setf(std::ios::showpoint); // Show trailing zeros. try { // Always use try'n'catch blocks to ensure any error messages are displayed. // Evaluate and display an evaluation of the Jahnke-Emden lambda function: boost::float64_t v = 1.; boost::float64_t x = 1.; std::cout << jahnke_emden_lambda(v, x) << std::endl; // 0.88010117148986700 //] [/cstdfloat_example_3] // We can show some evaluations with various precisions: { // float64_t for (int i = 0; i < 10; i++) { std::cout << std::setprecision(2) << boost::float64_t(i) << ' ' << std::setprecision(std::numeric_limits<boost::float64_t>::max_digits10) << jahnke_emden_lambda(boost::float64_t(i), v) << std::endl; // } } { // floatmax_t = the maximum available on this platform. for (int i = 0; i < 10; i++) { std::cout << std::setprecision(2) << boost::floatmax_t(i) << ' ' << std::setprecision(std::numeric_limits<boost::floatmax_t>::max_digits10) << jahnke_emden_lambda(boost::floatmax_t(i), v) << std::endl; // } } // Show the precision of long double (this might be 64, 80 or 128 bits). std::cout << "Floating-point type long double is available with:" << std::endl; std::cout << " std::numeric_limits<long double>::digits10 == " << std::numeric_limits<long double>::digits10 << std::endl; // 18 std::cout << " std::numeric_limits<long double>::max_digits10 == " << std::numeric_limits<long double>::max_digits10 << std::endl; // 21 long double p = boost::math::constants::pi<double>(); std::cout.precision(std::numeric_limits<long double>::digits10); std::cout << "pi = " << p << std::endl; //[cstdfloat_constant_2 //`These allow floating-point [*constants of at least the specified width] to be declared: // Declare Archimedes' constant using float32_t with approximately 7 decimal digits of precision. static const boost::float32_t pi = BOOST_FLOAT32_C(3.1415926536); // Declare the Euler-gamma constant with approximately 15 decimal digits of precision. static const boost::float64_t euler = BOOST_FLOAT64_C(0.57721566490153286060651209008240243104216); // Declare the Golden Ratio constant with the maximum decimal digits of precision that the platform supports. static const boost::floatmax_t golden_ratio = BOOST_FLOATMAX_C(1.61803398874989484820458683436563811772); //] [/cstdfloat_constant_2] // http://www.boost.org/doc/libs/1_55_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/float128.html //[cstdfloat_constant_1 // Display the constant pi to the maximum available precision. boost::floatmax_t pi_max = boost::math::constants::pi<boost::floatmax_t>(); std::cout.precision(std::numeric_limits<boost::floatmax_t>::digits10); std::cout << "Most precise pi = " << pi_max << std::endl; // If floatmax_t is float_128_t, then // Most precise pi = 3.141592653589793238462643383279503 //] [/cstdfloat_constant_1] // Test all the floating-point precisions in turn, and if they are available // then display how many decimal digits of precision. #ifdef BOOST_FLOAT16_C std::cout << "Floating-point type boost::float16_t is available." << std::endl; #else std::cout << "Floating-point type boost::float16_t is NOT available." << std::endl; #endif #ifdef BOOST_FLOAT32_C std::cout << "Floating-point type boost::float32_t is available." << std::endl; std::cout << " std::numeric_limits<boost::float32_t>::digits10 == " << std::numeric_limits<boost::float32_t>::digits10 << std::endl; std::cout << " std::numeric_limits<boost::float32_t>::max_digits10 == " << std::numeric_limits<boost::float32_t>::max_digits10 << std::endl; #else std::cout << "Floating-point type boost::float32_t is NOT available." << std::endl; #endif #ifdef BOOST_FLOAT64_C std::cout << "Floating-point type boost::float64_t is available." << std::endl; std::cout << " std::numeric_limits<boost::float64_t>::digits10 == " << std::numeric_limits<boost::float64_t>::digits10 << std::endl; std::cout << " std::numeric_limits<boost::float64_t>::max_digits10 == " << std::numeric_limits<boost::float64_t>::max_digits10 << std::endl; #else std::cout << "Floating-point type boost::float64_t is NOT available." << std::endl; #endif #ifdef BOOST_FLOAT80_C std::cout << "Floating-point type boost::float80_t is available." << std::endl; std::cout << " std::numeric_limits<boost::float80_t>::digits10 == " << std::numeric_limits<boost::float80_t>::digits10 << std::endl; std::cout << " std::numeric_limits<boost::float80_t>::max_digits10 == " << std::numeric_limits<boost::float80_t>::max_digits10 << std::endl; #else std::cout << "Floating-point type boost::float80_t is NOT available." << std::endl; #endif #ifdef BOOST_FLOAT128_C std::cout << "Floating-point type boost::float128_t is available." << std::endl; std::cout << " std::numeric_limits<boost::float128_t>::digits10 == " << std::numeric_limits<boost::float128_t>::digits10 << std::endl; std::cout << " std::numeric_limits<boost::float128_t>::max_digits10 == " << std::numeric_limits<boost::float128_t>::max_digits10 << std::endl; #else std::cout << "Floating-point type boost::float128_t is NOT available." << std::endl; #endif // Show some constants at a precision depending on the available type(s). #ifdef BOOST_FLOAT16_C std::cout.precision(boost::max_digits10<boost::float16_t>()); // Show all significant decimal digits, std::cout.setf(std::ios::showpoint); // including all significant trailing zeros. std::cout << "BOOST_FLOAT16_C(123.456789012345678901234567890) = " << BOOST_FLOAT16_C(123.456789012345678901234567890) << std::endl; // BOOST_FLOAT16_C(123.456789012345678901234567890) = 123.45678901234568 #endif //[floatmax_widths_1 #ifdef BOOST_FLOAT32_C std::cout.precision(boost::max_digits10<boost::float32_t>()); // Show all significant decimal digits, std::cout.setf(std::ios::showpoint); // including all significant trailing zeros. std::cout << "BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) = " << BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) << std::endl; // BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) = 123.456787 #endif //] [/floatmax_widths_1] #ifdef BOOST_FLOAT64_C std::cout.precision(boost::max_digits10<boost::float64_t>()); // Show all significant decimal digits, std::cout.setf(std::ios::showpoint); // including all significant trailing zeros. std::cout << "BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) = " << BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) << std::endl; // BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) = 123.45678901234568 #endif #ifdef BOOST_FLOAT80_C std::cout.precision(boost::max_digits10<boost::float80_t>()); // Show all significant decimal digits, std::cout.setf(std::ios::showpoint); // including all significant trailing zeros. std::cout << "BOOST_FLOAT80_C(123.4567890123456789012345678901234567890) = " << BOOST_FLOAT80_C(123.4567890123456789012345678901234567890) << std::endl; // BOOST_FLOAT80_C(123.4567890123456789012345678901234567890) = 123.456789012345678903 #endif #ifdef BOOST_FLOAT128_C std::cout.precision(boost::max_digits10<boost::float128_t>()); // Show all significant decimal digits, std::cout.setf(std::ios::showpoint); // including all significant trailing zeros. std::cout << "BOOST_FLOAT128_C(123.4567890123456789012345678901234567890) = " << BOOST_FLOAT128_C(123.4567890123456789012345678901234567890) << std::endl; // BOOST_FLOAT128_C(123.4567890123456789012345678901234567890) = 123.456789012345678901234567890123453 #endif /* //[floatmax_widths_2 BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) = 123.456787 BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) = 123.45678901234568 BOOST_FLOAT80_C(123.4567890123456789012345678901234567890) = 123.456789012345678903 BOOST_FLOAT128_C(123.4567890123456789012345678901234567890) = 123.456789012345678901234567890123453 //] [/floatmax_widths_2] */ // Display the precisions available for floatmax_t #ifdef BOOST_FLOATMAX_C BOOST_ASSERT(std::numeric_limits<boost::floatmax_t>::is_specialized == true); BOOST_ASSERT(std::numeric_limits<boost::floatmax_t>::is_iec559 == true); BOOST_ASSERT(BOOST_FLOATMAX_C(0.) == 0); std::cout << "floatmax_t " << std::numeric_limits<boost::floatmax_t>::digits << " bits\n" // 113 << std::numeric_limits<boost::floatmax_t>::digits10 << " decimal digits\n" // 34 << std::numeric_limits<boost::floatmax_t>::max_digits10 << " max_digits\n" // 36 << std::numeric_limits<boost::floatmax_t>::radix << " radix\n" << std::endl; int significand_bits = std::numeric_limits<boost::floatmax_t>::digits; int exponent_max = std::numeric_limits<boost::floatmax_t>::max_exponent; int exponent_min = std::numeric_limits<boost::floatmax_t>::min_exponent; int exponent_bits = 1 + static_cast<int>(std::log2(std::numeric_limits<boost::floatmax_t>::max_exponent)); int sign_bits = std::numeric_limits<boost::floatmax_t>::is_signed; std::cout << "significand_bits (including one implicit bit)" << significand_bits << ", exponent_bits " << exponent_bits << ", sign_bits " << sign_bits << std::endl; // One can compute the total number of bits in the floatmax_t, // but probably not at compile time. std::cout << "bits = " << significand_bits + exponent_bits + sign_bits -1 << std::endl; // -1 to take account of the implicit bit that is not part of the physical layout. // One can compare typedefs (but, of course, only those that are defined for the platform in use.) std::cout.setf(std::ios::boolalpha); std::cout << "double, double: " << std::is_same<double, double>::value << std::endl; bool b = boost::is_same<boost::floatmax_t, boost::float64_t>::value; std::cout << "boost::is_same<boost::floatmax_t, boost::float64_t>::value; " << b << std::endl; std::cout << "floatmax_t, float64_t: " << std::is_same<boost::floatmax_t, boost::float64_t>::value << std::endl; /*`So the simplest way of obtaining the total number of bits in the floatmax_t is to infer it from the std::numeric_limits<>::digits value. This is possible because the type, must be a IEEE754 layout. */ //[floatmax_1 const int fpbits = (std::numeric_limits<boost::floatmax_t>::digits == 113) ? 128 : (std::numeric_limits<boost::floatmax_t>::digits == 64) ? 80 : (std::numeric_limits<boost::floatmax_t>::digits == 53) ? 64 : (std::numeric_limits<boost::floatmax_t>::digits == 24) ? 32 : (std::numeric_limits<boost::floatmax_t>::digits == 11) ? 16 : 0; // Unknown - not IEEE754 format. std::cout << fpbits << " bits." << std::endl; //] [/floatmax_1] #endif } catch (std::exception ex) { // Display details about why any exceptions are thrown. std::cout << "Thrown exception " << ex.what() << std::endl; } } // int main()