Hi folks,
I'm doing some long (32 bit) math.
unsigned long int tt = 365 * 24 * 60 * 60 ;
Serial.println(tt) ;
yields 13184, should be 31536000.
With static cast it calculates correctly.
tt = (unsigned long int) 365 * (unsigned long int) 24 * (unsigned long int) 60 * (unsigned long int) 60 ;
Serial.println(tt) ;
seems that the interim math is 16 bit not 32 bit.
I would have thought that gcc would have given me a warning about this. (Are we not running with -Wall? )
This particular problem was easy to find/fix but this sort of problem can make one question the foundations and general slow development down.
Can I pass -Wall (or some other recommended switches) to gcc so that nasties are flagged? or other suggestions?
Thx,
Bruce
-- code --
#include <Time.h>
#include <stdio.h>
void setup() {
Serial.begin(9600);
unsigned long int tt = 365 * 24 * 60 * 60 ;
time_t time = 365 * 24 * 60 * 60 ;
Serial.println("Init");
Serial.println(time) ;
Serial.println(tt) ;
tt=60 ;
Serial.println(tt) ;
tt*=60 ;
Serial.println(tt) ;
tt*=24 ;
Serial.println(tt) ;
tt*=365 ;
Serial.println(tt) ;
tt = (unsigned long int) 365 * (unsigned long int) 24 * (unsigned long int) 60 * (unsigned long int) 60 ;
Serial.println(tt) ;
}
void loop() {
delay(1000) ;
}
-- serial port --
Init
13184
13184
60
3600
86400
31536000
31536000