multiplication error using long variable
i have variable greater 32,767 can't use int type. instead use long, strange error when multiplying long variable.
for example, variable called 'product' , @ point set product = 1087 * 44. expect product = 47828 instead product =-17708!
what going on here?
here sample code shows problem i'm encountering:
if way correct result however:
am using incorrect syntax when write product=1087*44; ?
for example, variable called 'product' , @ point set product = 1087 * 44. expect product = 47828 instead product =-17708!
what going on here?
here sample code shows problem i'm encountering:
code: [select]
long product = 0;
void setup(){
serial.begin(9600);
}
void loop(){
product = 1087*44; //this should return 47828 instead returns -17708
serial.print("1087 * 44 = ");
serial.println(product);
delay(5000);
}
if way correct result however:
code: [select]
long product = 0;
void setup(){
serial.begin(9600);
}
void loop(){
product = 1087;
serial.print(product);
serial.print(" * ");
serial.print(44);
product=product*44;
serial.print(" = ");
serial.println(product);
delay(5000);
}
am using incorrect syntax when write product=1087*44; ?
the problem both 1087 , 44 integers, multiplication done integers (which go 32,767), result (-17708) stored in long. to around this, can declare integer constants longs appending l, e.g.:
code: [select]
product = 1087l * 44l;
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > multiplication error using long variable
arduino
Comments
Post a Comment