Ask Your Question
2

Why do computations with pi default to symbolic?

asked 2019-07-19 23:58:44 +0100

dsejas gravatar image

updated 2019-07-20 06:29:17 +0100

Hello, Sage community,

It is known fact that SageMath uses symbolic computations by default. For example,

sqrt(8)

returns 2*sqrt(2). But it is also known that this behavior can be easily changed using decimals, like in the following case:

sqrt(8.0)

which returns 2.82842712474619.

However, this technique doesn't seem to work with symbolic constants like pi and e. For example,

80 / pi

returns the expected 80/pi, but

80.0 / pi

returns 80.0000000000000/pi instead of the numerical value 25.4647908947033.

I was wondering why is this the case.

Thanks in advance for your answers!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2019-07-20 11:07:19 +0100

tmonteil gravatar image

updated 2019-07-20 12:16:07 +0100

When you write sqrt(8.0), Sage calls the sqrt method of the floating-point number 8.0 (an element of RR). Hence you get a floating-point number. As you noticed in your first example, the sqrt method for (non square) integers returns elements of the symbolic ring SR.

When you write 80.0 / pi, Sage uses coercion (look at the manual for more details about this important concept in Sage) : it first searches for the common parent between floating-point 8.0 and symbolic pi, which is the symbolic ring, it transforms the two elements in this parent, and does the division there. So, in that case, 8.0 is first transformed into a symbolic 8.0 and then divided by the symbolic pi, which results in an element of the symbolic ring.

If you want the division between two floating-point numbers, and get a floating-point number, you can use the floating-point number version of $\pi$, which is RR.pi():

sage: 8.0 / RR.pi()
2.54647908947033

Note that pi is nothing else than SR.pi(), you can redefine pi = RR.pi() if you want to type things more easily.

edit flag offensive delete link more

Comments

Thank you very much @tmonteil. I didn't consider coercion in this scheme. You're right. Thanks!

dsejas gravatar imagedsejas ( 2019-07-20 14:56:39 +0100 )edit

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: 2019-07-19 23:58:44 +0100

Seen: 895 times

Last updated: Jul 20 '19