Floating point rounding modes: we should restrict them slightly
Michel Fortin
michel.fortin at michelf.com
Thu Sep 10 09:03:10 PDT 2009
On 2009-09-10 04:24:38 -0400, Don <nospam at nospam.com> said:
> 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.
I perfectly agree. I'm just wondering how to enforce it in the language.
Later in the thread you say:
> ... have an RAII struct to restore the mode automatically. But that's
> just syntax sugar.
But how do you prevent someone from writing:
auto s = new roundingMode(...);
The only way you can really make sure it's scoped to a specific
function is to call it from another function:
R performWithRoundingMode(R)(flag roundingMode, lazy R result)
{
flag oldRoundingMode = getRoundingMode();
setRoundingMode(roundingMode);
try
return result;
finally
setRoundingMode(oldRoundingMode);
}
auto i = performWithRoundingMode(TOWARDS_ZERO, 12 * 12);
Here you can't escape the scoping requirements.
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list