Pages: [1]   Go Down
Author Topic: %= operator missing in docs  (Read 1727 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I noticed all the other "compound operators" are listed, but %= is mysteriously missing.  However, my code using it appears to compile without issue, so I do believe it's there.
Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

er
dons steel hat
pops head over parapet

%= wot that do then?
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5597
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

er
dons steel hat
pops head over parapet

%= wot that do then?

% is the modulo operator (returns the remainder of a division); thus if x = 5 % 2, then x = 1.

So, lets say you set x = 8, then you do x %= 3 (prefix notation) - then x = 2.

Ultimately, I think the OP is reading more into the manual than there is; in standard C/C++, there isn't anything called "compound operators", just operators. These operators can be combined with the assignment operator (=) in a pre- or post-fix format. I think part of the confusion is the fact that the manual only touches on this, but doesn't go in depth as would a proper tutorial on ANSI C/C++, which leads to this confusion (on this point and others - such as "missing" or "hidden" functions which are a part of the standard library) for those without an understanding of the base language.

Orum: You might do yourself a favor and look into general ANSI C/C++ tutorials on the internet and/or in dead-tree form; such will only assist you in your understanding of the Arduino as a whole...

Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ahh nods sagely
guess it's one of those things that you'll need one day

think I'll stick to "easier-to-read" rather than save a byte or two

thanks for the explanation though!
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5597
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

ahh nods sagely
guess it's one of those things that you'll need one day

think I'll stick to "easier-to-read" rather than save a byte or two

thanks for the explanation though!

First off, I must apologize - I made a mistake: The notation "{var} {operator}= {value}" is actually a form of an assignment operation, and has nothing to do with pre- and post-fix notation, which is "{operator} {operator}{var}" and "{var}{operator} {operator}", respectively. For example:

Code:
int x = 0;

x += 1; // assignment, x = 1
++x; // pre-fix notation, x = 2
x++; // post-fix notation, x = 3

...however, you should know and understand pre- and post-fix notation, and how they work - because they can both be useful tools, even if they aren't necessarily as easy to understand. For instance, compare this example:

Code:
int i = 0;
while (i++ < 2) {
  serial.println(i);
}

...vs...

Code:
int i = 0;
while (++i < 2) {
  serial.println(i);
}

When you understand why these example don't give the same results, you will hopefully understand how pre- and post-fix notation can be useful...

 smiley
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, the main reason I brought it up was that all the other similar operators (+=, -=, *=, etc.) are listed, and %= is strangely missing.  Normally who are familiar with the operator, myself included, just assume it is there.  I was puzzled though when every other operator was listed, but %= was not, which is why I hope for the inclusion of %=.
Logged

Offline Offline
Full Member
***
Karma: 11
Posts: 147
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


think I'll stick to "easier-to-read" rather than save a byte or two

Note that the bytes you save are only in your source code. Both should compile to the same object code.  There is no performance benefit to the more compact version.

There's a lot to be said for easier to read. I've been programming for 30 years and "x%=3" would make me stop and scratch my heat.
Logged

Pages: [1]   Go Up
Jump to: