cac0ns3c / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

Long Int Multiply Error in Windows Arduino 019 version #383

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Declare a long-int 
2.Multiply 7500 by 8 and store result in long int previously declared
3.print out the result using println or Serial.println

What is the expected output? What do you see instead?
expected value is 60000 actual value is a negative number. (which screws up the 
while loop that tries to count it)

I noted earlier that an Integer that was loaded with a result higher than 32767 
also failed the while loop. This is obviously a range violation but the IDE 
didn't catch it.

What version of the Arduino software are you using? On what operating
system?  Which Arduino board are you using?

0019 on Windows 2000 I am using the Duemilanove/Nano w ATmega328 setting
to program an Ardweeny from Solarbotics. 

Please provide any additional information below.

My guess is that there is a bug in the implementation of Long Integer math, 
that sets the sign bit, when the derived value of the expression exceeds the 
size of a normal integer. That this passes the validity check, is disturbing. I 
also note that Serial.println cannot print String+LongIntValue+String type data 
and dumps garbage on the screen when it tries.

Original issue reported on code.google.com by grysm...@telus.net on 20 Oct 2010 at 11:41

GoogleCodeExporter commented 9 years ago
The multiplication is done in the data types being multiplied, regardless of 
the data type to which the result will be stored.  Try 7500L * 8 or (long) 7500 
* 8.

Original comment by dmel...@gmail.com on 17 Nov 2010 at 5:09