mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2019-08-03, 09:47   #1
Nick
 
Nick's Avatar
 
Dec 2012
The Netherlands

22×3×5×29 Posts
Default Expression evaluation

We all know that different processors may give different answers when evaluating -3 mod 2.
But it was news to me that calculators differ in their evaluation of 8÷2(2+2):
https://www.nytimes.com/2019/08/02/s...as-bedmas.html

They avoid trying to explain to a general audience that \(2^{2^3}=256\)...
Nick is online now   Reply With Quote
Old 2019-08-03, 12:18   #2
axn
 
axn's Avatar
 
Jun 2003

23·643 Posts
Default

8÷2(2+2) is an inconsistent notation, mixing explicit division operator and implicit multiplication operator, so naturally there can be differences in the interpretation.
axn is offline   Reply With Quote
Old 2019-08-03, 13:56   #3
xilman
Bamboozled!
 
xilman's Avatar
 
"𒉺𒌌𒇷𒆷𒀭"
May 2003
Down not across

2AC516 Posts
Default

Quote:
Originally Posted by axn View Post
8÷2(2+2) is an inconsistent notation, mixing explicit division operator and implicit multiplication operator, so naturally there can be differences in the interpretation.
BODMAS (in UK schools) BEDMAS (in US ditto) removes any ambiguity.
xilman is online now   Reply With Quote
Old 2019-08-03, 14:18   #4
a1call
 
a1call's Avatar
 
"Rashid Naimi"
Oct 2015
Remote to Here/There

2,141 Posts
Default

Takes away the ambiguity by giving precedence to division over multiplication, which are actually meant to have equal precedence which by original (before democratically acronym based convention) convention should be evaluated left-to-right, whichever comes 1st.
a1call is offline   Reply With Quote
Old 2019-08-03, 14:35   #5
a1call
 
a1call's Avatar
 
"Rashid Naimi"
Oct 2015
Remote to Here/There

2,141 Posts
Default

I think it is ok for the masses to democratically decide who the experts are, but not to democratically decide what the expert-opinion is and leave that part to the experts in the field. Otherwise we get Wikipedia.

Last fiddled with by a1call on 2019-08-03 at 14:36
a1call is offline   Reply With Quote
Old 2019-08-03, 20:27   #6
a1call
 
a1call's Avatar
 
"Rashid Naimi"
Oct 2015
Remote to Here/There

2,141 Posts
Default

Quote:
Originally Posted by xilman View Post
BODMAS (in UK schools) BEDMAS (in US ditto) removes any ambiguity.
My hat off to you sir. It took me a good 2 hours to comprehend what you said.

Last fiddled with by a1call on 2019-08-03 at 20:28
a1call is offline   Reply With Quote
Old 2019-08-03, 23:52   #7
ewmayer
2ω=0
 
ewmayer's Avatar
 
Sep 2002
República de California

101101100010102 Posts
Default

Quote:
Originally Posted by a1call View Post
Takes away the ambiguity by giving precedence to division over multiplication, which are actually meant to have equal precedence which by original (before democratically acronym based convention) convention should be evaluated left-to-right, whichever comes 1st.
No - the article's description of the convention makes clear that D,M and A,S are treated as equal-precedence operation pairs, with left-to-right breaking the resulting ties. Since C lacks an exponentiation operator this related issue does not arise there, but in my college engineering freshman Fortran class, there it was made clear that with multiple exponentiations in sequence, a**b**c (e.g. a^b^c using symbology more familiar to most of our readers), the order of evaluation is instead right-to-left, i.e. the above is interpreted as a^(b^c), so e.g. 2^3^4 gives 2^(3^4) = 2417851639229258349412352, not (2^3)^4 = 4096.

I tested both the expression in the OP and 2^3^4 using Posix bc, it conforms to the PEMDAS, including the above rule for exponentiation.

In related flamebait news, is it good or bad that C gives << and >> different priority than * and /?
ewmayer is offline   Reply With Quote
Old 2019-08-04, 03:02   #8
Kebbaj
 
Kebbaj's Avatar
 
"Kebbaj Reda"
May 2018
Casablanca, Morocco

2·47 Posts
Default Reading direction

Reading direction 8÷ 2(2+2).
https://www.mersenneforum.org/showth...038#post523038
Attached Thumbnails
Click image for larger version

Name:	RL_LR.png
Views:	203
Size:	88.1 KB
ID:	20850  

Last fiddled with by Kebbaj on 2019-08-04 at 03:07
Kebbaj is offline   Reply With Quote
Old 2019-08-04, 07:24   #9
Nick
 
Nick's Avatar
 
Dec 2012
The Netherlands

22·3·5·29 Posts
Default

Quote:
Originally Posted by ewmayer View Post
In related flamebait news, is it good or bad that C gives << and >> different priority than * and /?
It is, at least, easy to remember (coming just before < and >).
In my experience, code involving shifts also uses other bitwise operators so you end up needing brackets anyway, e.g.
Code:
t=(p<<5|p>>27)+(q&r^~q&s)+t+0x5a827999+tedoen[0];q=q<<30|q>>2;
(cryptonerds will recognize SHA).
Nick is online now   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rationality of an expression jnml Miscellaneous Math 7 2018-09-06 15:57
A Fib expression with multiplication MattcAnderson Homework Help 5 2016-11-01 08:16
Theoretical Evaluation of a GPU tapion64 PrimeNet 10 2014-04-09 22:21
regular expression help ixfd64 Programming 2 2009-03-01 06:19
Does Anyone Know how to Simplify the Following Expression? jinydu Puzzles 9 2004-04-02 01:03

All times are UTC. The time now is 07:58.


Sun Oct 17 07:58:08 UTC 2021 up 86 days, 2:27, 0 users, load averages: 1.24, 1.44, 1.39

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.