ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 12 Oct 2018 04:33:16 -0500What are the different real numbers in Sage ?http://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 ?
Thu, 28 Mar 2013 06:36:28 -0500http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/Comment by tmonteil for <div class="snippet"><p>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:</p>
<h4>Exact subfields of the reals:</h4>
<ul>
<li><p>QQ = RationalField() The genuine
<a href="http://www.sagemath.org/doc/reference/rings_standard/sage/rings/rational_field.html">rationals</a>.
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.</p></li>
<li><p>NumberField? <a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field.html">number fields</a> described by an irreducible polynomial.</p></li>
<li><p>AA = AlgebraicField() The genuine <a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/qqbar.html">algebraic numbers</a>. Computations are exact.</p></li>
<li><p>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 ?</p></li>
<li><p>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.</p></li>
</ul>
<blockquote>
<p>sage: CFF(sqrt(2))^2</p>
<p>[1, 1, 1491038293021224]</p>
</blockquote>
<h4>Floating point numbers:</h4>
<ul>
<li><p>RDF = RealDoubleField() Those are the of <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html">double precision floating numbers</a> the processor. Definitely the fastest, but a rounding is done at each computation. </p></li>
<li><p>RR = RealField() Floating number with arbitrary (still fixed) many bits of
precision. <a href="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</a>
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.</p></li>
<li><p>RLF = RealLazyField() like RealField() but can wait that the user fixes the precision to get one. <a href="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</a></p></li>
</ul>
<blockquote>
<p>sage: a = RLF(pi)</p>
<p>sage: RealField(100)(a+2)</p>
<p>5.1415926535897932384626433833</p>
</blockquote>
<ul>
<li><p>RIF = RealIntervalField() A real number is represented as an interval
containing it, and whose extremities are RealFields elements <a href="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</a></p></li>
<li><p>RLF.interval_field() mixture of the two previous ones.</p></li>
<li><p>sage.rings.real_lazy.LazyAlgebraic</p></li>
</ul>
<h4>Relations between those fields</h4>
<ul>
<li>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.</li>
</ul>
<blockquote>
<p>sage: composite_field(RDF,QQbar)</p>
<p>Complex Double Field</p>
</blockquote>
<ul>
<li><p><a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field_morphisms.html">Some</a>
<a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/maps.html">maps</a>
are also defined to embed an abstract number field into the real line.</p></li>
<li><p>The method <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html">algebraic_dependency()</a> that allows to guess whether a floating number corresponds to some algebraic number of a given degree.</p></li>
</ul>
<p>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 ?</p>
<p>Given an algebraic ...<span class="expander"> <a>(more)</a></span></p></div>http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/?comment=43926#post-id-43926See https://trac.sagemath.org/ticket/15944 for a sample worksheet.Fri, 12 Oct 2018 04:33:16 -0500http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/?comment=43926#post-id-43926Comment by kcrisman for <div class="snippet"><p>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:</p>
<h4>Exact subfields of the reals:</h4>
<ul>
<li><p>QQ = RationalField() The genuine
<a href="http://www.sagemath.org/doc/reference/rings_standard/sage/rings/rational_field.html">rationals</a>.
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.</p></li>
<li><p>NumberField? <a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field.html">number fields</a> described by an irreducible polynomial.</p></li>
<li><p>AA = AlgebraicField() The genuine <a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/qqbar.html">algebraic numbers</a>. Computations are exact.</p></li>
<li><p>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 ?</p></li>
<li><p>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.</p></li>
</ul>
<blockquote>
<p>sage: CFF(sqrt(2))^2</p>
<p>[1, 1, 1491038293021224]</p>
</blockquote>
<h4>Floating point numbers:</h4>
<ul>
<li><p>RDF = RealDoubleField() Those are the of <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html">double precision floating numbers</a> the processor. Definitely the fastest, but a rounding is done at each computation. </p></li>
<li><p>RR = RealField() Floating number with arbitrary (still fixed) many bits of
precision. <a href="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</a>
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.</p></li>
<li><p>RLF = RealLazyField() like RealField() but can wait that the user fixes the precision to get one. <a href="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</a></p></li>
</ul>
<blockquote>
<p>sage: a = RLF(pi)</p>
<p>sage: RealField(100)(a+2)</p>
<p>5.1415926535897932384626433833</p>
</blockquote>
<ul>
<li><p>RIF = RealIntervalField() A real number is represented as an interval
containing it, and whose extremities are RealFields elements <a href="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</a></p></li>
<li><p>RLF.interval_field() mixture of the two previous ones.</p></li>
<li><p>sage.rings.real_lazy.LazyAlgebraic</p></li>
</ul>
<h4>Relations between those fields</h4>
<ul>
<li>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.</li>
</ul>
<blockquote>
<p>sage: composite_field(RDF,QQbar)</p>
<p>Complex Double Field</p>
</blockquote>
<ul>
<li><p><a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field_morphisms.html">Some</a>
<a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/maps.html">maps</a>
are also defined to embed an abstract number field into the real line.</p></li>
<li><p>The method <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html">algebraic_dependency()</a> that allows to guess whether a floating number corresponds to some algebraic number of a given degree.</p></li>
</ul>
<p>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 ?</p>
<p>Given an algebraic ...<span class="expander"> <a>(more)</a></span></p></div>http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/?comment=36807#post-id-36807Apparently there are also `RealLiteral`s, which seem to be none of the above.Fri, 03 Mar 2017 07:51:41 -0600http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/?comment=36807#post-id-36807Comment by Eviatar Bach for <div class="snippet"><p>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:</p>
<h4>Exact subfields of the reals:</h4>
<ul>
<li><p>QQ = RationalField() The genuine
<a href="http://www.sagemath.org/doc/reference/rings_standard/sage/rings/rational_field.html">rationals</a>.
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.</p></li>
<li><p>NumberField? <a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field.html">number fields</a> described by an irreducible polynomial.</p></li>
<li><p>AA = AlgebraicField() The genuine <a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/qqbar.html">algebraic numbers</a>. Computations are exact.</p></li>
<li><p>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 ?</p></li>
<li><p>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.</p></li>
</ul>
<blockquote>
<p>sage: CFF(sqrt(2))^2</p>
<p>[1, 1, 1491038293021224]</p>
</blockquote>
<h4>Floating point numbers:</h4>
<ul>
<li><p>RDF = RealDoubleField() Those are the of <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html">double precision floating numbers</a> the processor. Definitely the fastest, but a rounding is done at each computation. </p></li>
<li><p>RR = RealField() Floating number with arbitrary (still fixed) many bits of
precision. <a href="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</a>
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.</p></li>
<li><p>RLF = RealLazyField() like RealField() but can wait that the user fixes the precision to get one. <a href="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</a></p></li>
</ul>
<blockquote>
<p>sage: a = RLF(pi)</p>
<p>sage: RealField(100)(a+2)</p>
<p>5.1415926535897932384626433833</p>
</blockquote>
<ul>
<li><p>RIF = RealIntervalField() A real number is represented as an interval
containing it, and whose extremities are RealFields elements <a href="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</a></p></li>
<li><p>RLF.interval_field() mixture of the two previous ones.</p></li>
<li><p>sage.rings.real_lazy.LazyAlgebraic</p></li>
</ul>
<h4>Relations between those fields</h4>
<ul>
<li>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.</li>
</ul>
<blockquote>
<p>sage: composite_field(RDF,QQbar)</p>
<p>Complex Double Field</p>
</blockquote>
<ul>
<li><p><a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field_morphisms.html">Some</a>
<a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/maps.html">maps</a>
are also defined to embed an abstract number field into the real line.</p></li>
<li><p>The method <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html">algebraic_dependency()</a> that allows to guess whether a floating number corresponds to some algebraic number of a given degree.</p></li>
</ul>
<p>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 ?</p>
<p>Given an algebraic ...<span class="expander"> <a>(more)</a></span></p></div>http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/?comment=18001#post-id-18001It would be really nice to have a list like this in the documentation.Thu, 28 Mar 2013 12:16:55 -0500http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/?comment=18001#post-id-18001Answer by vdelecroix for <div class="snippet"><p>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:</p>
<h4>Exact subfields of the reals:</h4>
<ul>
<li><p>QQ = RationalField() The genuine
<a href="http://www.sagemath.org/doc/reference/rings_standard/sage/rings/rational_field.html">rationals</a>.
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.</p></li>
<li><p>NumberField? <a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field.html">number fields</a> described by an irreducible polynomial.</p></li>
<li><p>AA = AlgebraicField() The genuine <a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/qqbar.html">algebraic numbers</a>. Computations are exact.</p></li>
<li><p>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 ?</p></li>
<li><p>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.</p></li>
</ul>
<blockquote>
<p>sage: CFF(sqrt(2))^2</p>
<p>[1, 1, 1491038293021224]</p>
</blockquote>
<h4>Floating point numbers:</h4>
<ul>
<li><p>RDF = RealDoubleField() Those are the of <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html">double precision floating numbers</a> the processor. Definitely the fastest, but a rounding is done at each computation. </p></li>
<li><p>RR = RealField() Floating number with arbitrary (still fixed) many bits of
precision. <a href="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</a>
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.</p></li>
<li><p>RLF = RealLazyField() like RealField() but can wait that the user fixes the precision to get one. <a href="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</a></p></li>
</ul>
<blockquote>
<p>sage: a = RLF(pi)</p>
<p>sage: RealField(100)(a+2)</p>
<p>5.1415926535897932384626433833</p>
</blockquote>
<ul>
<li><p>RIF = RealIntervalField() A real number is represented as an interval
containing it, and whose extremities are RealFields elements <a href="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</a></p></li>
<li><p>RLF.interval_field() mixture of the two previous ones.</p></li>
<li><p>sage.rings.real_lazy.LazyAlgebraic</p></li>
</ul>
<h4>Relations between those fields</h4>
<ul>
<li>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.</li>
</ul>
<blockquote>
<p>sage: composite_field(RDF,QQbar)</p>
<p>Complex Double Field</p>
</blockquote>
<ul>
<li><p><a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field_morphisms.html">Some</a>
<a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/maps.html">maps</a>
are also defined to embed an abstract number field into the real line.</p></li>
<li><p>The method <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html">algebraic_dependency()</a> that allows to guess whether a floating number corresponds to some algebraic number of a given degree.</p></li>
</ul>
<p>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 ?</p>
<p>Given an algebraic ...<span class="expander"> <a>(more)</a></span></p></div> http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/?answer=14701#post-id-14701Hello,
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()
6127/5000
sage: a.exact_rational()
2759355491689903/2251799813685248
sage: a.nearby_rational(max_error=0.0001)
87/71
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
3.141592653589794?
sage: pi_rr
3.14159265358979
sage: pi_rlf == pi_rr
True
sage: RLF(pi_rr) == pi_rlf
False
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)
5.792015681006562933?
sage: a.interval_diameter(2**-70)
5.7920156810065629328307508193548308713?
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
0.6931471805599453?
But which does not work quite well:
sage: exp(log2) == 2
False
VincentFri, 29 Mar 2013 00:37:34 -0500http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/?answer=14701#post-id-14701Answer by tmonteil for <div class="snippet"><p>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:</p>
<h4>Exact subfields of the reals:</h4>
<ul>
<li><p>QQ = RationalField() The genuine
<a href="http://www.sagemath.org/doc/reference/rings_standard/sage/rings/rational_field.html">rationals</a>.
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.</p></li>
<li><p>NumberField? <a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field.html">number fields</a> described by an irreducible polynomial.</p></li>
<li><p>AA = AlgebraicField() The genuine <a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/qqbar.html">algebraic numbers</a>. Computations are exact.</p></li>
<li><p>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 ?</p></li>
<li><p>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.</p></li>
</ul>
<blockquote>
<p>sage: CFF(sqrt(2))^2</p>
<p>[1, 1, 1491038293021224]</p>
</blockquote>
<h4>Floating point numbers:</h4>
<ul>
<li><p>RDF = RealDoubleField() Those are the of <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html">double precision floating numbers</a> the processor. Definitely the fastest, but a rounding is done at each computation. </p></li>
<li><p>RR = RealField() Floating number with arbitrary (still fixed) many bits of
precision. <a href="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</a>
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.</p></li>
<li><p>RLF = RealLazyField() like RealField() but can wait that the user fixes the precision to get one. <a href="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</a></p></li>
</ul>
<blockquote>
<p>sage: a = RLF(pi)</p>
<p>sage: RealField(100)(a+2)</p>
<p>5.1415926535897932384626433833</p>
</blockquote>
<ul>
<li><p>RIF = RealIntervalField() A real number is represented as an interval
containing it, and whose extremities are RealFields elements <a href="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</a></p></li>
<li><p>RLF.interval_field() mixture of the two previous ones.</p></li>
<li><p>sage.rings.real_lazy.LazyAlgebraic</p></li>
</ul>
<h4>Relations between those fields</h4>
<ul>
<li>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.</li>
</ul>
<blockquote>
<p>sage: composite_field(RDF,QQbar)</p>
<p>Complex Double Field</p>
</blockquote>
<ul>
<li><p><a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field_morphisms.html">Some</a>
<a href="http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/maps.html">maps</a>
are also defined to embed an abstract number field into the real line.</p></li>
<li><p>The method <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html">algebraic_dependency()</a> that allows to guess whether a floating number corresponds to some algebraic number of a given degree.</p></li>
</ul>
<p>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 ?</p>
<p>Given an algebraic ...<span class="expander"> <a>(more)</a></span></p></div> http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/?answer=14727#post-id-14727Perhaps should we add the Python builtin `float` as well.
Wed, 03 Apr 2013 08:19:55 -0500http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/?answer=14727#post-id-14727