User:Netch/sandbox

Rounding to prepare for shorter precision
This rounding mode (RPSP in this chapter) is used to avoid getting wrong result with double (including multiple) rounding. With this rounding mode, one can avoid wrong result after double rounding, if all roundings except the final one are done using RPSP, and only final rounding uses the externally requested mode.

With decimal arithmetic, if there is a choice between numbers with the smallest sigificant digit 0 or 1, 4 or 5, 5 or 6, 9 or 0, then the number with such the digit different from 0 or 5 shall be selected; otherwise, choice is arbitrary. IBM defines that, in the latter case, a digit with the smaller magnitude shall be selected. RPSP can be applied with the step between two consequent roundings as small as a single digit (for example, rounding to 1/10 can be applied after rounding to 1/100). For example, when rounding to integer, 20.0 is rounded to 20; 20.1 and 20.9 are rounded to 21; 22.0, 22.1, and 22.9 are rounded to 22; 24.0, 24.1 ad 24.9 are rounded to 24; 25.0 is rounded to 25; 25.1 is rounded to 26. In the example from "Double rounding" section, rounding 9.46 to one decimal gives 9.4, which rounding to integer in turn gives 9.

With binary arithmetic, the rounding is made as "round to odd" (not to be mixed with "round half to odd".) For example, when rounding to 1/4:


 * x == 2.0 => result is 2
 * 2.0 < x < 2.5 => result is 2.25
 * x == 2.5 => result is 2.5
 * 2.5 < x < 3.0 => result is 2.75
 * x == 3.0 => result is 3.0

For correct results, RPSP can be applied with the step of at least 2 binary digits, otherwise, wrong result may appear. For example,


 * 3.125 RPSP to 1/4 => result is 3.25
 * 3.25 RPSP to 1/2 => result is 3.5
 * 3.5 round-half-to-even to 1 => result is 4 (wrong)

If the step was 2 bits or more, 3.25 RPSP to 1 => result is 3.

Double rounding
In some algorithms, an intermediate result is computed in a larger precision, then must be rounded to the final precision. Double rounding can be avoided by choosing an adequate rounding for the intermediate computation. This consists in avoiding to round to midpoints for the final rounding (except when the midpoint is exact). In binary arithmetic, the idea is to round the result toward zero, and set the least significant bit to 1 if the rounded result is inexact; this rounding is called sticky rounding. Equivalently, it consists in returning the intermediate result when it is exactly representable, and the nearest floating-point number with an odd significand otherwise; this is why it is also known as rounding to odd.