Go Down

### Topic: Casting question (Read 991 times)previous topic - next topic

#### __Tango

##### Jun 17, 2011, 03:22 pm
Hey folks, I'm doing some float calculations and something is working in a way i didn't expect and i'm not sure why.

`float wAvg = 5789.5615;unsigned long tDiffMillis = 1011;float distance = 0.01169;void setup() {    Serial.begin ( 115200 );    Serial.println ( ( wAvg * tDiffMillis / ( 3600 * 1000 ) ) / distance, 3 );    Serial.println ( ( wAvg * tDiffMillis / ( 3600000 ) ) / distance, 3 );}void loop() {}`

The first line prints

-111764.601

which is wrong, but the second line (which seems equivalent to me) prints out the correct value of

139.085

I tried this on a linux based machine converting it to C:

`#include <stdio.h>int main ( int argc, char **argv ) {    float wAvg = 5789.5615;    unsigned long tDiffMillis = 1011;    float distance = 0.01169;    printf ( "%.3f\n", ( wAvg * tDiffMillis / ( 3600 * 1000 ) ) / distance );    printf ( "%.3f\n", ( wAvg * tDiffMillis / ( 3600000 ) ) / distance );}`

and the proper value is printed both times.

Can someone help me figure out what i'm missing?

Thanks.

#### PaulS

#1
##### Jun 17, 2011, 03:27 pm
3600 is an int. 1000 is an int. The result of the multiplication is an int.

3600UL * 1000UL should result in the same output as the code with the 3600000 (which I wouldn't have expected to work). The compiler treats literals as ints, and 3600000 does not fit in an int.

#### AWOL

#2
##### Jun 17, 2011, 03:28 pm
You need to remember that an "int" on an Arduino is 16 bits, so 1000 * 3600 = -4480.
A 32 bit Linux machine will have 32 bit "int"s

#### johnwasser

#3
##### Jun 17, 2011, 04:03 pm

33600UL * 1000UL should result in the same output as the code with the 3600000 (which I wouldn't have expected to work). The compiler treats literals as ints, and 3600000 does not fit in an int.

The compiler is smart enough to use a 'long' for integer constants that won't fit in an 'int'.

(Just constants like 3600000, not constant expressions like 3600*1000)
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

#### __Tango

#4
##### Jun 17, 2011, 04:10 pmLast Edit: Jun 17, 2011, 04:12 pm by __Tango Reason: 1
Ah...that's it. I didn't realize the compiler would cast like that.   Thanks all!

Go Up