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

5×353 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 offline   Reply With Quote
Old 2019-08-03, 12:18   #2
axn
 
axn's Avatar
 
Jun 2003

124378 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 online now   Reply With Quote
Old 2019-08-03, 13:56   #3
xilman
Bamboozled!
 
xilman's Avatar
 
"๐’‰บ๐’ŒŒ๐’‡ท๐’†ท๐’€ญ"
May 2003
Down not across

1150310 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

24·11·13 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

24·11·13 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

43608 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

267408 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

32×11 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:	245
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

5×353 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 offline   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:45.


Wed Oct 5 07:45:19 UTC 2022 up 48 days, 5:13, 0 users, load averages: 1.08, 1.38, 1.45

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, 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.

โ‰  ยฑ โˆ“ รท ร— ยท โˆ’ โˆš โ€ฐ โŠ— โŠ• โŠ– โŠ˜ โŠ™ โ‰ค โ‰ฅ โ‰ฆ โ‰ง โ‰จ โ‰ฉ โ‰บ โ‰ป โ‰ผ โ‰ฝ โŠ โŠ โŠ‘ โŠ’ ยฒ ยณ ยฐ
โˆ  โˆŸ ยฐ โ‰… ~ โ€– โŸ‚ โซ›
โ‰ก โ‰œ โ‰ˆ โˆ โˆž โ‰ช โ‰ซ โŒŠโŒ‹ โŒˆโŒ‰ โˆ˜ โˆ โˆ โˆ‘ โˆง โˆจ โˆฉ โˆช โจ€ โŠ• โŠ— ๐–• ๐–– ๐–— โŠฒ โŠณ
โˆ… โˆ– โˆ โ†ฆ โ†ฃ โˆฉ โˆช โŠ† โŠ‚ โŠ„ โŠŠ โŠ‡ โŠƒ โŠ… โŠ‹ โŠ– โˆˆ โˆ‰ โˆ‹ โˆŒ โ„• โ„ค โ„š โ„ โ„‚ โ„ต โ„ถ โ„ท โ„ธ ๐“Ÿ
ยฌ โˆจ โˆง โŠ• โ†’ โ† โ‡’ โ‡ โ‡” โˆ€ โˆƒ โˆ„ โˆด โˆต โŠค โŠฅ โŠข โŠจ โซค โŠฃ โ€ฆ โ‹ฏ โ‹ฎ โ‹ฐ โ‹ฑ
โˆซ โˆฌ โˆญ โˆฎ โˆฏ โˆฐ โˆ‡ โˆ† ฮด โˆ‚ โ„ฑ โ„’ โ„“
๐›ข๐›ผ ๐›ฃ๐›ฝ ๐›ค๐›พ ๐›ฅ๐›ฟ ๐›ฆ๐œ€๐œ– ๐›ง๐œ ๐›จ๐œ‚ ๐›ฉ๐œƒ๐œ— ๐›ช๐œ„ ๐›ซ๐œ… ๐›ฌ๐œ† ๐›ญ๐œ‡ ๐›ฎ๐œˆ ๐›ฏ๐œ‰ ๐›ฐ๐œŠ ๐›ฑ๐œ‹ ๐›ฒ๐œŒ ๐›ด๐œŽ๐œ ๐›ต๐œ ๐›ถ๐œ ๐›ท๐œ™๐œ‘ ๐›ธ๐œ’ ๐›น๐œ“ ๐›บ๐œ”