Ask Your Question
1

Difference between RealField and numerical_approx

asked 2016-03-20 05:26:35 +0100

Pretty Antlers gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-03-20 13:19:38 +0100

slelievre gravatar image

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:

http://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/

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

1 follower

Stats

Asked: 2016-03-20 05:26:35 +0100

Seen: 836 times

Last updated: Mar 20 '16