Struct std.math.FloatingPointControl
Control the Floating point hardware
struct FloatingPointControl
;
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 | |
hasExceptionTraps [get]
|
bool | |
rounding [set]
|
uint | Change the floating-point hardware rounding mode |
rounding [get]
|
uint |
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. |
Aliases
Name | Description |
---|---|
ExceptionMask
|
|
RoundingMode
|
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
// The set hardware exceptions and rounding modes will be disabled when
// leaving this scope.
}
Example
FloatingPointControl fpctrl;
fpctrl .rounding = FloatingPointControl .roundDown;
writeln(lrint(1.5)); // 1.0
fpctrl .rounding = FloatingPointControl .roundUp;
writeln(lrint(1.4)); // 2.0
fpctrl .rounding = FloatingPointControl .roundToNearest;
writeln(lrint(1.5)); // 2.0
Authors
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger