[Issue 360] Compile-time floating-point calculations are sometimes inconsistent
Bradley Smith
digitalmars-com at baysmith.com
Fri Sep 22 16:48:20 PDT 2006
To summarize: ---
The compiler is allowed to evaluate intermediate results at a greater
precision than that of the operands. The literal type suffix (like 'f')
only indicates the type. The compiler may maintain internally as much
precision as possible, for purposes of constant folding. Committing the
actual precision of the result is done as late as possible.
For a low-precision constant put the value into a static, non-const
variable. Since this is not really a constant, it cannot be constant
folded and therefore affected by a possible compile-time increase in
precision. However, if mixed with a higher precision at runtime, a
increase in precision will still occur.
The way to write robust floating point calculations in D is to ensure
that increasing the precision of the calculations will not break the
result.
--- end of summary
This is the explanation I was looking for. Although it was clear that
during runtime, D evaluates intermediate results at high precision. The
compile-time behavior (namely using a const) is different than the
runtime behavior (using a static), but I don't think that is clearly
explained in the documentation.
Would you please add this information to the D documentation? Perhaps an
addition to the Floating Point page
(http://www.digitalmars.com/d/float.html). Of course, if any of the
above is incorrect, please change as necessary.
A follow-on question would be: How does one create an low-precision
constant that is ensured to actually stay constant? A static won't do
since a static is really non-const, and a programming error would change
the value.
Thanks,
Bradley
More information about the Digitalmars-d-bugs
mailing list