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 section Floating Point Representation Concepts).
Numbers smaller than @math{2^r} (where @math{r} is the minimum exponent,
FLT_MIN_RADIX-1
for float) cannot be represented as
normalized numbers. Rounding all such numbers to zero or @math{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
@math{4/-@infinity{}}. 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:
To change the rounding mode, use this function:
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.
Go to the first, previous, next, last section, table of contents.