Is this a bug of Arduino Compiler

#define THERMAL_RUNAWAY_PERIOD 40
unsigned long startTime, currentTime;

void setup() {
  // put your setup code here, to run once:
  startTime = millis();
  Serial.begin(9000);
}

void loop() {
  // put your main code here, to run repeatedly:
  currentTime = millis();
  if((currentTime - startTime) > ( THERMAL_RUNAWAY_PERIOD * 1000))
  {
    Serial.println("Done!");
  } else {
    Serial.println("wait");
    delay(500);
  }

}

Can any one try this short code to see if it can jump into the if condition? with my board, it never work.

As I printout the THERMAL_RUNAWAY_PERIOD *1000, it's huge random number.

I use Arduino 1.6.5 mac 64 bit version

The trouble is that an integer constant is treated as an int by default. The highest number that can be expressed by an int is 32,767 so 40000 overflows and becomes -25536. Then when the int is compared to the unsigned long in

if((currentTime - startTime) > ( THERMAL_RUNAWAY_PERIOD * 1000))

-25536 is converted to unsigned long which is 4294941760. So your code will actually jump into the if condition but it will take over 49 days to do so. You can make your code work right by changing the line to:

if((currentTime - startTime) > ( THERMAL_RUNAWAY_PERIOD * 1000UL))

which changes the data type to unsigned long. Or change the first line to
const unsigned long THERMAL_RUNAWAY_PERIOD = 40;You can find more information on this here: Integer Constants - Arduino Reference

it's huge random number.

No, it's not random, it is absolutely repeatable.

Did you really mean this?

  Serial.begin(9000);

It is possible, but usually people want this:

  Serial.begin(9600);

PLUS everything that pert and AWOL wrote.

As a general rule you should assume that the compiler is never wrong. It is infinitely more likely that the problem is in your code (or mine). Wondering if the compiler is wrong is a waste of time.

Of course it is possible that there are bugs in any piece of software but compilers have been used so much that you would be extremely unfortunate to come across one.

...R

Thank you all who gave me the reply.

I believe (somewhere) there is test code that compiler writers use to test all the compiler features. GCC has probably been through that testing.

Google "validation suite"