1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Logging floating point type numbers</title> 5<link rel="stylesheet" href="../../../boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../index.html" title="Boost.Test"> 8<link rel="up" href="../test_tools_support_for_logging.html" title="Tools supports for logging"> 9<link rel="prev" href="contexts.html" title="Contexts"> 10<link rel="next" href="../log_formats.html" title="Log formats"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="contexts.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../test_tools_support_for_logging.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../log_formats.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_test.test_output.test_tools_support_for_logging.log_floating_points"></a><a class="link" href="log_floating_points.html" title="Logging floating point type numbers">Logging 28 floating point type numbers</a> 29</h4></div></div></div> 30<p> 31 It may appear that floating-point numbers are displayed by the <span class="emphasis"><em>Unit 32 Test Framework</em></span> with an excessive number of decimal digits. However 33 the number of digits shown is chosen to avoid apparently nonsensical displays 34 like <code class="computeroutput"><span class="special">[</span><span class="number">1.00000</span> 35 <span class="special">!=</span> <span class="number">1.00000</span><span class="special">]</span></code> when comparing exactly unity against a 36 value which is increased by just one least significant binary digit using 37 the default precision for float of just 6 decimal digits, given by <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="keyword">float</span><span class="special">>::</span><span class="identifier">digits10</span></code>. The function used for the number 38 of decimal digits displayed is that proposed for a future C++ Standard, 39 <a href="http://www2.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf" target="_top">A 40 Proposal to add a max significant decimal digits value</a>, to be called 41 <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">::</span><span class="identifier">max_digits10</span><span class="special">();</span></code>. 42 For 32-bit floats, 9 decimal digits are needed to ensure a single bit change 43 produces a different decimal digit string. 44 </p> 45<p> 46 So a much more helpful display using 9 decimal digits is thus: <code class="computeroutput"><span class="special">[</span><span class="number">1.00000000</span> <span class="special">!=</span> <span class="number">1.00000012</span><span class="special">]</span></code> showing that the two values are in fact 47 different. 48 </p> 49<p> 50 For <span class="bold"><strong>IEEE754</strong></span> 32-bit float values - 9 decimal 51 digits are shown. For 64-bit <span class="bold"><strong>IEEE754</strong></span> double 52 - 17 decimal digits. For <span class="bold"><strong>IEEE754</strong></span> extended 53 long double using 80-bit - 21 decimal digits. For <span class="bold"><strong>IEEE754</strong></span> 54 quadruple long double 128-bit, and SPARC extended long double 128-bit - 55 36 decimal digits. For floating-point types, a convenient formula to calculate 56 <code class="computeroutput"><span class="identifier">max_digits10</span></code> is: <code class="computeroutput"><span class="number">2</span> <span class="special">+</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">FPT</span><span class="special">>::</span><span class="identifier">digits</span> 57 <span class="special">*</span> <span class="number">3010</span><span class="special">/</span><span class="number">10000</span></code>; 58 </p> 59<div class="note"><table border="0" summary="Note"> 60<tr> 61<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td> 62<th align="left">Note</th> 63</tr> 64<tr><td align="left" valign="top"><p> 65 Note that a user defined floating point type UDFPT must define <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">UDFPT</span><span class="special">>::</span><span class="identifier">is_specialized</span> <span class="special">=</span> 66 <span class="keyword">true</span></code> and provide an appropriate 67 value for <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">UDFPT</span><span class="special">>::</span><span class="identifier">digits</span></code>, the number of bits used for 68 the significand or mantissa. For example, for the SPARC extended long 69 double 128, 113 bits are used for the significand (one of which is implicit). 70 </p></td></tr> 71</table></div> 72</div> 73<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 74<td align="left"></td> 75<td align="right"><div class="copyright-footer">Copyright © 2001-2020 Boost.Test contributors<p> 76 Distributed under the Boost Software License, Version 1.0. (See accompanying 77 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 78 </p> 79</div></td> 80</tr></table> 81<hr> 82<div class="spirit-nav"> 83<a accesskey="p" href="contexts.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../test_tools_support_for_logging.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../log_formats.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 84</div> 85</body> 86</html> 87