Ask Your Question

What are the different real numbers in Sage ?

asked 2013-03-28 12:36:28 +0200

tmonteil gravatar image

updated 2013-04-23 08:26:49 +0200

No computer can grasp the genuine real numbers, so I am trying to understand how sage handles them (and going to write a small tutorial about this). Here is a list of approaching objects i found:

Exact subfields of the reals:

  • QQ = RationalField() The genuine rationals. Denominators can be arbitrary big, the computations are exact, so the rounding error is only made when approaching a real by a rational, not during furher computations. Notice that the size of the denominators may grow along the computations, which can therefore become very slow.

  • NumberField? number fields described by an irreducible polynomial.

  • AA = AlgebraicField() The genuine algebraic numbers. Computations are exact.

  • SR = sage.symbolic.ring.SymbolicRing() This is the field made of expressions like sqrt(2+pi). It handles a lot of real numbers but seems very slow. Computations are not necessarily exact, since this field contains floating points real numbers. Is there a way to express any algebraic number in SR ?

  • CFF = ContinuedFractionField() reals are represented as a finite list of convergents. Claims it is exact but it actually approaches real numbers by rationals since the list of convergents are finite. However, we could imagine a future version using iterators instead of lists.

sage: CFF(sqrt(2))^2

[1, 1, 1491038293021224]

Floating point numbers:

sage: a = RLF(pi)

sage: RealField(100)(a+2)


Relations between those fields

  • Two fields approaching the real numbers K and L can be compared using the function composite_field(K,L), that finds the "best" field containing both K and L.

sage: composite_field(RDF,QQbar)

Complex Double Field

  • Some maps are also defined to embed an abstract number field into the real line.

  • The method algebraic_dependency() that allows to guess whether a floating number corresponds to some algebraic number of a given degree.

Is this list complete or am i missing other real representations ? Im i wrong in the previous descriptions ? Could you order them by speed, by number of methods available (without coercion) ? Do you have any remark that could help the beginner to understand the subtilities in the use of those fields in Sage ?

Given an algebraic ... (more)

edit retag flag offensive close merge delete



It would be really nice to have a list like this in the documentation.

Eviatar Bach gravatar imageEviatar Bach ( 2013-03-28 18:16:55 +0200 )edit

Apparently there are also RealLiterals, which seem to be none of the above.

kcrisman gravatar imagekcrisman ( 2017-03-03 14:51:41 +0200 )edit

See for a sample worksheet.

tmonteil gravatar imagetmonteil ( 2018-10-12 11:33:16 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2013-03-29 06:37:34 +0200

vdelecroix gravatar image

updated 2013-03-29 06:41:53 +0200


As Eviatar said, it would be very nice to have it in Sage documentation! Here are some more remarks.

0) RLF is not a floating point field but an exact field: those are real numbers with an arbitrary precision. But as said in the documentation, it is mostly used as an "intermediate" field between number fields and all different floating point fields.

1) There are three nice methods to get from RR to QQ:

sage: a = RR(1.2254)
sage: a.simplest_rational()
sage: a.exact_rational()
sage: a.nearby_rational(max_error=0.0001)

Note that .nearby_rational() implements its own continued fraction algorithm.

2) There should be a huge warning for comparison of elements from different fields of real numbers::

sage: pi_rlf = RLF(pi)
sage: pi_rr = RR(pi)
sage: pi_rlf
sage: pi_rr

sage: pi_rlf == pi_rr
sage: RLF(pi_rr) == pi_rlf

More generally, if you intend to make some documentation, it may be very useful to have a two dimensional array which says given x from field F1 and y from field F2 where is made the comparison x == y.

3) RIF is especially useful to locate roots of a polynomial. Actually, AA uses RIF with various precisions in background (which is essential for comparisons):

sage: a = AA(sqrt(2) + sqrt(3) + sqrt(7))
sage: a._value.parent()
Real Interval Field with 64 bits of precision
sage: a.interval_diameter(2**-20)
sage: a.interval_diameter(2**-70)
sage: a._value.parent()
Real Interval Field with 128 bits of precision

There is a big difference between AA and number fields: number fields, even with a specified embedding, are not able to make comparisons (but you may have a look at tickets #13213 and #7160).

4) SR contains much more than numbers:

sage: sin(x).parent()
Symbolic Ring

And there is a wrapper for symbolic numbers to make them an element of RLF:

sage: log(2).parent()
Symbolic Ring
sage: log2 = RLF(log(2))
sage: log2

But which does not work quite well:

sage: exp(log2) == 2


edit flag offensive delete link more

answered 2013-04-03 15:19:55 +0200

tmonteil gravatar image

Perhaps should we add the Python builtin float as well.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2013-03-28 12:36:28 +0200

Seen: 7,261 times

Last updated: Apr 23 '13