std.math.IeeeFlags/ieeeFlags
- multiple declarations
Function ieeeFlags
Returns
snapshot of the current state of the floating-point status flags
Example
version (InlineAsm_X86_Any)
{
pragma(inline, false) static void blockopt(ref real x) {}
resetIeeeFlags();
real a = 3.5;
blockopt(a); // avoid constant propagation by the optimizer
a /= 0.0L;
writeln(a); // real.infinity
assert(ieeeFlags .divByZero);
blockopt(a); // avoid constant propagation by the optimizer
a *= 0.0L;
assert(isNaN(a));
assert(ieeeFlags .invalid);
}
}
/** Control the Floating point hardware
Change the IEEE754 floating-point rounding mode and the floating-point
hardware exceptions.
By default, the rounding mode is roundToNearest and all hardware exceptions
are disabled. For most applications, debugging is easier if the <i>division
by zero</i>, <i>overflow</i>, and <i>invalid operation</i> exceptions are enabled.
These three are combined into a <i>severeExceptions</i> value for convenience.
Note in particular that if <i>invalidException</i> is enabled, a hardware trap
will be generated whenever an uninitialized floating-point variable is used.
All changes are temporary. The previous state is restored at the
end of the scope.
Example:
{ FloatingPointControl fpctrl;
// Enable hardware exceptions for division by zero, overflow to infinity, // invalid operations, and uninitialized floating-point variables. fpctrl.enableExceptions(FloatingPointControl.severeExceptions);
// This will generate a hardware exception, if x is a
// default-initialized floating point variable:
real x; // Add = 0
or even = real
to not throw the exception.
real y = x * 3.0;
// The exception is only thrown for default-uninitialized NaN-s. // NaN-s with other payload are valid: real z = y * real.nan; // ok
// The set hardware exceptions and rounding modes will be disabled when // leaving this scope.
Struct IeeeFlags
IEEE exception status flags ('sticky bits')
struct IeeeFlags
;
These flags indicate that an exceptional floating-point condition has occurred. They indicate that a NaN or an infinity has been generated, that a result is inexact, or that a signalling NaN has been encountered. If floating-point exceptions are enabled (unmasked), a hardware exception will be generated instead of setting these flags.
Properties
Name | Type | Description |
---|---|---|
divByZero [get]
|
bool | An infinity was generated by division by zero |
divByZero [get]
|
bool | An infinity was generated by division by zero |
inexact [get]
|
bool | The result cannot be represented exactly, so rounding occurred. |
inexact [get]
|
bool | The result cannot be represented exactly, so rounding occurred. |
invalid [get]
|
bool | A machine NaN was generated. |
invalid [get]
|
bool | A machine NaN was generated. |
overflow [get]
|
bool | An infinity was generated by overflow |
overflow [get]
|
bool | An infinity was generated by overflow |
underflow [get]
|
bool | A zero was generated by underflow |
underflow [get]
|
bool | A zero was generated by underflow |
Example
version (InlineAsm_X86_Any)
{
static void func() {
int a = 10 * 10;
}
pragma(inline, false) static void blockopt(ref real x) {}
real a = 3.5;
// Set all the flags to zero
resetIeeeFlags();
assert(!ieeeFlags .divByZero);
blockopt(a); // avoid constant propagation by the optimizer
// Perform a division by zero.
a /= 0.0L;
writeln(a); // real.infinity
assert(ieeeFlags .divByZero);
blockopt(a); // avoid constant propagation by the optimizer
// Create a NaN
a *= 0.0L;
assert(ieeeFlags .invalid);
assert(isNaN(a));
// Check that calling func() has no effect on the
// status flags.
IeeeFlags f = ieeeFlags;
func();
writeln(ieeeFlags); // f
}
Authors
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger