Java Reference
In-Depth Information
Rounding
Rounding (or rounding-off) is the process of adjusting a numerical value so
that it fits in a particular format. In general, the purpose of rounding opera-
tions is to reduce the error that arises from the loss of one or more digits.
For example, the number 27,445.89 can be reduced to an integer value by
truncating it to 27,445 or by rounding to 27,446. In this case the rounded
value is a more accurate representation of the original number than the one
obtained by chopping off the last two digits.
IEEE 754 requires that implementations provide the following round-
ing modes:
1. Round to Nearest . This should be the default rounding mode. In this mode
the result is the nearest representable value. The standard also describes
howroundingistotakeplacewhentheresultisequallyneartworepresent-
able values. This case, sometimes called the halfway case, occurs when
rounding decimal numbers in which the last non-zero digit is 5.
For example,with the number 128.500 the arbitrary rounding rule often
taught in high-school is to round up. This value would be rounded to the in-
teger 129. An alternative rounding mode is called round to nearest even .In
rounding the value 20,000.50 to an integer value there are two equally near
options: 20,001 and 20,000. In the rounding to the nearest even mode the
number 20,000 is preferred since it is an even number. Binary representa-
tions can be easily rounded to the nearest even result by selecting the value
in which the least significant bit is zero. Note that this method is also valid
with binary coded decimals.
2. Round to positive infinity . In this rounding mode the result is rounded to
the next highest representable value. This rounding mode is sometimes
called rounding up.
3. Round to negative infinity . In this rounding mode the result is rounded to
the next lowest representable value. This rounding mode is sometimes
called rounding down.
4. Truncate . According to the definition at the beginning of this section, trun-
cationisnotconsideredaroundingmode.Truncation,alsocalledchopping
or chopping-off, consists in discarding the non-representable portion and
disregarding its value. The chop-off operation is sometimes used in gener-
ating an integer result from a fractional operand.
Interval arithmetic
The possibility of selecting rounding to positive infinity or negative infinity
(round-up and round-down) allows the use of a technique known as interval
Search WWH ::




Custom Search