ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 04 Apr 2013 20:34:40 +0200What are the differences between RealDoubleField() and RealField(53) ?https://ask.sagemath.org/question/9991/what-are-the-differences-between-realdoublefield-and-realfield53/Hi,
This question is related to
[question 2402](http://ask.sagemath.org/question/2402/what-are-the-different-real-numbers-in-sage)
(still open!) and tries to collect differences between RDF=RealDoubleField() and RR=RealField(53).
These are two floating point real number fields with both 53 bits of precision. The first one comes from the processor floating-point arithmetic, the second one is "emulated" by mpfr. They are assumed to follow the same rounding standards (to the nearest, according to the [sagebook](http://sagebook.gforge.inria.fr/), but i may be wrong).
However, we can see some differences between them:
sage: RDF(1/10)*10 == RDF(1)
False
sage: RDF(1/10)*10 - RDF(1)
-1.11022302463e-16
sage: RR(1/10)*10 == RR(1)
True
sage: sage: RR(1/10)*10 - RR(1)
0.000000000000000
Could you explain that ?
**EDIT: this was a bug and it is now fixed**, see [trac ticket 14416](http://trac.sagemath.org/ticket/14416).
There are also some specificities on which field should be used for some
methods.
- For example, it seems that the eignevalues are not well computed on RR, but are
correctly computed on RDF ([see trac #13660](http://trac.sagemath.org/sage_trac/ticket/13660)). What is the reason for that ?
- Also, it seems that when dealing with huge matrices, the fast atlas library in
only used when entries are in RDF, not in RR
([see trac #10815](http://trac.sagemath.org/sage_trac/ticket/10815)).
Are there other difference that should be known between both implementations of floating point numbers in Sage ?
ThierrytmonteilThu, 04 Apr 2013 20:34:40 +0200https://ask.sagemath.org/question/9991/What are the different real numbers in Sage ?https://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/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](http://www.sagemath.org/doc/reference/rings_standard/sage/rings/rational_field.html).
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](http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field.html) described by an irreducible polynomial.
- AA = AlgebraicField() The genuine [algebraic numbers](http://www.sagemath.org/doc/reference/number_fields/sage/rings/qqbar.html). 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: ####
- RDF = RealDoubleField() Those are the of [double precision floating numbers](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html) the processor. Definitely the fastest, but a rounding is done at each computation.
- RR = RealField() Floating number with arbitrary (still fixed) many bits of
precision. [http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_mpfr.html](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_mpfr.html)
Slower than RDF (in particular, atlas won't be used with them). Notice that
changing precision back and forth is a bad idea. Notice that NaN and Infinity belong to RDF and RR.
- RLF = RealLazyField() like RealField() but can wait that the user fixes the precision to get one. [http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_lazy.html](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_lazy.html)
> sage: a = RLF(pi)
> sage: RealField(100)(a+2)
> 5.1415926535897932384626433833
- RIF = RealIntervalField() A real number is represented as an interval
containing it, and whose extremities are RealFields elements [http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_mpfi.html](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_mpfi.html)
- RLF.interval_field() mixture of the two previous ones.
- sage.rings.real_lazy.LazyAlgebraic
#### 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](http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field_morphisms.html)
[maps](http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/maps.html)
are also defined to embed an abstract number field into the real line.
- The method [algebraic_dependency()](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html) 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 number, is it faster to work on AA or on a number field (of
smaller dimension) containing it ? Notice that one can find such an associated
number field with the method [as_number_field_element()](http://www.sagemath.org/doc/reference/number_fields/sage/rings/qqbar.html#sage.rings.qqbar.AlgebraicNumber_base.as_number_field_element)
Do you have any hint, striking example, recommandations of use in special cases
that one should like to learn in a tutorial about real numbers ?
tmonteilThu, 28 Mar 2013 12:36:28 +0100https://ask.sagemath.org/question/9950/