Navigation

Operators and Keywords

Function List:

C++ API

Function File: fesetround (mode)

Change the rounding mode of the floating point arithmetics.

Following the IEEE-754 standard recommendation, modern processors, including x86 and x86_64 architectures, support changing the rounding direction of the floating point arithmetics.

Possible values of mode are

0
Sets the rounding mode to ‘towards zero’, i.e. ‘truncate
0.5
Sets the rounding mode to ‘nearest’ (the standard mode)
Inf
Sets the rounding mode to ‘up’, i.e. ‘towards +infinity
-Inf
Sets the rounding mode to ‘down’, i.e. ‘towards -infinity

When successful, fesetround returns 0. It is always recommended to verify the result experimentally by calling the function fetestenv, which tests the rounding mode and the precision of the floating point arithmetics.

It is known that some numerical libraries may set their own rounding modes. Therefore fesetround may affect operations performed by numerical libraries called by Octave in a different way than it does the interpreted code. Calls to such numerical libraries may also cancel changes previously made by fesetround:

          fesetprec(1); fesetround(0); fetestenv
          -| Machine epsilon: 1.19209e-07
          -| Rounding mode:   0
          hilb(2)\ones(2,1); fetestenv
          -| Machine epsilon: 2.22045e-16
          -| Rounding mode:   0.5

(the above test has been run on an x86 32-bit system using Octave 3.0.2 package provided along with the Fedora 9 Linux distribution).

A possible application of this function, following the idea by W. Kahan, is an experimental (in)stability analysis. Running some code with various settings of the floating point arithmetics may reveal some instability of the numerical algorithm being used in the code or reveal possible ill conditioning of the very problem being solved.

Literature

  1. W. Kahan, "How Futile are Mindless Assessments of Roundoff in Floating-Point Computation?", available on the Web: <http://www.cs.berkeley.edu/~wkahan/Mindless.pdf>.
  2. W. Kahan, "Why I can Debug some Numerical Programs You can't", available on the Web: <http://www.cs.berkeley.edu/~wkahan/Stnfrd50.pdf>.
  3. Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 1: Basic Architecture, May 2007.

Demonstration 1

The following code

  fesetround(+Inf);
  test = (1 + realmin > 1)
  fesetround(0.5);

Produces the following output

test =  1

Demonstration 2

The following code

  fesetround(0.5);
  test = (1 + realmin > 1)
  fesetround(0.5);

Produces the following output

test = 0