system
August 17, 2012, 11:03am
1
Look at this simple arithmetic line:
long icr1_count = 16000000 / (20000 * 2);
Initially, the "20000" is a defined constant in a macro, I changed it to the real value to debug.
I used Serial.print() to see the value of icr1_count , and its value is: -626
It is negative.
Now I changed the line, I typecasted it:
long icr1_count = 16000000 / (long)(20000 * 2);
The serial monitor says: icr1_count = -626
It was still wrong.
I then tried this:
long icr1_count = 16000000 / ((long)20000 * 2);
Serial monitor says: icr1_count = 400
Can you please explain what just happened?
Thanks. =)
system
August 17, 2012, 11:12am
2
Can you please explain what just happened?
Signed 16 bit arithmetic happened.
long icr1_count = 16000000L/ (20000L * 2)
system
August 17, 2012, 11:30am
3
AWOL:
Can you please explain what just happened?
Signed 16 bit arithmetic happened.
long icr1_count = 16000000L/ (20000L * 2)
Well, that is obvious. Also is this a valid notation in C: 20000L ? This is the first time I've seen it..
Then if this works, why does typecasting (20000 * 2) didn't work?
system
August 17, 2012, 11:32am
4
Also is this a valid notation in C: 20000L ?
Yes, that's why I showed it to you.
why does typecasting (20000 * 2) didn't work?
Because 40000 > 32767
system
August 17, 2012, 11:41am
5
Ah, so intermediate calculation values are stored as int by default? OK=)
Thanks =)
Intermediate calculation values on ints are stored handled (not necessarily stored) as int.
system
August 18, 2012, 11:40am
7
Well, when they are handled, the values reside in the memory right? So i think "stored" is also correct. Everything are stored in the registers, its just how the cpu interpret them.
system
August 18, 2012, 1:51pm
8
wakoko79:
Well, when they are handled, the values reside in the memory right? So i think "stored" is also correct. Everything are stored in the registers, its just how the cpu interpret them.
It's only semantics, but 'handled' is the more correct term. In other words, what code is generated in order to evaluate the expression you coded.