Floating-Point Rounding Mode (MIT/GNU Scheme 12.0.90)

Previous: Floating-Point Exceptions, Up: Fixnum and Flonum Operations [Contents][Index]

#### 4.8.5 Floating-Point Rounding Mode

IEEE 754-2008 supports four rounding modes, which determine the answer given by a floating-point computation when the exact result lies between two floating-point numbers but is not a floating-point number itself:

`to-nearest`

  Round to the nearest floating-point number. If there are two equidistant ones, choose the one whose least significant digit is even. Also known as “round-to-nearest/ties-to-even”.

`toward-zero`

  Round to the floating-point number closest to zero.

`downward`

  Round to the greatest floating-point number below.

`upward`

  Round to the least floating-point number above.

Warning: Not all procedures in MIT/GNU Scheme respect the rounding mode. Only the basic arithmetic operations — +, -, *, /, and sqrt — will reliably respect it. The main purpose of changing the rounding mode is to diagnose numerical instability by injecting small perturbations throughout the computation.

Bug: It would be nice if we had “round-to-odd”, where any inexact result is rounded to the nearest odd floating-point number, for implementing “doubled”-precision algorithms. But we don’t. Sorry.

procedure: flo:default-rounding-mode ¶

  Returns a symbol for the default rounding mode, which is always `to-nearest`.

procedure: flo:rounding-modes ¶

  Returns a list of the supported rounding modes as symbols.

procedure: flo:rounding-mode ¶
procedure: flo:set-rounding-mode! mode ¶

  Gets or sets the current rounding mode as a symbol, entering a per-thread environment.

procedure: flo:with-rounding-mode mode thunk ¶

  Call thunk in a per-thread environment with the rounding mode set to mode. On return, the floating-point environment, including rounding mode, is restored to what it was before.

  Non-local exit from and re-entrance to thunk behaves as if the call is surrounded by `flo:preserving-environment` (see Floating-Point Environment).

Previous: Floating-Point Exceptions, Up: Fixnum and Flonum Operations [Contents][Index]