Possible bug in IDE version 0017

Hello,

I have faced weard problem when playing with arduino 0017. When I'm multiplying with 10000 and placing results to variables values are mixed up somehow. This is not the case if I place same values to variables without any calculation. And If I use + and - calculation this problem doesn't occur either.

I'm newbie with arduino so please tell me if this is something basic.

long temppi = 6000;
  Serial.println(temppi);
  temppi = 60000;
  Serial.println(temppi);
  temppi = 600000;
  Serial.println(temppi);
  temppi = 6000000;
  Serial.println(temppi);

Output:

6000
60000
600000
6000000

 long temppi = (1000 * 6);
  Serial.println(temppi);
  temppi = (10000 * 6);
  Serial.println(temppi);
  temppi = (100000 * 6);
  Serial.println(temppi);
  temppi = (1000000 * 6);
  Serial.println(temppi);

Output:
6000
-5536
600000
6000000


  long temppi = 1000 * 6;
  Serial.println(temppi);
  temppi = 0x2170 * 6;
  Serial.println(temppi);
  temppi = 100000 * 6;
  Serial.println(temppi);
  temppi = 1000000 * 6;
  Serial.println(temppi);

Output:

6000
-14176
600000
6000000

  float temppi = 1000 * 6;
  Serial.println(temppi);
  temppi = 0x2170 * 6;
  Serial.println(temppi);
  temppi = 100000 * 6;
  Serial.println(temppi);
  temppi = 1000000 * 6;
  Serial.println(temppi);


Output:

6000.00
-14176.00
600000.00
6000000.00


double temppi = 1000 * 6;
  Serial.println(temppi);
  temppi = 0x2170 * 6;
  Serial.println(temppi);
  temppi = 100000 * 6;
  Serial.println(temppi);
  temppi = 1000000 * 6;
  Serial.println(temppi);

Output

6000.00
-14176.00
600000.00
6000000.00

 double temppi = 10000 * 6;
  temppi = temppi * 10;
  Serial.println(temppi);

Output:

-55360.00

 double temppi = 10000 * 6;
  temppi = temppi / 10;
  Serial.println(temppi);

Output:

-553.60

 double temppi = 10000 * 6;
  temppi = temppi - 10000;
  Serial.println(temppi);

Output:

-15536.00



 double temppi = 10000 * 6;
  Serial.println(temppi);
  temppi = 60000;
  Serial.println(temppi);

Output:
-5536.00
60000.00



  double temppi = 10000 * 6;
  Serial.println(temppi);
  temppi = 59999 + 1;
  Serial.println(temppi);


Output:
-5536.00
60000.00

  double temppi = 10000 * 6;
  Serial.println(temppi);
  temppi = 6 * 10000;
  Serial.println(temppi);
  temppi = 6000 * 10;
  Serial.println(temppi);

-5536.00
-5536.00
-5536.00


  double temppi = 10000 * 6;
  Serial.println(temppi);
  temppi = 2 * 30000;
  Serial.println(temppi);
  temppi = 2 * 5000;
  Serial.println(temppi);

-5536.00
-5536.00
10000.00



  double temppi = 10000 * 6;
  Serial.println(temppi);
  temppi = 2 * 30000;
  Serial.println(temppi);
  temppi = 2 * 5000;
  Serial.println(temppi);
  temppi = 4 * 5000;
  Serial.println(temppi);
  temppi = 6 * 5000;
  Serial.println(temppi);
  temppi = 8 * 5000;
  Serial.println(temppi);
  temppi = 10 * 5000;
  Serial.println(temppi);



-5536.00
-5536.00
10000.00
20000.00
30000.00
-25536.00
-15536.00

I have also tried many types of variables long, float, double and there is always same results regardless variable type.

Have you tried specifying your constants with "l" suffix?

That is all constants longer than the maximum value for an int (32767) should end with an "l" or ("ul" for unsigned long. E.g. 60000 would be 60000l or 60000ul. An usigned int (16-bit) can be specified with "u" sufix only. E.g. 40000u.

One difference between Arduino/AtMega and Windows/Intel is that the AtMega is an 8-bit CPU whereas Intel is 32bit or 64bit. The compiler will typically optimize for the respective bit-size. On the AtMega there is significant overhead involved with doing calculations on anything larger than 8-bits and the compiler will not generate code for this unless you insist (that is why we need to use the suffix notation). On Intel however the opposite is true. 8-bit arithmetic will add overhead whereas 32-bit will not.

I think however the compiler should default to use 32-bit arithmetic for constants that do not fit within 16 bits. This is perhaps something the Arduino team can pass on to the AVR/compiler guys. You're not the first to notice this issue and you will probably not be the last.

Hi BenF,

and thanks for your answer. Now if I try this way:

temppi = (100000ul * 6);
Serial.println(temppi); 

or

temppi = (100000ul * 6ul);
Serial.println(temppi);

or 

temppi = (100000l * 6);
Serial.println(temppi);

Result is always the same: -5536

If I run this sketch:

void setup()
{
  long temppi;

  Serial.begin(57600);
  Serial.println("Test Sketch");

  temppi = (100000ul * 6);
  Serial.println(temppi);

  temppi = (100000ul * 6ul);
  Serial.println(temppi);

  temppi = (100000l * 6);
  Serial.println(temppi);

}

void loop()
{
}

I get this:

Test Sketch
600000
600000
600000

If you run this and get something different you may want to look at your installation and possibly remove/reinstall.

Hi,

I installed Arduino 0017 on my another computer and now this "ul" thing works well :). I have had also some other problems with m first computer. For example Arduino chrashes every now and then.

Thanks for your help! (I definitely have to get new computer)