Fog Creek Software
Discussion Board




Code Question Sorta

In C++, what is the order of evaluation of the following expression? And what does the modulus accomplish?  (Hour and Minute can be 0 - 24.  The expression is used to determine the degree of the hour hand of a clock given an Hour and Minute in UTC (GMT) time.)

degree = (Hour * 30) % 360 + Minute / 2;

I am thinking it is:

1. (Hour * 30)
2. Minute / 2
3. (#1) % (360)
4. (#3) + (#2)

It's really the % that confuses me.  I don't understand why it's needed.

Clock
Wednesday, February 26, 2003

On page 53 of my copy of K&R, it states that modulus has the same precedence as * and /, and it associates left to right.  So your example function evaluates as:
degree = ((Hour * 30) % 360) + (Minute / 2);
as you suspected.

why don't you just use parentheses if you have doubts about the order?

--sam

petrov
Wednesday, February 26, 2003

Modulus is equivalent to multiplication and division in the order of operations.

It is present to keep the result of the function from being greater than a complete rotation of the circle (360 degrees).

Example: 20 hours, 30 minutes

Degree = ( 20 * 30 ) % 360 + 30 / 2
Degree = (600) % 360 + 15
Degree = 240 + 15
Degree = 255

Without the modulus, the result would be 615 degrees.

Gordo
Wednesday, February 26, 2003

You said that hour can be from 0-24 -- did you mean 0 - 23? Anyway, I'd guess that the % is only there to make sure that degrees never goes above 360. Perhaps the hour-hand-plotting code only handles degrees from 0-360, or is less accurate with higher figures?

Your analysis of the operator precedence seems correct, since "multiplicative" operators (*, /, %) are higher than "arithmetic operators" (-, +). Quick search on google threw up this link:
http://www-agrw.informatik.uni-kl.de/~jmayer/c-operator-precedence.html

Adrian Gilby
Wednesday, February 26, 2003

Wow, two other people replied to this while I was writing my response.

Adrian Gilby
Wednesday, February 26, 2003

Thanks guys.  It now makes sense to me.  The actual line of code is from "Programming Windows Fifth Edition" by Charles Petzold.  It is in the chapter on Timers.  The analog clock program.

Clock
Wednesday, February 26, 2003

"In C++, what is the order of evaluation of the following expression?"

C++, like C, generally leaves the order of evaluation of expressions to the compiler, so the answer to your question is "It depends on your compiler's implementation of the language."

Please notw that order of evaluation and precedence have _nothing_ to do with one another. Given the expression:

a + b * c

a C++ compiler is free to evaluate (i.e. determine the values of) a, b and c in any order it chooses. However, the final value of the expression must be:

(value of b multiplied by value of c) plus value of a

This is a common source of misunderstanding for C++ and C newbies.

Neil Butterworth
Wednesday, February 26, 2003

<nitpick>
"Programming Windows Fifth Edition" by Charles Petzold is all C, no C++.
</nitpick>

Nick H
Wednesday, February 26, 2003

>> "Programming Windows Fifth Edition" by Charles Petzold is all C, no C++.

While that is a true statement, my code is C++.  I never said that Mr. Petzolds code was C++.  I merely stated that is where I got the source line from.  If you look on page 348 you will see it.

My source code file has an extension of .cpp and his end with .c.

Isn't the file extenion one way the compiler determines if the code is C versus C++.  Of course, some compilers may have flags for this.

Clock
Wednesday, February 26, 2003

Flags = (Command Line) Options or Pragma

Clock
Wednesday, February 26, 2003

When I was a kid in school about  7 or 8 years old we got taught 'BODMAS' which stood for:
Brackets
Order (I think)
Division
Multiplication
Addition
Subtraction

meaning that expressions in brackets are evaluated first, then divisions, multiplications, additions and finally subtractions.

Which I think is pretty well how most compilers behave.

Realist
Wednesday, February 26, 2003

So I get

1. (Hour * 30)
2. Minute / 2
3. (#1) % (360)
4. (#3) + (#2)

as well

Realist
Wednesday, February 26, 2003

Neil,

Do you ever get the feeling you are talking to a brick wall?

I agree with you on this. There are no guarantees whatsoever which of the operands will be evaluated first.

Which order the operators are applied relative to each other is more deterministic. In the -given- expression it would be first the * then the % then the / and the + last.

Ed the Millwright
Wednesday, February 26, 2003

In case I wasn't clear, all the rest of you are wrong.

(puts on language lawyer hat)
Page 238 of the K&R and section 17.2 of the ARM both indicate that *, / and % have the same precedence level.
(takes off language lawyer hat)

Ed the Millwright
Wednesday, February 26, 2003

Actually Neil, Gordo and sam all got it right.

Ed the Millwright
Wednesday, February 26, 2003

My 0.02$ for what it's worth:
If you're ever in doubt (as you obviously were in this case) use an extra set (or two) of parantheses!!! It not only works, it also improves readability and maintainability of your code!

Masken
Thursday, February 27, 2003

Isnt this just clearer

(( hour * 60 + minute ) / 2) % 360

simplify
Thursday, February 27, 2003

not to mention it actually keeps the range from 0-359,
versus the original which does not.

simplify
Thursday, February 27, 2003

whoops
actually -> clearly....

simplify
Thursday, February 27, 2003

WHo cares anyway?

Realist
Thursday, February 27, 2003

*  Recent Topics

*  Fog Creek Home