Calculation Error? 60*60*1000 is NOT 3600000 ???!

I did already some Ardunio projects, though I am not a hardcore programmer.
Yesterday I discovered an interesting effect which I thought of posting here as this might
occur to more people, I guess.

My code was like this:

unsigned long T0; // Time stamp
unsigned long Rest_L; // remaining time in min
unsigned long Dauer; // Duration
i

void loop(void) {
...

T0 = millis(); // is set when special condition occurs and used as reference for calculation later

.....

Rest_L= ((T0+(60601000) - millis() )/60000); //calculate remaining time in minutes

This didn't work out.
Then I tried to get closer to the problem and isolated this:

Dauer = 60603600;

printed then Dauer to the serial monitor.
And it was't 3600000 to my surprise.

Then I substituted the "60601000" by 3600000 in my comparison equation and it worked.

Rest_L= ((T0+3600000 - millis() )/60000); //calculate remaining time in minutes

I find this behaviour a bit surprising, but probably harcore programmers will laugh.
Nevertheless several newbies might fall into the same trap.
So I thought it might be useful to post it.

Cheers, Peter

Not this one again

60UL * 60UL etc

Yes, this is what I thought as well would be necessary,
So, values not specifically declared are "int"? And this creates the overflow?
Right?

As said, for a newbie this is not easy to get.

Cheers, Peter

all the math that can be done at compile time are done with integer on 16 bits unless told otherwise by using l or ul in your numbers

t = 60*60*1000ul; will do the right thing (you need at least 1 long integer in the formula to get the math done with more bits)

understood.
I kind of foresaw this as I declared my result value already as unsigned long.
Thought this would do it.
But now I know.

Cheers, Peter

Yep - sometimes our wishes or expectations are let down by the cruel reality of the pre-processor or compiler

that being said, the compiler warns me that I'm doing something stupid if I write

unsigned long t = 60*60*1000;

at compile time you would get

warning: integer overflow in expression [-Woverflow]
 unsigned long t = 60 * 60 * 1000;
                           ^

so always look at warning and fix them is a good practice.