Pages: [1]   Go Down
Author Topic: Documentation request: operator precedence  (Read 1258 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I notice that the Arduino Reference, here:
http://arduino.cc/en/Reference/HomePage
does not seem to contain any information about operator precedence (at least, not where I could find it easily).

Not knowing about operator precedence led me to make huge bugs.
For example, I used (x&15>9) for intended ((x&15)>9).
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18742
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can Google "C++ operator precedence" but I agree, a mention on the Reference page would be nice.
Logged


Offline Offline
Full Member
***
Karma: 2
Posts: 197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can Google "C++ operator precedence" but I agree, a mention on the Reference page would be nice.

Here is what I am trying to understand:
The Reference page has information on these operators, among others: + - * / % = ==
I see that you explain the use of these operators, rather than assuming that we are familiar with them from having used C++. Also, you volunteer this explanation, rather than simply referring us to a (non-Arduino-specific) C++ reference.
What I don't understand is a philosophy that reckons it worthwhile to explain the use of the operators, but considers it less than obligatory to even mention that these operators have an order of precedence, much less detail what that order is, exactly.
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can Google "C++ operator precedence" but I agree, a mention on the Reference page would be nice.

Here is what I am trying to understand:
The Reference page has information on these operators, among others: + - * / % = ==
I see that you explain the use of these operators, rather than assuming that we are familiar with them from having used C++. Also, you volunteer this explanation, rather than simply referring us to a (non-Arduino-specific) C++ reference.
What I don't understand is a philosophy that reckons it worthwhile to explain the use of the operators, but considers it less than obligatory to even mention that these operators have an order of precedence, much less detail what that order is, exactly.

The order of most operations is the same as normal (PEMDAS), so it's intuitive for those new to arduino. Knowing that the language has them is not. Once you get into bitwise arithmetic you're advanced enough to know where to look it up.
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Just to keep you and anyone you want to share your code with sane, you can use the ( ) as you did to illustrate your confusion in the first post, it doesn't generate any additional code and ensures you get the results you wanted.

Duane B

Logged


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13676
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I double DuaneB,

use as much () as possible or use "simple C notations" by using additional variables.

e.g  if ((x&15)>9)....

becomes

t = x & 15;
if (t > 9)

(x&15>9) for intended
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
or use "simple C notations" by using additional variables

Even better, especially for those occasions when ((x&15)>9) doesn't do what you expected and needs to be debugged.

I assume that the compiler optimises out all of the temporary variables anyway so the resulting upload is the same whether you can read it or not in which case there is no justification for writing condensed code.

Anyone going to volunteer to test the optimisation ?

Duane B
Logged


Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1847
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I notice that the Arduino Reference, here:
http://arduino.cc/en/Reference/HomePage
does not seem to contain any information about operator precedence (at least, not where I could find it easily).

Not knowing about operator precedence led me to make huge bugs.
For example, I used (x&15>9) for intended ((x&15)>9).
This is a place where using the IDE isolates you from the underlying compiler.  GCC actually has a -Wparentheses that warns about this:

Code:
unsigned long foo (unsigned long x)
{
  return (x&15>9);
}

On my Linux system g++ -O2 -Wparentheses gives:

Code:
foo.cc: In function ‘long unsigned int foo(long unsigned int)’:
foo.cc:3:16: warning: suggest parentheses around comparison in operand of ‘&’

Quote
or use "simple C notations" by using additional variables

Even better, especially for those occasions when ((x&15)>9) doesn't do what you expected and needs to be debugged.

I assume that the compiler optimises out all of the temporary variables anyway so the resulting upload is the same whether you can read it or not in which case there is no justification for writing condensed code.

Anyone going to volunteer to test the optimisation ?

Duane B

Assuming the IDE actually passes an optimization option to the compiler, such as -O or -O2, I can state for non-fortran code extra parenthesis won't matter in general.  For Fortran, the language standard actually states that the compiler is not free to reorder expressions with an explicit parenthesis (it matters in some corner cases involving floating point), and modern versions of GCC now have options that say it is ok to rearrange things for speed.

Now, I've been working on GCC for at least 21 years now (with a small break when I was working on other compilers), and even back in the 1.37 era when I joined the project, it would do that level of optimization, assuming you pass at least -O when compiling.  Note, I don't work on the AVR side of things, my day job is the care and feeding of the powerpc backend.  I haven't even looked at what the instruction set of the AVR is, and I don't particularly want to, since my Arduino stuff is a hobby.
Logged

Pages: [1]   Go Up
Jump to: