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.Sun, 20 Mar 2016 13:19:38 +0100Difference between RealField and numerical_approxhttps://ask.sagemath.org/question/32834/difference-between-realfield-and-numerical_approx/What is the difference between using RealField, as in
sage: RealField(10).pi()
3.1
and `numerical_approx`, aka `n`, as in
sage: pi().n(10)
3.1
Are they actually the same function under the hood or should one be used over the other in some cases?Sun, 20 Mar 2016 05:26:35 +0100https://ask.sagemath.org/question/32834/difference-between-realfield-and-numerical_approx/Answer by slelievre for <p>What is the difference between using RealField, as in</p>
<pre><code>sage: RealField(10).pi()
3.1
</code></pre>
<p>and <code>numerical_approx</code>, aka <code>n</code>, as in</p>
<pre><code>sage: pi().n(10)
3.1
</code></pre>
<p>Are they actually the same function under the hood or should one be used over the other in some cases?</p>
https://ask.sagemath.org/question/32834/difference-between-realfield-and-numerical_approx/?answer=32835#post-id-32835The method `.N()` or `.n()` is a shortcut for `.numerical_approx()`
and will try to give you a numerical approximation that lives in `RR`
which is a shortcut for `RealField()`.
If you specify a precision, `.N(10)` or `.n(10)` or `.numerical_approx(10)`
give you a numerical approximation that lives in `RealField(10)`. Here,
the precision `10` specifies the number of bits of precision.
You can also specify the number of (base ten) digits of precision:
by using `.N(digits=10)` or `.n(digits=10)` or `.numerical_approx(digits=10)`.
So the answer to your question is yes, the two things you are using are
doing the same thing.
Note that the fastest floating-point real numbers in Sage are `RDF`,
short for `RealDoubleField`.
So in general, I would advise to compute in `RDF`, use `RDF`'s pi
sage: pi_n = RDF.pi()
and map everything you need to compute with into `RDF`.
See a more detailed comparison of all approximations of the real field
in SageMath in this ask-sage question:
[http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/](http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/)
Sun, 20 Mar 2016 13:19:38 +0100https://ask.sagemath.org/question/32834/difference-between-realfield-and-numerical_approx/?answer=32835#post-id-32835