Joel C. Salomon wrote:
Don wrote:
Consider this code:
double add(double x, double y) {
return x + y;
}
Is this code pure? (…)
Technically, it isn't. If you change the floating-point rounding mode on
the processor, you get a different result for exactly the same inputs.
IEEE 754-2008, §4 (Attributes and Rounding), prescribes:
An attribute is logically associated with a program block to
modify its numerical and exception semantics. A user can specify
a constant value for an attribute parameter.
Some attributes have the effect of an implicit parameter to most
individual operations of this standard; language standards shall
specify
— rounding-direction attributes (see 4.3)
and should specify
— alternate exception handling attributes (see 8).
If D offered some way to statically set these attributes on a block to
anything but the default, this could make the code pure. (Unless you
explicitly set the mode to “dynamic”, as normally is the case on x86.)
But there's only 2 modes that are important in practice: 'default' and
'dynamic'. If you have dynamic, any other mode can be done trivially
with a library solution. (A compiler vendor could choose to recognize
the function call as an intrinsic, and perform optimisatio n in a few cases.
More likely, though, you’d want to template code like this on the
currently-selected rounding mode.
So, can D become the first language to properly implement this aspect of
754-2008?
—Joel Salomon
Unless you have a specific use-case in mind?