View source code
Display the source code in std/math.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

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

NameTypeDescription
enabledExceptions[get] uint
hasExceptionTraps[get] bool
rounding[set] uintChange the floating-point hardware rounding mode
rounding[get] uint

Methods

NameDescription
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

NameDescription
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

License

Boost License 1.0.