mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   Programming (https://www.mersenneforum.org/forumdisplay.php?f=29)
-   -   Expression evaluation (https://www.mersenneforum.org/showthread.php?t=24659)

Nick 2019-08-03 09:47

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):
[URL]https://www.nytimes.com/2019/08/02/science/math-equation-pedmas-bemdas-bedmas.html[/URL]

They avoid trying to explain to a general audience that \(2^{2^3}=256\)...

axn 2019-08-03 12:18

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.

xilman 2019-08-03 13:56

[QUOTE=axn;522989]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.[/QUOTE]BODMAS (in UK schools) BEDMAS (in US ditto) removes any ambiguity.

a1call 2019-08-03 14:18

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 2019-08-03 14:35

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.:smile:

a1call 2019-08-03 20:27

[QUOTE=xilman;522992]BODMAS (in UK schools) BEDMAS (in US ditto) removes any ambiguity.[/QUOTE]
My hat off to you sir. It took me a good 2 hours to comprehend what you said.:smile:

ewmayer 2019-08-03 23:52

[QUOTE=a1call;522995]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.[/QUOTE]

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 /?

Kebbaj 2019-08-04 03:02

Reading direction
 
1 Attachment(s)
Reading direction 8÷ 2(2+2).
[url]https://www.mersenneforum.org/showthread.php?p=523038#post523038[/url]

Nick 2019-08-04 07:24

[QUOTE=ewmayer;523039]In related flamebait news, is it good or bad that C gives << and >> different priority than * and /?[/QUOTE]
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;

[/CODE] (cryptonerds will recognize SHA).


All times are UTC. The time now is 23:18.

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