Rounding in Sage

399 views
Skip to first unread message

Simon King

unread,
Apr 7, 2020, 10:10:38 AM4/7/20
to sage-...@googlegroups.com
Hi!

A few days ago, I asked on sage-support about rounding in Sage. But
since there was no answer and since it is relevant to my teaching in the
upcoming semester, let me repost here (with modifications).

According to IEEE 754, the default rounding mode for floating-point
operations is "round half to even". However, in examples it seems that
the rounding roule in RR is: "round half away from zero if the total
number of decimal digits in the result is odd and towards zero if the
total number of decimal digits of the result is even". Does that conform
to an established standard?

For teaching, it would be nice to have a customisable rounding function
that in particular allows rounding for different bases (binary, decimal,
etc), and allows to chose different rules (always round to 0, always
round away from 0, always round to +infty, always round to -infty,
round half to 0, round half away from 0, round half to +infty, round
half to -infty, round half to even, round half to odd, round half
alternatingly to 0 and away from 0, do the rounding in RR, ...).

Is there such function in Sage? Otherwise I would just write a custom
function for my course.

Best regards,
Simon

Thierry

unread,
Apr 7, 2020, 10:16:59 AM4/7/20
to sage-...@googlegroups.com
Hi,

On Tue, Apr 07, 2020 at 02:10:28PM -0000, Simon King wrote:
> Hi!
>
> A few days ago, I asked on sage-support about rounding in Sage. But
> since there was no answer and since it is relevant to my teaching in the
> upcoming semester, let me repost here (with modifications).

An appropriate place seems to be : https://ask.sagemath.org/questions/

> According to IEEE 754, the default rounding mode for floating-point
> operations is "round half to even". However, in examples it seems that
> the rounding roule in RR is: "round half away from zero if the total
> number of decimal digits in the result is odd and towards zero if the
> total number of decimal digits of the result is even". Does that conform
> to an established standard?
>
> For teaching, it would be nice to have a customisable rounding function
> that in particular allows rounding for different bases (binary, decimal,
> etc), and allows to chose different rules (always round to 0, always
> round away from 0, always round to +infty, always round to -infty,
> round half to 0, round half away from 0, round half to +infty, round
> half to -infty, round half to even, round half to odd, round half
> alternatingly to 0 and away from 0, do the rounding in RR, ...).
>
> Is there such function in Sage? Otherwise I would just write a custom
> function for my course.

RR is a shortcut for RealField(), but the RealField function has a `rnd`
option, see:

sage: RealField?

Ciao,
Thierry


> Best regards,
> Simon
>
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/r6i1kk%24371d%241%40ciao.gmane.io.

Marc Mezzarobba

unread,
Apr 7, 2020, 11:34:58 AM4/7/20
to sage-...@googlegroups.com
Hi Simon,

Simon King wrote:
> According to IEEE 754, the default rounding mode for floating-point
> operations is "round half to even". However, in examples it seems that
> the rounding roule in RR is: "round half away from zero if the total
> number of decimal digits in the result is odd and towards zero if the
> total number of decimal digits of the result is even

I don't think I'm able to provide a complete answer, but here a a few
elements.

In principle, I think both RR and RDF should comply with IEEE-754
rounding rules (in the case of RDF, provided your platform does). In
particular, simply converting a rational number to a certain RealField
should (as far as I understand) round it to that field's precision
according to that field's rounding mode.

However, the examples you posted to sage-support, e.g.,

sage: round(3.55, ndigits=1)
3.5
sage: round(3.555, ndigits=2)
3.56

test much more than that. First, the round() toplevel function is a huge
mess, see #25827 for some observations about it. Second, when you write
3.55, the preparser does not turn that into code that creates a
RealNumber. An intermediate type called RealLiteral is used, with its
own “features” related to rounding, see in particular #15542. Third,
while any binary floating-point number can in principle be represented
exactly in decimal, Sage sometimes tries to be clever when displaying
floating-point numbers, which can involve rounding them again, see in
particular the docstring of RealNumber.str().

Hope this helps...

--
Marc

Thierry

unread,
Apr 7, 2020, 11:53:58 AM4/7/20
to sage-...@googlegroups.com
By the way, an excellent ressource to teach those kind of things and
check carefully what happens is the sign_mantissa_exponent method:

sage: a = RR(1.1)
sage: a
1.10000000000000
sage: a.sign_mantissa_exponent()
(1, 4953959590107546, -52)

Ciao,
Thierry
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/r6i6ip%242sdn%241%40ciao.gmane.io.

Simon King

unread,
Apr 7, 2020, 12:08:26 PM4/7/20
to sage-...@googlegroups.com
On 2020-04-07, Thierry <sage-goo...@lma.metelu.net> wrote:
> An appropriate place seems to be : https://ask.sagemath.org/questions/

I will never prefer ask.sagemath over sage-support.

Simon King

unread,
Apr 7, 2020, 12:15:06 PM4/7/20
to sage-...@googlegroups.com
Hi!

On 2020-04-07, Thierry <sage-goo...@lma.metelu.net> wrote:
> By the way, an excellent ressource to teach those kind of things and
> check carefully what happens is the sign_mantissa_exponent method:
>
> sage: a = RR(1.1)
> sage: a
> 1.10000000000000
> sage: a.sign_mantissa_exponent()
> (1, 4953959590107546, -52)

Nice, I didn't know about that method. Thank you for the hint!
Simon

Samuel Lelievre

unread,
Apr 8, 2020, 1:36:12 PM4/8/20
to sage-devel
About Sage and floating-point numbers, if I remember correctly,

- IEEE 754 is not a complete specification, some details are left
  up to the implementation and therefore IEEE 754 compliant
  computations can vary from machine to machine (depending
  on processor or OS)
- in Sage, `RDF` and `float` use machine floats and so follow IEEE 754
- by contrast RR and RealField are specified more tightly so as to
  give the same computations on any machine
- the range of exponents in RR is a lot wider than in RDF
- RDF is a lot faster than RR

Two excellent sources on real and floating-point numbers in Sage

- The many ways of computing with real numbers
  Talk by Vincent Delecroix at Sage Days 88 in IMA, U Minnesota, 2017

- What are the different real numbers in Sage

Related Sage Trac tickets

- Deprecate is_RealField / is_RealNumber

- rename RealField/RealNumber

- Modernize sage.rings.real_mpfr

- Thematic tutorial about real and complex numbers in Sage

- Deprecate global RealNumber() and ComplexNumber()

Simon King

unread,
Apr 9, 2020, 2:21:58 AM4/9/20
to sage-...@googlegroups.com
Hi Samuel,

thank you for the interesting sources!

Best regards,
Simon
Reply all
Reply to author
Forward
0 new messages