Previous: Floating-point Context, Up: Floating-point Programming


15.2.3 Floating-point Rounding Mode

The rounding mode specifies the behavior for the results of numerical operations when discarding extra precision. Each rounding mode indicates how the least significant returned digit of a rounded result is to be calculated. table-rounding-modes lists the IEEE-754 defined rounding modes:

Rounding Mode IEEE Name
Round to nearest, ties to even roundTiesToEven
Round toward plus Infinity roundTowardPositive
Round toward negative Infinity roundTowardNegative
Round toward zero roundTowardZero
Round to nearest, ties away from zero roundTiesToAway

Table 15.2: IEEE 754 Rounding Modes

The default mode roundTiesToEven is the most preferred, but the least intuitive. This method does the obvious thing for most values, by rounding them up or down to the nearest digit. For example, rounding 1.132 to two digits yields 1.13, and rounding 1.157 yields 1.16.

However, when it comes to rounding a value that is exactly halfway between, things do not work the way you probably learned in school. In this case, the number is rounded to the nearest even digit. So rounding 0.125 to two digits rounds down to 0.12, but rounding 0.6875 to three digits rounds up to 0.688. You probably have already encountered this rounding mode when using printf to format floating-point numbers. For example:

     BEGIN {
         x = -4.5
         for (i = 1; i < 10; i++) {
             x += 1.0
             printf("%4.1f => %2.0f\n", x, x)
         }
     }

produces the following output when run on the author's system:1

     -3.5 => -4
     -2.5 => -2
     -1.5 => -2
     -0.5 => 0
      0.5 => 0
      1.5 => 2
      2.5 => 2
      3.5 => 4
      4.5 => 4

The theory behind the rounding mode roundTiesToEven is that it more or less evenly distributes upward and downward rounds of exact halves, which might cause any round-off error to cancel itself out. This is the default rounding mode used in IEEE-754 computing functions and operators.

The other rounding modes are rarely used. Round toward positive infinity (roundTowardPositive) and round toward negative infinity (roundTowardNegative) are often used to implement interval arithmetic, where you adjust the rounding mode to calculate upper and lower bounds for the range of output. The roundTowardZero mode can be used for converting floating-point numbers to integers. The rounding mode roundTiesToAway rounds the result to the nearest number and selects the number with the larger magnitude if a tie occurs.

Some numerical analysts will tell you that your choice of rounding style has tremendous impact on the final outcome, and advise you to wait until final output for any rounding. Instead, you can often avoid round-off error problems by setting the precision initially to some value sufficiently larger than the final desired precision, so that the accumulation of round-off error does not influence the outcome. If you suspect that results from your computation are sensitive to accumulation of round-off error, one way to be sure is to look for a significant difference in output when you change the rounding mode.


Footnotes

[1] It is possible for the output to be completely different if the C library in your system does not use the IEEE-754 even-rounding rule to round halfway cases for printf.