Struct std.math.FloatingPointControl
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 division
by zero, overflow, and invalid operation exceptions are enabled.
These three are combined into a severeExceptions
value for convenience.
Note in particular that if invalidException 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.
Properties
Name | Type | Description |
---|---|---|
enabledExceptions
[get]
|
uint |
Return the exceptions which are currently enabled (unmasked) |
hasExceptionTraps
[get]
|
bool |
Returns true if the current FPU supports exception trapping |
rounding
[set]
|
uint |
Change the floating-point hardware rounding mode
|
rounding
[get]
|
uint |
Return the currently active rounding mode
|
Methods
Name | Description |
---|---|
disableExceptions
|
Disable (mask) specific hardware exceptions . Multiple exceptions may be ORed together.
|
enableExceptions
|
Enable (unmask) specific hardware exceptions . Multiple exceptions may be ORed together.
|
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.nan
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 // Changing the rounding mode: fpctrl.rounding = FloatingPointControl.roundUp; assert(rint(1.1) == 2); // The set hardware exceptions will be disabled when leaving this scope. // The original rounding mode will also be restored. } // Ensure previous values are returned: assert(!FloatingPointControl.enabledExceptions); assert(FloatingPointControl.rounding == FloatingPointControl.roundToNearest); assert(rint(1.1) == 1);
Authors
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw