void loop(){
Serial.println(a);
delay(500);
}
//----------------------------------------
The result should be 125
But now is 4294967239.
why??? Thank you!
1802125 = 45000. Ints can hold a maximum of 32767, so you get an overflow into a negative value. Casting the value to a long after all the operations makes no difference, but you then assign that negative number to an unsigned long. Because negative values are stores in two's complement form, their resulting positive interpretation ends up being a very large value. Cast your individual values to unsigned long, then none of your operations will overflow.
1802125 = 45000. Ints can hold a maximum of 32767, so you get an overflow into a negative value. Casting the value to a long after all the operations makes no difference, but you then assign that negative number to an unsigned long. Because negative values are stores in two's complement form, their resulting positive interpretation ends up being a very large value. Cast your individual values to unsigned long, then none of your operations will overflow.
[/quote]
BIDMAS. It does division first. 125/360 with integer division is 0. Now 125.0/360.0 = some decimal value.