Precedence of Logical Operators

K

Keith Thompson

Ben Pfaff said:
I once had a teacher deduct points for using "n * (n + 1) / 2" as
the sum of 1...n, because she didn't know that it was a correct
formula. I had to demonstrate to her and the entire class that
it was correct.

Suppose you're using a system where int is 16 bits, and n==255. The
intermediate expression 255*256 yields 65280, which overflows, though
the final result is 32640, which doesn't. The formula invokes
undefined behavior, whereas adding the numbers 1..n individually
wouldn't. Similar examples exist for other sizes of int.

A workaround is to divide either n or n+1, whichever is even, by 2
before doing the multiplication:

n%2==0 ? (n/2) * (n+1) : n * (n+1)/2
 
S

Skarmander

Skarmander said:
Adding precedence, as many programming languages do, has the
practical advantage of write down some expressions with less
parentheses, but it's simply not a universal convention.
Wow, that sentence seriously no grammar. I trust everyone still got the
gist of it...

S.
 
O

Old Wolf

Ben said:
I once had a teacher deduct points for using "n * (n + 1) / 2" as
the sum of 1...n, because she didn't know that it was a correct
formula. I had to demonstrate to her and the entire class that
it was correct.

Oh my gauss.

In primary school, a (very blonde) teacher posed an equation
similar to this:

4 * 3 + 2 + 9 - 4 + 17 * 2 + 3 * 0

and asserted that the answer was 0, because anything times zero
is zero. When I demonstrated otherwise on my calculator, she said
that my calculator must be wrong.
 
A

August Karlstrom

Old said:
Oh my gauss.

In primary school, a (very blonde) teacher posed an equation

I suppose you mean "expression" (I'm currently in nitpicking mode ;-)
similar to this:

4 * 3 + 2 + 9 - 4 + 17 * 2 + 3 * 0

and asserted that the answer was 0, because anything times zero
is zero. When I demonstrated otherwise on my calculator, she said
that my calculator must be wrong.


August
 
M

Malcolm

August Karlstrom said:
#include <stdbool.h>

bool a, b, c, d;
int x, y, z, u;

int main(void)
{
d = a || b && c; gibberish
u = x + y * z;
sensible expression anyone with a basic understanding of mathematical
conventions can read
return 0;
}

$ gcc -Wall test.c
test.c: In function ‘main’:
test.c:8: warning: suggest parentheses around && within ||

</shell-session>

For sake of consistency, why not suggest parentheses around `y * z' as
well? (which would be absurd)
If you submit gibberish code to the compiler you should get warnings out.
Nothing wrong with that.
 
A

August Karlstrom

Malcolm said:
sensible expression anyone with a basic understanding of mathematical
conventions can read

That's a ridiculous motivation for using extra parentheses. As we all
know, there are numerous examples of where C does not follow
mathematical conventions. In mathematics `x = x + 1' is an equation with
no solutions and 1/2 does not equal 0 etc. In C the symbols && and ||
are different from ∧ and ∨ and they use short-circuit evaluation, so
they are already quite different from their mathematical counterpart.

Moreover, in basically all imperative languages I have looked at (ADA is
an exception), `and' has higher precedence than `or', so my example
above is certainly not "gibberish". It's rather a matter of personal
preference.


August
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top