Different ways of rounding

15 views
Skip to first unread message

Simon King

unread,
Apr 3, 2020, 1:14:03 PM4/3/20
to sage-s...@googlegroups.com
Hi!

According to IEEE 754, the default rounding mode for floating-point
operations is "round half to even". However, if one calls "round" on
elements of RR, the rounding apparently is "round half away from zero if
the total number of digits in the result is odd and towards zero if
the total number of digits of the result is even":

sage: round(3.5, ndigits=0)
4
sage: round(3.55, ndigits=1)
3.5
sage: round(3.555, ndigits=2)
3.56
sage: round(-3.5, ndigits=0)
-4
sage: round(-3.55, ndigits=1)
-3.5
sage: round(-3.555, ndigits=2)
-3.56
sage: round(-13.555, ndigits=2)
-13.55
sage: round(13.555, ndigits=2)
13.55
sage: builtins.round(13.555)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-29-9b82fece61b0> in <module>()
----> 1 builtins.round(RealNumber('13.555'))

TypeError: type sage.rings.real_mpfr.RealLiteral doesn't define __round__ method


This gives rise to loads of questions:

1. Is the rule that I formulated really what is used?
2. Is it documented somewhere? It is neither documented in the round()
function nor in the .round() method of sage.rings.real_mpfr.RealLiteral.
3. Is there some "official" standard (similar to IEEE 754) supporting
Sage's rounding?
4. Shouldn't the rounding be defined in a .__round__() method rather
than in a .round() method?
5. Is it possible to explicitly request "half to even" rounding?

Best regards,
Simon


Reply all
Reply to author
Forward
0 new messages