over 2 years ago

## GSoC #6: Round Modes and Their Behavior

In cfelton's implementation of fixbv, there are some kinds of round modes implemented in _resize.py, as in the source code:

The behavior of ceil, fix and floor is quite clear in this case. Whatever fractional part is, it will be rounded to the integer towards +inf, 0, or -inf.

But it seems not quite clear for the last four modes.

OK, let's see the source code in _resize.py first:

To read the last 4 kinds of resolution, it is necessary to know the behavior of Python's built-in round function.

Here we assume we do not provide ndigits parameter to round function.

Python's document says that round will round the numbers to the nearest integer. However, if the fractional part is 0.5, round will round to the nearest even number. That is to say, round(2.5) will be 2, but round(3.5) will be 4.

So, I guess that the behavior of round modes round, round_even, and convergent are the same.

Finally, nearest will be the same as the above three round modes in negative values, but for positive values, it will be different. If the fractional part is 0.5, it will advance to the larger integer, otherwise round to the nearest.

To verify this, I wrote a small program for it. The rounding code is copied from corresponding function in _resize.py.

It tests different values for different round modes.

Here is the result:

EDIT: The code of "nearest" rounding did not handle the case of negative values, so the condition case should be:

And its behavior when the fractional part is 0.5 (either positive or negative) should be rounding away from 0.