ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 11 Feb 2021 00:57:54 +0100PEMDAS unexpected resulthttps://ask.sagemath.org/question/50172/pemdas-unexpected-result/ I have entered the following expression in sagemath 9.0 on Linux
(3^2 * 4^2)-(8^2 - 3^2)//(6^2-9*8)
and sage gives 146 as the result.
Nevertheless, I solved on paper and I get 145.
So my question is Am i doing something wrong with PEMDAS on sagemath?
Thank you in advance.Thu, 05 Mar 2020 15:45:50 +0100https://ask.sagemath.org/question/50172/pemdas-unexpected-result/Answer by eric_g for <p>I have entered the following expression in sagemath 9.0 on Linux</p>
<p>(3^2 * 4^2)-(8^2 - 3^2)//(6^2-9*8)</p>
<p>and sage gives 146 as the result.
Nevertheless, I solved on paper and I get 145.</p>
<p>So my question is Am i doing something wrong with PEMDAS on sagemath?</p>
<p>Thank you in advance.</p>
https://ask.sagemath.org/question/50172/pemdas-unexpected-result/?answer=50181#post-id-50181The behavior you observe is due to the floor division `//` having a higher precedence than the subtraction operator `-` but a lower precedence than the negative operator `-`, see the [table of operator precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence). Compare:
sage: a = 8^2 - 3^2; a
55
sage: b = 6^2 - 9*8; b
-36
sage: n(a/b)
-1.52777777777778
sage: a//b
-2
sage: 0 - a//b # subtraction operator
2
sage: - a//b # negative operator
1
sage: 0 + (- a//b) # different from 0 - a//b
1
Sat, 07 Mar 2020 17:03:48 +0100https://ask.sagemath.org/question/50172/pemdas-unexpected-result/?answer=50181#post-id-50181Comment by slelievre for <p>The behavior you observe is due to the floor division <code>//</code> having a higher precedence than the subtraction operator <code>-</code> but a lower precedence than the negative operator <code>-</code>, see the <a href="https://docs.python.org/3/reference/expressions.html#operator-precedence">table of operator precedence</a>. Compare:</p>
<pre><code>sage: a = 8^2 - 3^2; a
55
sage: b = 6^2 - 9*8; b
-36
sage: n(a/b)
-1.52777777777778
sage: a//b
-2
sage: 0 - a//b # subtraction operator
2
sage: - a//b # negative operator
1
sage: 0 + (- a//b) # different from 0 - a//b
1
</code></pre>
https://ask.sagemath.org/question/50172/pemdas-unexpected-result/?comment=55665#post-id-55665@13negro you can accept the answer by clicking the check mark below the answer's score.
This will mark your question as solved.Thu, 11 Feb 2021 00:57:54 +0100https://ask.sagemath.org/question/50172/pemdas-unexpected-result/?comment=55665#post-id-55665Comment by 13negro for <p>The behavior you observe is due to the floor division <code>//</code> having a higher precedence than the subtraction operator <code>-</code> but a lower precedence than the negative operator <code>-</code>, see the <a href="https://docs.python.org/3/reference/expressions.html#operator-precedence">table of operator precedence</a>. Compare:</p>
<pre><code>sage: a = 8^2 - 3^2; a
55
sage: b = 6^2 - 9*8; b
-36
sage: n(a/b)
-1.52777777777778
sage: a//b
-2
sage: 0 - a//b # subtraction operator
2
sage: - a//b # negative operator
1
sage: 0 + (- a//b) # different from 0 - a//b
1
</code></pre>
https://ask.sagemath.org/question/50172/pemdas-unexpected-result/?comment=50190#post-id-50190thank you very much.Sun, 08 Mar 2020 21:11:10 +0100https://ask.sagemath.org/question/50172/pemdas-unexpected-result/?comment=50190#post-id-50190