Check the numbers again, I almost made the same mistake.
Not sure why unsigned works and signed does not, but math is usually done with integers, so you may need to tell the compiler that at least one of the numbers is a long for the multiplication.
Whaaaaaa????
2,147,483,647 is SMALLER THAN 2,457,840
On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This >yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).
Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from >-2,147,483,648 to 2,147,483,647.
I tried using long and I got the wrong answer as @Uknod , but that doesn't explain why.
Ofcourse unsigned long worked;
Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won’t store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).
I get the wrong answer using unsigned long, although it is positive because an unsigned number can never be negative. The compiler gives a warning of overflow regardless of the use of long or unsigned long.
Yes, thats what I thought, I tohought I was loosing it there, so why long doesn;t work is a mystery, but like a lt of things arduino, you just have to go with what works, unsigned long works so I'm happy.
int x = 0;
long y = 0;
unsigned long z = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
x = 532 * 4620;
Serial.println(" equation => variable = 532 * 4620 ");
Serial.println(" ");
Serial.print(" int x ");
Serial.println(x);
y = 532 * 4620;
Serial.print(" long y ");
Serial.println(y);
z = 532 * 4620;
Serial.print(" unsigned long z ");
Serial.println(z);
Serial.println("=============================================");
delay(2000);
}
And my unsigned long == 4294934768
equation => variable = 532 * 4620
int x -32528
long y -32528
unsigned long z 4294934768
int x = 0;
long y = 0;
unsigned long z = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
x = 532 * 4620;
Serial.println(" equation => variable = 532 * 4620 ");
Serial.println(" ");
Serial.print(" int x ");
Serial.println(x);
y = 532 * 4620;
Serial.print(" long y ");
Serial.println(y);
z = 532 * 4620;
Serial.print(" unsigned long z ");
Serial.println(z);
z = 532L * 4620L;
Serial.println(" z = 532L * 4620L ");
Serial.print(" unsigned long z ");
Serial.println(z);
Serial.println("=============================================");
delay(2000);
}
Output;
equation => variable = 532 * 4620
int x -32528
long y -32528
unsigned long z 4294934768
z = 532L * 4620L
unsigned long z 2457840