# Casting question

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.

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.

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

PaulS: 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)

Ah…that’s it. I didn’t realize the compiler would cast like that. Thanks all! 