# 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?

edit retag close merge delete

Sort by » oldest newest most voted

The 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:

more