Why do computations with pi default to symbolic?

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.

edit retag close merge delete

Sort by » oldest newest most voted

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.

more

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

( 2019-07-20 07:56:39 -0600 )edit