Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Huge difference.</p> <p>As the name implies, a <a href="http://en.wikipedia.org/wiki/Double_precision_floating-point_format" rel="noreferrer"><code>double</code></a> has 2x the precision of <a href="http://en.wikipedia.org/wiki/Single_precision_floating-point_format" rel="noreferrer"><code>float</code></a><sup>[1]</sup>. In general a <code>double</code> has 15 decimal digits of precision, while <code>float</code> has 7.</p> <p>Here's how the number of digits are calculated:</p> <blockquote> <p><code>double</code> has 52 mantissa bits + 1 hidden bit: log(2<sup>53</sup>)÷log(10) = 15.95 digits</p> <p><code>float</code> has 23 mantissa bits + 1 hidden bit: log(2<sup>24</sup>)÷log(10) = 7.22 digits</p> </blockquote> <p>This precision loss could lead to truncation errors much easier to float up, e.g.</p> <pre><code>float a = 1.f / 81; float b = 0; for (int i = 0; i &lt; 729; ++ i) b += a; printf("%.7g\n", b); // prints 9.000023 </code></pre> <p>while</p> <pre><code>double a = 1.0 / 81; double b = 0; for (int i = 0; i &lt; 729; ++ i) b += a; printf("%.15g\n", b); // prints 8.99999999999996 </code></pre> <p>Also, the maximum value of float is about <code>3e38</code>, but double is about <code>1.7e308</code>, so using <code>float</code> can hit "infinity" (i.e. a special floating-point number) much more easily than <code>double</code> for something simple, e.g. computing the factorial of 60.</p> <p>During testing, maybe a few test cases contain these huge numbers, which may cause your programs to fail if you use floats.</p> <hr> <p>Of course, sometimes, even <code>double</code> isn't accurate enough, hence we sometimes have <code>long double</code><sup>[1]</sup> (the above example gives 9.000000000000000066 on Mac), but all floating point types suffer from <em>round-off errors</em>, so if precision is very important (e.g. money processing) you should use <code>int</code> or a fraction class.</p> <hr> <p>Furthermore, don't use <code>+=</code> to sum lots of floating point numbers, as the errors accumulate quickly. If you're using Python, use <code>fsum</code>. Otherwise, try to implement the <a href="http://en.wikipedia.org/wiki/Kahan_summation_algorithm" rel="noreferrer">Kahan summation algorithm</a>.</p> <hr> <p><sup>[1]: The C and C++ standards do not specify the representation of <code>float</code>, <code>double</code> and <code>long double</code>. It is possible that all three are implemented as IEEE double-precision. Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) <code>float</code> <em>is</em> indeed a IEEE single-precision floating point number (binary32), and <code>double</code> <em>is</em> a IEEE double-precision floating point number (binary64).</sup></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload