Next: Control Functions, Previous: Floating Point Errors, Up: Arithmetic
Floating-point calculations are carried out internally with extra precision, and then rounded to fit into the destination type. This ensures that results are as precise as the input data. IEEE 754 defines four possible rounding modes:
FLT_EPSILON
.
fenv.h defines constants which you can use to refer to the various rounding modes. Each one will be defined if and only if the FPU supports the corresponding rounding mode.
FE_TONEAREST
FE_UPWARD
FE_DOWNWARD
FE_TOWARDZERO
Underflow is an unusual case. Normally, IEEE 754 floating point
numbers are always normalized (see Floating Point Concepts).
Numbers smaller than 2^r (where r is the minimum exponent,
FLT_MIN_RADIX-1
for float) cannot be represented as
normalized numbers. Rounding all such numbers to zero or 2^r
would cause some algorithms to fail at 0. Therefore, they are left in
denormalized form. That produces loss of precision, since some bits of
the mantissa are stolen to indicate the decimal point.
If a result is too small to be represented as a denormalized number, it
is rounded to zero. However, the sign of the result is preserved; if
the calculation was negative, the result is negative zero.
Negative zero can also result from some operations on infinity, such as
4/-∞. Negative zero behaves identically to zero except
when the copysign
or signbit
functions are used to check
the sign bit directly.
At any time one of the above four rounding modes is selected. You can find out which one with this function:
Returns the currently selected rounding mode, represented by one of the values of the defined rounding mode macros.
To change the rounding mode, use this function:
Changes the currently selected rounding mode to round. If round does not correspond to one of the supported rounding modes nothing is changed.
fesetround
returns zero if it changed the rounding mode, a nonzero value if the mode is not supported.
You should avoid changing the rounding mode if possible. It can be an expensive operation; also, some hardware requires you to compile your program differently for it to work. The resulting code may run slower. See your compiler documentation for details.