1 /////////////////////////////////////////////////////////////// 2 // Copyright 2013 John Maddock. Distributed under the Boost 3 // Software License, Version 1.0. (See accompanying file 4 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt 5 6 // Demonstrations of using Boost.Multiprecision float128 quad type. 7 // (Only available using GCC compiler). 8 9 // Contains Quickbook markup in comments. 10 11 //[float128_eg 12 #include <boost/multiprecision/float128.hpp> 13 #include <boost/math/special_functions/gamma.hpp> 14 #include <iostream> 15 main()16int main() 17 { 18 using namespace boost::multiprecision; // Potential to cause name collisions? 19 // using boost::multiprecision::float128; // is safer. 20 21 /*`The type float128 provides operations at 128-bit precision with 22 [@https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format#IEEE_754_quadruple-precision_binary_floating-point_format:_binary128 Quadruple-precision floating-point format] 23 and have full `std::numeric_limits` support: 24 */ 25 float128 b = 2; 26 //` There are 15 bits of (biased) binary exponent and 113-bits of significand precision 27 std::cout << std::numeric_limits<float128>::digits << std::endl; 28 //` or 33 decimal places: 29 std::cout << std::numeric_limits<float128>::digits10 << std::endl; 30 //` We can use any C++ std library function, so let's show all the at-most 36 potentially significant digits, and any trailing zeros, as well: 31 std::cout.setf(std::ios_base::showpoint); // Include any trailing zeros. 32 std::cout << std::setprecision(std::numeric_limits<float128>::max_digits10) 33 << log(b) << std::endl; // Shows log(2) = 0.693147180559945309417232121458176575 34 //` We can also use any function from Boost.Math, for example, the 'true gamma' function `tgamma`: 35 std::cout << boost::math::tgamma(b) << std::endl; 36 /*` And since we have an extended exponent range, we can generate some really large 37 numbers here (4.02387260077093773543702433923004111e+2564): 38 */ 39 std::cout << boost::math::tgamma(float128(1000)) << std::endl; 40 /*` We can declare constants using GCC or Intel's native types, and literals with the Q suffix, and these can be declared `constexpr` if required: 41 */ 42 /*<-*/ 43 #ifndef BOOST_NO_CXX11_CONSTEXPR 44 /*->*/ 45 // std::numeric_limits<float128>::max_digits10 = 36 46 constexpr float128 pi = 3.14159265358979323846264338327950288Q; 47 std::cout.precision(std::numeric_limits<float128>::max_digits10); 48 std::cout << "pi = " << pi << std::endl; //pi = 3.14159265358979323846264338327950280 49 /*<-*/ 50 #endif 51 /*->*/ 52 //] [/float128_eg] 53 return 0; 54 } 55 56 /* 57 //[float128_numeric_limits 58 59 GCC 8.1.0 60 61 Type name is float128_t: 62 Type is g 63 std::is_fundamental<> = true 64 std::is_signed<> = true 65 std::is_unsigned<> = false 66 std::is_integral<> = false 67 std::is_arithmetic<> = true 68 std::is_const<> = false 69 std::is_trivial<> = true 70 std::is_standard_layout<> = true 71 std::is_pod<> = true 72 std::numeric_limits::<>is_exact = false 73 std::numeric_limits::<>is bounded = true 74 std::numeric_limits::<>is_modulo = false 75 std::numeric_limits::<>is_iec559 = true 76 std::numeric_limits::<>traps = false 77 std::numeric_limits::<>tinyness_before = false 78 std::numeric_limits::<>max() = 1.18973149535723176508575932662800702e+4932 79 std::numeric_limits::<>min() = 3.36210314311209350626267781732175260e-4932 80 std::numeric_limits::<>lowest() = -1.18973149535723176508575932662800702e+4932 81 std::numeric_limits::<>min_exponent = -16381 82 std::numeric_limits::<>max_exponent = 16384 83 std::numeric_limits::<>epsilon() = 1.92592994438723585305597794258492732e-34 84 std::numeric_limits::<>radix = 2 85 std::numeric_limits::<>digits = 113 86 std::numeric_limits::<>digits10 = 33 87 std::numeric_limits::<>max_digits10 = 36 88 std::numeric_limits::<>has denorm = true 89 std::numeric_limits::<>denorm min = 6.47517511943802511092443895822764655e-4966 90 std::denorm_loss = false 91 limits::has_signaling_NaN == false 92 std::numeric_limits::<>quiet_NaN = nan 93 std::numeric_limits::<>infinity = inf 94 95 //] [/float128_numeric_limits] 96 */ 97 98 99