Floating point rounding modes: we should restrict them slightly
Don
nospam at nospam.com
Thu Sep 10 01:24:38 PDT 2009
A quiz:
Conside a function
real foo() {....}
This is not pure, so that it may side-effects, but even so, are listings
one and two equivalent?
LISTING ONE:
real x = foo(1) + foo(2) + foo(3);
LISTING TWO:
real x1 = foo(1);
real x2 = foo(2);
real x3 = foo(3);
real x = x1 + x2 + x3;
In C and C++ (and currently in D), they are NOT equivalent!
foo() is allowed to change the floating-point rounding mode, so it could
change the meaning of '+'.
This is quite ridiculous. This is a 'freedom' that comes with a heavy
price tag.
Actually doing this would make code nearly impossible to debug, and I
don't think anyone is stupid enough to actually do this
...but as long as it's theoretically possible, it prevents the compiler
from doing useful optimisations.
Disallowing this kind of obscure, dangerous nonsense is, I think, part
of the core mission of D.
PROPOSAL:
Change the spec by adding the line to float.html:
"If the floating-point rounding mode is changed within a function, it
must be restored before the function exits. If this rule is violated
(for example, by the use of inline asm), the rounding mode used for
subsequent calculations is undefined."
This slight tightening of semantics can have a significant benefit for
all floating-point code.
More information about the Digitalmars-d
mailing list