1 // Copyright John Maddock 2014
2 // Copyright Christopher Kormanyos 2014.
3 // Copyright Paul A. Bristow 2016.
4
5 // Use, modification and distribution are subject to the
6 // Boost Software License, Version 1.0. (See accompanying file
7 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9 // Contains Quickbook snippets as C++ comments - do not remove.
10
11 /*
12 `This example shows use of a specified-width floating-point typedef
13 to evaluate a moderately complex math function using
14 [@http://en.wikipedia.org/wiki/Double-precision_floating-point_format IEEE754 64-bit double-precision].
15 about 15 decimal digits `std::numeric_limits<boost::float64_t>::digits10`,
16 (but never exceeding 17 decimal digits `std::numeric_limits<boost::float64_t>::max_digits10`).
17
18 The Jahnke-Emden lambda function is described at
19
20 Weisstein, Eric W. "Lambda Function." From MathWorld--A Wolfram Web Resource.
21 http://mathworld.wolfram.com/LambdaFunction.html
22
23 E. Jahnke and F. Emden, "Tables of Functions with Formulae and Curves,"
24 Dover, New York, 4th ed., (1945), pages 180-188.
25
26 */
27
28 //[cstdfloat_example_1
29 #include <boost/cstdfloat.hpp> // For float_64_t, float128_t. Must be first include!
30 #include <cmath> // for pow function.
31 #include <boost/math/special_functions.hpp> // For gamma function.
32 //] [/cstdfloat_example_1]
33 #include <boost/type_traits/is_same.hpp>
34
35 #include <iostream>
36
37 /*!
38 Function max_digits10
39 Returns maximum number of possibly significant decimal digits for a floating-point type FPT,
40 even for older compilers/standard libraries that
41 lack support for std::numeric_limits<FPT>::max_digits10,
42 when the Kahan formula 2 + binary_digits * 0.3010 is used instead.
43 Also provides the correct result for Visual Studio 2010
44 (where the value 8 provided for float is wrong).
45 */
46 namespace boost
47 {
48 template <typename FPT>
max_digits10()49 const int max_digits10()
50 {
51 // Since max_digits10 is not defined (or wrong) on older systems, define a local max_digits10.
52 // Usage: int m = max_digits10<boost::float64_t>();
53 const int m =
54 #if (defined BOOST_NO_CXX11_NUMERIC_LIMITS) || (_MSC_VER == 1600) // is wrongly 8 not 9 for VS2010.
55 2 + std::numeric_limits<FPT>::digits * 3010/10000;
56 #else
57 std::numeric_limits<FPT>::max_digits10;
58 #endif
59 return m;
60 }
61 } // namespace boost
62
63 //`Define the Jahnke-Emden_lambda function.
64 //[cstdfloat_example_2
jahnke_emden_lambda(boost::float64_t v,boost::float64_t x)65 boost::float64_t jahnke_emden_lambda(boost::float64_t v, boost::float64_t x)
66 {
67 const boost::float64_t gamma_v_plus_one = boost::math::tgamma(v + 1);
68 const boost::float64_t x_half_pow_v = std::pow(x /2, v);
69
70 return gamma_v_plus_one * boost::math::cyl_bessel_j(x, v) / x_half_pow_v;
71 }
72 //] [/cstdfloat_example_2]
73
main()74 int main()
75 {
76 std::cout.setf(std::ios::showpoint); // show all significant trailing zeros.
77
78 long double p = 1.L;
79 //std::cout.precision(std::numeric_limits<long double>::digits10);
80
81 std::cout << "pi = " << p << std::endl;
82
83 //[cstdfloat_example_3
84 //`Ensure that all possibly significant digits (17) including trailing zeros are shown.
85
86 std::cout.precision(std::numeric_limits<boost::float64_t>::max_digits10);
87 std::cout.setf(std::ios::showpoint); // Show trailing zeros.
88
89 try
90 { // Always use try'n'catch blocks to ensure any error messages are displayed.
91
92 // Evaluate and display an evaluation of the Jahnke-Emden lambda function:
93 boost::float64_t v = 1.;
94 boost::float64_t x = 1.;
95 std::cout << jahnke_emden_lambda(v, x) << std::endl; // 0.88010117148986700
96 //] [/cstdfloat_example_3]
97
98 // We can show some evaluations with various precisions:
99 { // float64_t
100 for (int i = 0; i < 10; i++)
101 {
102 std::cout << std::setprecision(2) << boost::float64_t(i) << ' '
103 << std::setprecision(std::numeric_limits<boost::float64_t>::max_digits10)
104 << jahnke_emden_lambda(boost::float64_t(i), v) << std::endl; //
105 }
106 }
107 { // floatmax_t = the maximum available on this platform.
108 for (int i = 0; i < 10; i++)
109 {
110 std::cout << std::setprecision(2) << boost::floatmax_t(i) << ' '
111 << std::setprecision(std::numeric_limits<boost::floatmax_t>::max_digits10)
112 << jahnke_emden_lambda(boost::floatmax_t(i), v) << std::endl; //
113 }
114 }
115 // Show the precision of long double (this might be 64, 80 or 128 bits).
116 std::cout << "Floating-point type long double is available with:" << std::endl;
117 std::cout << " std::numeric_limits<long double>::digits10 == "
118 << std::numeric_limits<long double>::digits10 << std::endl; // 18
119 std::cout << " std::numeric_limits<long double>::max_digits10 == "
120 << std::numeric_limits<long double>::max_digits10 << std::endl; // 21
121 long double p = boost::math::constants::pi<double>();
122 std::cout.precision(std::numeric_limits<long double>::digits10);
123 std::cout << "pi = " << p << std::endl;
124
125 //[cstdfloat_constant_2
126 //`These allow floating-point [*constants of at least the specified width] to be declared:
127
128 // Declare Archimedes' constant using float32_t with approximately 7 decimal digits of precision.
129 static const boost::float32_t pi = BOOST_FLOAT32_C(3.1415926536);
130
131 // Declare the Euler-gamma constant with approximately 15 decimal digits of precision.
132 static const boost::float64_t euler =
133 BOOST_FLOAT64_C(0.57721566490153286060651209008240243104216);
134
135 // Declare the Golden Ratio constant with the maximum decimal digits of precision that the platform supports.
136 static const boost::floatmax_t golden_ratio =
137 BOOST_FLOATMAX_C(1.61803398874989484820458683436563811772);
138 //] [/cstdfloat_constant_2]
139
140 // http://www.boost.org/doc/libs/1_55_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/float128.html
141 //[cstdfloat_constant_1
142 // Display the constant pi to the maximum available precision.
143 boost::floatmax_t pi_max = boost::math::constants::pi<boost::floatmax_t>();
144 std::cout.precision(std::numeric_limits<boost::floatmax_t>::digits10);
145 std::cout << "Most precise pi = " << pi_max << std::endl;
146 // If floatmax_t is float_128_t, then
147 // Most precise pi = 3.141592653589793238462643383279503
148 //] [/cstdfloat_constant_1]
149
150 // Test all the floating-point precisions in turn, and if they are available
151 // then display how many decimal digits of precision.
152 #ifdef BOOST_FLOAT16_C
153 std::cout << "Floating-point type boost::float16_t is available." << std::endl;
154 #else
155 std::cout << "Floating-point type boost::float16_t is NOT available." << std::endl;
156 #endif
157
158 #ifdef BOOST_FLOAT32_C
159 std::cout << "Floating-point type boost::float32_t is available." << std::endl;
160 std::cout << " std::numeric_limits<boost::float32_t>::digits10 == "
161 << std::numeric_limits<boost::float32_t>::digits10 << std::endl;
162 std::cout << " std::numeric_limits<boost::float32_t>::max_digits10 == "
163 << std::numeric_limits<boost::float32_t>::max_digits10 << std::endl;
164 #else
165 std::cout << "Floating-point type boost::float32_t is NOT available." << std::endl;
166 #endif
167
168 #ifdef BOOST_FLOAT64_C
169 std::cout << "Floating-point type boost::float64_t is available." << std::endl;
170 std::cout << " std::numeric_limits<boost::float64_t>::digits10 == "
171 << std::numeric_limits<boost::float64_t>::digits10 << std::endl;
172 std::cout << " std::numeric_limits<boost::float64_t>::max_digits10 == "
173 << std::numeric_limits<boost::float64_t>::max_digits10 << std::endl;
174 #else
175 std::cout << "Floating-point type boost::float64_t is NOT available." << std::endl;
176 #endif
177
178 #ifdef BOOST_FLOAT80_C
179 std::cout << "Floating-point type boost::float80_t is available." << std::endl;
180 std::cout << " std::numeric_limits<boost::float80_t>::digits10 == "
181 << std::numeric_limits<boost::float80_t>::digits10 << std::endl;
182 std::cout << " std::numeric_limits<boost::float80_t>::max_digits10 == "
183 << std::numeric_limits<boost::float80_t>::max_digits10 << std::endl;
184 #else
185 std::cout << "Floating-point type boost::float80_t is NOT available." << std::endl;
186 #endif
187
188 #ifdef BOOST_FLOAT128_C
189 std::cout << "Floating-point type boost::float128_t is available." << std::endl;
190 std::cout << " std::numeric_limits<boost::float128_t>::digits10 == "
191 << std::numeric_limits<boost::float128_t>::digits10 << std::endl;
192 std::cout << " std::numeric_limits<boost::float128_t>::max_digits10 == "
193 << std::numeric_limits<boost::float128_t>::max_digits10 << std::endl;
194 #else
195 std::cout << "Floating-point type boost::float128_t is NOT available." << std::endl;
196 #endif
197
198 // Show some constants at a precision depending on the available type(s).
199 #ifdef BOOST_FLOAT16_C
200 std::cout.precision(boost::max_digits10<boost::float16_t>()); // Show all significant decimal digits,
201 std::cout.setf(std::ios::showpoint); // including all significant trailing zeros.
202
203 std::cout << "BOOST_FLOAT16_C(123.456789012345678901234567890) = "
204 << BOOST_FLOAT16_C(123.456789012345678901234567890) << std::endl;
205 // BOOST_FLOAT16_C(123.456789012345678901234567890) = 123.45678901234568
206 #endif
207
208 //[floatmax_widths_1
209 #ifdef BOOST_FLOAT32_C
210 std::cout.precision(boost::max_digits10<boost::float32_t>()); // Show all significant decimal digits,
211 std::cout.setf(std::ios::showpoint); // including all significant trailing zeros.
212 std::cout << "BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) = "
213 << BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) << std::endl;
214 // BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) = 123.456787
215 #endif
216 //] [/floatmax_widths_1]
217
218 #ifdef BOOST_FLOAT64_C
219 std::cout.precision(boost::max_digits10<boost::float64_t>()); // Show all significant decimal digits,
220 std::cout.setf(std::ios::showpoint); // including all significant trailing zeros.
221 std::cout << "BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) = "
222 << BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) << std::endl;
223 // BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) = 123.45678901234568
224 #endif
225
226 #ifdef BOOST_FLOAT80_C
227 std::cout.precision(boost::max_digits10<boost::float80_t>()); // Show all significant decimal digits,
228 std::cout.setf(std::ios::showpoint); // including all significant trailing zeros.
229 std::cout << "BOOST_FLOAT80_C(123.4567890123456789012345678901234567890) = "
230 << BOOST_FLOAT80_C(123.4567890123456789012345678901234567890) << std::endl;
231 // BOOST_FLOAT80_C(123.4567890123456789012345678901234567890) = 123.456789012345678903
232 #endif
233
234 #ifdef BOOST_FLOAT128_C
235 std::cout.precision(boost::max_digits10<boost::float128_t>()); // Show all significant decimal digits,
236 std::cout.setf(std::ios::showpoint); // including all significant trailing zeros.
237 std::cout << "BOOST_FLOAT128_C(123.4567890123456789012345678901234567890) = "
238 << BOOST_FLOAT128_C(123.4567890123456789012345678901234567890) << std::endl;
239 // BOOST_FLOAT128_C(123.4567890123456789012345678901234567890) = 123.456789012345678901234567890123453
240 #endif
241
242 /*
243 //[floatmax_widths_2
244 BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) = 123.456787
245 BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) = 123.45678901234568
246 BOOST_FLOAT80_C(123.4567890123456789012345678901234567890) = 123.456789012345678903
247 BOOST_FLOAT128_C(123.4567890123456789012345678901234567890) = 123.456789012345678901234567890123453
248 //] [/floatmax_widths_2]
249 */
250
251 // Display the precisions available for floatmax_t
252
253 #ifdef BOOST_FLOATMAX_C
254 BOOST_ASSERT(std::numeric_limits<boost::floatmax_t>::is_specialized == true);
255 BOOST_ASSERT(std::numeric_limits<boost::floatmax_t>::is_iec559 == true);
256 BOOST_ASSERT(BOOST_FLOATMAX_C(0.) == 0);
257
258 std::cout << "floatmax_t " << std::numeric_limits<boost::floatmax_t>::digits << " bits\n" // 113
259 << std::numeric_limits<boost::floatmax_t>::digits10 << " decimal digits\n" // 34
260 << std::numeric_limits<boost::floatmax_t>::max_digits10 << " max_digits\n" // 36
261 << std::numeric_limits<boost::floatmax_t>::radix << " radix\n"
262 << std::endl;
263
264 int significand_bits = std::numeric_limits<boost::floatmax_t>::digits;
265 int exponent_max = std::numeric_limits<boost::floatmax_t>::max_exponent;
266 int exponent_min = std::numeric_limits<boost::floatmax_t>::min_exponent;
267 int exponent_bits = 1 + static_cast<int>(std::log2(std::numeric_limits<boost::floatmax_t>::max_exponent));
268 int sign_bits = std::numeric_limits<boost::floatmax_t>::is_signed;
269
270 std::cout << "significand_bits (including one implicit bit)" << significand_bits
271 << ", exponent_bits " << exponent_bits
272 << ", sign_bits " << sign_bits << std::endl;
273
274 // One can compute the total number of bits in the floatmax_t,
275 // but probably not at compile time.
276
277 std::cout << "bits = " << significand_bits + exponent_bits + sign_bits -1 << std::endl;
278 // -1 to take account of the implicit bit that is not part of the physical layout.
279
280 // One can compare typedefs (but, of course, only those that are defined for the platform in use.)
281 std::cout.setf(std::ios::boolalpha);
282 std::cout << "double, double: " << std::is_same<double, double>::value << std::endl;
283 bool b = boost::is_same<boost::floatmax_t, boost::float64_t>::value;
284 std::cout << "boost::is_same<boost::floatmax_t, boost::float64_t>::value; " << b << std::endl;
285 std::cout << "floatmax_t, float64_t: "
286 << std::is_same<boost::floatmax_t, boost::float64_t>::value << std::endl;
287
288 /*`So the simplest way of obtaining the total number of bits in the floatmax_t
289 is to infer it from the std::numeric_limits<>::digits value.
290 This is possible because the type, must be a IEEE754 layout. */
291 //[floatmax_1
292 const int fpbits =
293 (std::numeric_limits<boost::floatmax_t>::digits == 113) ? 128 :
294 (std::numeric_limits<boost::floatmax_t>::digits == 64) ? 80 :
295 (std::numeric_limits<boost::floatmax_t>::digits == 53) ? 64 :
296 (std::numeric_limits<boost::floatmax_t>::digits == 24) ? 32 :
297 (std::numeric_limits<boost::floatmax_t>::digits == 11) ? 16 :
298 0; // Unknown - not IEEE754 format.
299 std::cout << fpbits << " bits." << std::endl;
300 //] [/floatmax_1]
301 #endif
302
303 }
304 catch (std::exception ex)
305 { // Display details about why any exceptions are thrown.
306 std::cout << "Thrown exception " << ex.what() << std::endl;
307 }
308
309 } // int main()
310
311 /*
312 [cstdfloat_output
313
314 GCC 4.8.1 with quadmath
315
316 pi = 1.00000
317 0.88010117148986700
318 0.0 0.0000000000000000
319 1.0 0.88010117148986700
320 2.0 4.6137984620549872
321 3.0 16.274830009244951
322 4.0 -25.360637961042869
323 5.0 -1257.9038883512264
324 6.0 -12749.592182518225
325 7.0 -3020.9830849309437
326 8.0 2421897.6013183584
327 9.0 45577595.449204877
328 0.0 0.00000000000000000000000000000000000
329 1.0 0.880101171489866995756301548681221902
330 2.0 4.61379846205498722611082484945654869
331 3.0 16.2748300092449511566883302293717861
332 4.0 -25.3606379610428689375112298876047134
333 5.0 -1257.90388835122644195507746189832687
334 6.0 -12749.5921825182249449426308274269104
335 7.0 -3020.98308493094373261556029319763184
336 8.0 2421897.60131835844367742538452148438
337 9.0 45577595.4492048770189285278320312500
338 Floating-point type long double is available with:
339 std::numeric_limits<long double>::digits10 == 18
340 std::numeric_limits<long double>::max_digits10 == 21
341 pi = 3.14159265358979312
342 Most precise pi = 3.141592653589793238462643383279503
343 Floating-point type boost::float16_t is NOT available.
344 Floating-point type boost::float32_t is available.
345 std::numeric_limits<boost::float32_t>::digits10 == 6
346 std::numeric_limits<boost::float32_t>::max_digits10 == 9
347 Floating-point type boost::float64_t is available.
348 std::numeric_limits<boost::float64_t>::digits10 == 15
349 std::numeric_limits<boost::float64_t>::max_digits10 == 17
350 Floating-point type boost::float80_t is available.
351 std::numeric_limits<boost::float80_t>::digits10 == 18
352 std::numeric_limits<boost::float80_t>::max_digits10 == 21
353 Floating-point type boost::float128_t is available.
354 std::numeric_limits<boost::float128_t>::digits10 == 34
355 std::numeric_limits<boost::float128_t>::max_digits10 == 36
356 BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) = 123.456787
357 BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) = 123.45678901234568
358 BOOST_FLOAT80_C(123.4567890123456789012345678901234567890) = 123.456789012345678903
359 BOOST_FLOAT128_C(123.4567890123456789012345678901234567890) = 123.456789012345678901234567890123453
360 floatmax_t 113 bits
361 34 decimal digits
362 36 max_digits
363 2 radix
364
365 significand_bits (including one implicit bit)113, exponent_bits 15, sign_bits 1
366 bits = 128
367 double, double: true
368 boost::is_same<boost::floatmax_t, boost::float64_t>::value; false
369 floatmax_t, float64_t: false
370 128 bits.
371
372 RUN SUCCESSFUL (total time: 53ms)
373
374 GCC 6.1.1
375
376 pi = 1.00000
377 0.88010117148986700
378 0.0 0.0000000000000000
379 1.0 0.88010117148986700
380 2.0 4.6137984620549872
381 3.0 16.274830009244951
382 4.0 -25.360637961042869
383 5.0 -1257.9038883512264
384 6.0 -12749.592182518225
385 7.0 -3020.9830849309437
386 8.0 2421897.6013183584
387 9.0 45577595.449204877
388 0.0 0.00000000000000000000000000000000000
389 1.0 0.880101171489866995756301548681221902
390 2.0 4.61379846205498722611082484945654869
391 3.0 16.2748300092449511566883302293717861
392 4.0 -25.3606379610428689375112298876047134
393 5.0 -1257.90388835122644195507746189832687
394 6.0 -12749.5921825182249449426308274269104
395 7.0 -3020.98308493094373261556029319763184
396 8.0 2421897.60131835844367742538452148438
397 9.0 45577595.4492048770189285278320312500
398 Floating-point type long double is available with:
399 std::numeric_limits<long double>::digits10 == 18
400 std::numeric_limits<long double>::max_digits10 == 21
401 pi = 3.14159265358979312
402 Most precise pi = 3.14159265358979323846264338327950
403 Floating-point type boost::float16_t is NOT available.
404 Floating-point type boost::float32_t is available.
405 std::numeric_limits<boost::float32_t>::digits10 == 6
406 std::numeric_limits<boost::float32_t>::max_digits10 == 9
407 Floating-point type boost::float64_t is available.
408 std::numeric_limits<boost::float64_t>::digits10 == 15
409 std::numeric_limits<boost::float64_t>::max_digits10 == 17
410 Floating-point type boost::float80_t is available.
411 std::numeric_limits<boost::float80_t>::digits10 == 18
412 std::numeric_limits<boost::float80_t>::max_digits10 == 21
413 Floating-point type boost::float128_t is available.
414 std::numeric_limits<boost::float128_t>::digits10 == 33
415 std::numeric_limits<boost::float128_t>::max_digits10 == 36
416 BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) = 123.456787
417 BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) = 123.45678901234568
418 BOOST_FLOAT80_C(123.4567890123456789012345678901234567890) = 123.456789012345678903
419 BOOST_FLOAT128_C(123.4567890123456789012345678901234567890) = 123.456789012345678901234567890123453
420 floatmax_t 113 bits
421 33 decimal digits
422 36 max_digits
423 2 radix
424
425 significand_bits (including one implicit bit)113, exponent_bits 15, sign_bits 1
426 bits = 128
427 double, double: true
428 boost::is_same<boost::floatmax_t, boost::float64_t>::value; false
429 floatmax_t, float64_t: false
430 128 bits.
431
432
433 MSVC 2013 64-bit
434
435 1> pi = 1.00000
436 1> 0.88010117148986700
437 1> 0.00 0.00000000000000000
438 1> 1.0 0.88010117148986700
439 1> 2.0 4.6137984620549854
440 1> 3.0 16.274830009244948
441 1> 4.0 -25.360637961042869
442 1> 5.0 -1257.9038883512258
443 1> 6.0 -12749.592182518225
444 1> 7.0 -3020.9830849309396
445 1> 8.0 2421897.6013183575
446 1> 9.0 45577595.449204892
447 1> 0.00 0.00000000000000000
448 1> 1.0 0.88010117148986700
449 1> 2.0 4.6137984620549854
450 1> 3.0 16.274830009244948
451 1> 4.0 -25.360637961042869
452 1> 5.0 -1257.9038883512258
453 1> 6.0 -12749.592182518225
454 1> 7.0 -3020.9830849309396
455 1> 8.0 2421897.6013183575
456 1> 9.0 45577595.449204892
457 1> Floating-point type long double is available with:
458 1> std::numeric_limits<long double>::digits10 == 15
459 1> std::numeric_limits<long double>::max_digits10 == 17
460 1> pi = 3.14159265358979
461 1> Most precise pi = 3.14159265358979
462 1> Floating-point type boost::float16_t is NOT available.
463 1> Floating-point type boost::float32_t is available.
464 1> std::numeric_limits<boost::float32_t>::digits10 == 6
465 1> std::numeric_limits<boost::float32_t>::max_digits10 == 9
466 1> Floating-point type boost::float64_t is available.
467 1> std::numeric_limits<boost::float64_t>::digits10 == 15
468 1> std::numeric_limits<boost::float64_t>::max_digits10 == 17
469 1> Floating-point type boost::float80_t is NOT available.
470 1> Floating-point type boost::float128_t is NOT available.
471 1> BOOST_FLOAT32_C(123.4567890123456789012345678901234567890) = 123.456787
472 1> BOOST_FLOAT64_C(123.4567890123456789012345678901234567890) = 123.45678901234568
473 1> floatmax_t 53 bits
474 1> 15 decimal digits
475 1> 17 max_digits
476 1> 2 radix
477 1>
478 1> significand_bits (including one implicit bit)53, exponent_bits 11, sign_bits 1
479 1> bits = 64
480 1> double, double: true
481 1> boost::is_same<boost::floatmax_t, boost::float64_t>::value; true
482 1> floatmax_t, float64_t: true
483 1> 64 bits.
484 ] [/cstdfloat_output]
485
486
487 */
488
489