Ask Your Question
0

PEMDAS unexpected result

asked 2020-03-05 15:45:50 +0200

13negro gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-03-07 17:03:48 +0200

eric_g gravatar image

The 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. 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
edit flag offensive delete link more

Comments

thank you very much.

13negro gravatar image13negro ( 2020-03-08 21:11:10 +0200 )edit

@13negro you can accept the answer by clicking the check mark below the answer's score.

This will mark your question as solved.

slelievre gravatar imageslelievre ( 2021-02-11 00:57:54 +0200 )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

Stats

Asked: 2020-03-05 15:45:50 +0200

Seen: 317 times

Last updated: Mar 08 '20