Home > Objects -> Numbers


I am only interested in doing numerical math with real numbers in Sage so I use: integers and decimals.

  • Three types of real numbers exist in sage: Integers (whole numbers), rationals (fractions) and decimals
  • I mention rationals (fractions) so you know they exist as a separate number type in Sage
  • Rule 1: Integers are used for counting.
    • To make any number into an integer, there are 3 commands: int(), floor(), ceiling()
  • Rule 2: Decimals are for calculating. Sage uses only double precision 64 bit decimals (I think).
    • To make any number into a decimal, there is 1 command: float()

I have gotten every error known to man and God with respect to number types - as I get them again, I will add them here and to the page.

Floating Point Arithmetic - Robert Bradshaw

In response to my question about the various types of decimals in Sage, Robert Bradshaw wrote this to me ... Thank-you Robert.
Floating point arithmetic: A cannonical reference is http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html, but if that's a bit dense there are many other good sources on the web.

Things get more interesting for Sage, as there are several models for floating point numbers available depending on the use. In turn these are:
(1) float, created with float(3.0) or 3.0r, which is the native Python floating point represented by the machine (typically IEEE 754 double or extended precision), fastest but potentially less accurate and platform dependant.
(2) sage.rings.real_double.RealDoubleElement, created by RDF(3.0), which is also IEEE 754 double or extended precision, but has more sage-specific functionality.
(3) sage.rings.real_mpfr.RealNumber, the default, created by RR(3.0) or doing arithmetic with 3.0, back by http://www.mpfr.org/ to 53-bits of precision, accurate rounding on all operations, platform-independent, but slower than the above
(3b) sage.rings.real_mpfr.RealNumber with alternative precision, e.g. RealField(1000)(3) for 1000 bits (not digits) of precision.

Less common:
(4) sage.rings.real_mpfi.RealIntervalFieldElement, created by RIF(3.0), which does arithmetic on intervals (as most real numbers can't be represented accurately) and can bue used to provide provable statements of inequality. Can also be arbitrary precision, e.g. RealIntervalField(1000)(3)
(5) RLF, the real lazy field (mostly used under the hood for coercion from exact to in-exact domains).
(6) sage.rings.real_mpfr.RealLiteral, which is what you get when typing 3.0, which stores the typed value to infinite precision and converts to a fixed precision (53-bit if implicit) upon demand. This is needed so that RealField(1000)(1.2) is 1.2 to 1000 bits, not just 53 bits (as it would be if 1.2 was parsed to 53 bits then passed in to RealField(1000)).

Other References for Number Types in Sage: Rationals (et.al.) in Sage

Keywords: sage, numbers, integers, decimals, float, precision