GNU Scientific Library Reference Manual  Third Edition (v1.12) by M. Galassi, J. Davies, J. Theiler, B. Gough, G. Jungman, P. Alken, M. Booth, F. Rossi Paperback (6"x9"), 592 pages, 60 figures ISBN 0954612078 RRP £24.95 ($39.95) 
40.2 Setting up your IEEE environment
The IEEE standard defines several modes for controlling the behavior of floating point operations. These modes specify the important properties of computer arithmetic: the direction used for rounding (e.g. whether numbers should be rounded up, down or to the nearest number), the rounding precision and how the program should handle arithmetic exceptions, such as division by zero.
Many of these features can now be controlled via standard functions such
as fpsetround
, which should be used whenever they are available.
Unfortunately in the past there has been no universal API for
controlling their behavioreach system has had its own lowlevel way
of accessing them. To help you write portable programs GSL allows you
to specify modes in a platformindependent way using the environment
variable GSL_IEEE_MODE
. The library then takes care of all the
necessary machinespecific initializations for you when you call the
function gsl_ieee_env_setup
.
 Function: void gsl_ieee_env_setup ()
 This function reads the environment variable
GSL_IEEE_MODE
and attempts to set up the corresponding specified IEEE modes. The environment variable should be a list of keywords, separated by commas, like this,GSL_IEEE_MODE
= "keyword,keyword,..."where keyword is one of the following modenames,

singleprecision

doubleprecision

extendedprecision

roundtonearest

rounddown

roundup

roundtozero

maskall

maskinvalid

maskdenormalized

maskdivisionbyzero

maskoverflow

maskunderflow

trapinexact

trapcommon
If
GSL_IEEE_MODE
is empty or undefined then the function returns immediately and no attempt is made to change the system's IEEE mode. When the modes fromGSL_IEEE_MODE
are turned on the function prints a short message showing the new settings to remind you that the results of the program will be affected.If the requested modes are not supported by the platform being used then the function calls the error handler and returns an error code of
GSL_EUNSUP
.When options are specified using this method, the resulting mode is based on a default setting of the highest available precision (double precision or extended precision, depending on the platform) in roundtonearest mode, with all exceptions enabled apart from the inexact exception. The inexact exception is generated whenever rounding occurs, so it must generally be disabled in typical scientific calculations. All other floatingpoint exceptions are enabled by default, including underflows and the use of denormalized numbers, for safety. They can be disabled with the individual
mask
settings or together usingmaskall
.The following adjusted combination of modes is convenient for many purposes,
GSL_IEEE_MODE="doubleprecision,"\ "maskunderflow,"\ "maskdenormalized"
This choice ignores any errors relating to small numbers (either denormalized, or underflowing to zero) but traps overflows, division by zero and invalid operations.
Note that on the x86 series of processors this function sets both the original x87 mode and the newer mxcsr mode, which controls SSE floatingpoint operations. The SSE floatingpoint units do not have a precisioncontrol bit, and always work in doubleprecision. The singleprecision and extendedprecision keywords have no effect in this case.

To demonstrate the effects of different rounding modes consider the following program which computes e, the base of natural logarithms, by summing a rapidlydecreasing series,
e = 1 + 1/2! + 1/3! + 1/4! + ... = 2.71828182846...
#include <stdio.h> #include <gsl/gsl_math.h> #include <gsl/gsl_ieee_utils.h> int main (void) { double x = 1, oldsum = 0, sum = 0; int i = 0; gsl_ieee_env_setup (); /* read GSL_IEEE_MODE */ do { i++; oldsum = sum; sum += x; x = x / i; printf ("i=%2d sum=%.18f error=%g\n", i, sum, sum  M_E); if (i > 30) break; } while (sum != oldsum); return 0; }
Here are the results of running the program in roundtonearest
mode. This is the IEEE default so it isn't really necessary to specify
it here,
$ GSL_IEEE_MODE="roundtonearest" ./a.out i= 1 sum=1.000000000000000000 error=1.71828 i= 2 sum=2.000000000000000000 error=0.718282 .... i=18 sum=2.718281828459045535 error=4.44089e16 i=19 sum=2.718281828459045535 error=4.44089e16
After nineteen terms the sum converges to within
4 \times 10^16 of the correct value.
If we now change the rounding mode to
rounddown
the final result is less accurate,
$ GSL_IEEE_MODE="rounddown" ./a.out i= 1 sum=1.000000000000000000 error=1.71828 .... i=19 sum=2.718281828459041094 error=3.9968e15
The result is about
4 \times 10^15
below the correct value, an order of magnitude worse than the result
obtained in the roundtonearest
mode.
If we change to rounding mode to roundup
then the final result
is higher than the correct value (when we add each term to the sum the
final result is always rounded up, which increases the sum by at least
one tick until the added term underflows to zero). To avoid this
problem we would need to use a safer converge criterion, such as
while (fabs(sum  oldsum) > epsilon)
, with a suitably chosen
value of epsilon.
Finally we can see the effect of computing the sum using
singleprecision rounding, in the default roundtonearest
mode. In this case the program thinks it is still using double precision
numbers but the CPU rounds the result of each floating point operation
to singleprecision accuracy. This simulates the effect of writing the
program using singleprecision float
variables instead of
double
variables. The iteration stops after about half the number
of iterations and the final result is much less accurate,
$ GSL_IEEE_MODE="singleprecision" ./a.out .... i=12 sum=2.718281984329223633 error=1.5587e07
with an error of O(10^7), which corresponds to single precision accuracy (about 1 part in 10^7). Continuing the iterations further does not decrease the error because all the subsequent results are rounded to the same value.
ISBN 0954612078  GNU Scientific Library Reference Manual  Third Edition (v1.12)  See the print edition 