Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Fixed-precision floating point types, the ones natively supported by your CPU's floating point unit (<code>float</code>, <code>double</code>, <code>real</code>) are not optimal for any calculation that needs many digits of precision, such as the example you've given.</p> <p>The problem is that these floating-point types have a finite number of digits of precision (binary digits, actually) that limits the length of number that can be represented by such a data type. The <code>float</code> type has a limit of approximately 7 decimal digits (e.g. 3.141593); the <code>double</code> type is limited to 14 (e.g. 3.1415926535898); and the <code>real</code> type has a similar limit (slightly more than that of <code>double</code>).</p> <p>Adding exceedingly small numbers to a floating-point value will therefore result in those digits being lost. Watch what happens when we add the following two float values together:</p> <pre><code>float a = 1.234567f, b = 0.0000000001234567 float c = a + b; writefln("a = %f b = %f c = %f", a, b, c); </code></pre> <p>Both <code>a</code> and <code>b</code> are valid float values and retain approximately 7 digits of precision apiece in isolation. But when added, only the frontmost 7 digits are preserved because it's getting shoved back into a float:</p> <pre><code>1.2345670001234567 =&gt; 1.234567|0001234567 =&gt; 1.234567 ^^^^^^^^^^^ sent to the bit bucket </code></pre> <p>So <code>c</code> ends up equal to <code>a</code> because the finer digits of precision from the addition of <code>a</code> and <code>b</code> get whacked off.</p> <p><a href="https://stackoverflow.com/questions/1218149">Here's another explanation of the concept</a>, probably much better than mine.</p> <hr> <p>The answer to this problem is arbitrary-precision arithmetic. Unfortunately, support for arbitrary-precision arithmetic is not in CPU hardware; therefore, it's not (typically) in your programming language. However, there are many libraries that support arbitrary-precision floating-point types and the math you want to perform on them. See <a href="https://stackoverflow.com/questions/2568446">this question</a> for some suggestions. You probably won't find any D-specific libraries for this purpose today, but there are plenty of C libraries (GMP, MPFR, and so on) that should be easy enough to use in isolation, and even more so if you can find D bindings for one of them.</p>
    singulars
    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. 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.
    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