Movement against float.init being nan
Paul Backus
snarwin at gmail.com
Thu Aug 25 16:06:43 UTC 2022
On Thursday, 25 August 2022 at 14:34:52 UTC, Paul Backus wrote:
> Looks like D allows you to enable floating-point exceptions
> using the [`std.math.hardware.FloatingPointControl`][1]
> interface.
>
> Of course, these are hardware exceptions, so you will get
> SIGFPE rather than a thrown Error with a backtrace. To turn
> them into D Errors you'd need to set up a signal handler, like
> how `etc.linux.memoryerror` does it (or whatever the equivalent
> is on Windows).
>
> [1]:
> https://phobos.dpldocs.info/std.math.hardware.FloatingPointControl.html
Unfortunately, it looks like this will not work in practice,
because the "invalid" floating-point exception is raised only
when a NaN is *created*, not when it's *propagated*. Since the
initial NaN values of default-initialized `float`/`double`
variables are created at compile time, not runtime, no exception
is ever raised for them.
Example program:
```d
import std.math.hardware;
float div(float x, float y)
{
return x / y;
}
void main()
{
FloatingPointControl fpctrl;
fpctrl.enableExceptions(FloatingPointControl.severeExceptions);
float f = float.nan; // ok - no exception
float g = div(0.0f, 0.0f); // crashes with SIGFPE
}
```
More information about the Digitalmars-d
mailing list