ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 20 Jul 2019 07:56:39 -0500Why do computations with pi default to symbolic?https://ask.sagemath.org/question/47217/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.
Thanks in advance for your answers!Fri, 19 Jul 2019 16:58:44 -0500https://ask.sagemath.org/question/47217/why-do-computations-with-pi-default-to-symbolic/Answer by tmonteil for <p>Hello, Sage community,</p>
<p>It is known fact that SageMath uses symbolic computations by default. For example,</p>
<pre><code>sqrt(8)
</code></pre>
<p>returns <code>2*sqrt(2)</code>. But it is also known that this behavior can be easily changed using decimals, like in the following case:</p>
<pre><code>sqrt(8.0)
</code></pre>
<p>which returns <code>2.82842712474619</code>.</p>
<p>However, this technique doesn't seem to work with symbolic constants like <code>pi</code> and <code>e</code>. For example,</p>
<pre><code>80 / pi
</code></pre>
<p>returns the expected <code>80/pi</code>, but</p>
<pre><code>80.0 / pi
</code></pre>
<p>returns <code>80.0000000000000/pi</code> instead of the numerical value <code>25.4647908947033</code>.</p>
<p>I was wondering why is this the case.</p>
<p>Thanks in advance for your answers!</p>
https://ask.sagemath.org/question/47217/why-do-computations-with-pi-default-to-symbolic/?answer=47223#post-id-47223When 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.Sat, 20 Jul 2019 04:07:19 -0500https://ask.sagemath.org/question/47217/why-do-computations-with-pi-default-to-symbolic/?answer=47223#post-id-47223Comment by dsejas for <p>When you write <code>sqrt(8.0)</code>, Sage calls the <code>sqrt</code> method of the floating-point number 8.0 (an element of <code>RR</code>). Hence you get a floating-point number. As you noticed in your first example, the <code>sqrt</code> method for (non square) integers returns elements of the symbolic ring <code>SR</code>.</p>
<p>When you write <code>80.0 / pi</code>, Sage uses <em>coercion</em> (look at the manual for more details about this important concept in Sage) : it first searches for the common parent between floating-point <code>8.0</code> and symbolic <code>pi</code>, which is the symbolic ring, it transforms the two elements in this parent, and does the division there. So, in that case, <code>8.0</code> is first transformed into a symbolic <code>8.0</code> and then divided by the symbolic <code>pi</code>, which results in an element of the symbolic ring.</p>
<p>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 <code>RR.pi()</code>:</p>
<pre><code>sage: 8.0 / RR.pi()
2.54647908947033
</code></pre>
<p>Note that <code>pi</code> is nothing else than <code>SR.pi()</code>, you can redefine <code>pi = RR.pi()</code> if you want to type things more easily.</p>
https://ask.sagemath.org/question/47217/why-do-computations-with-pi-default-to-symbolic/?comment=47224#post-id-47224Thank you very much @tmonteil. I didn't consider coercion in this scheme. You're right. Thanks!Sat, 20 Jul 2019 07:56:39 -0500https://ask.sagemath.org/question/47217/why-do-computations-with-pi-default-to-symbolic/?comment=47224#post-id-47224