code8825 / arduino

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

Print::printFloat erroneously prints 0.00 for large numbers. #967

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Execute this function call:   Serial.println(4294967290.0);

What is the expected output? What do you see instead?

I expect to see 4294967290.0 or similar (ie. rounded to 7 digits).

What I see is: 0.00

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

IDE 1.0
OS/X
Uno

Please provide any additional information below.

It appears that the code below in Print::printFloat is responsible:

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;

Because of imprecision in the storing of the floating point number, it is 
actually stored as 4.2949673e9 (4294967300) which is too large to fit into an 
unsigned long.

However surely printing 0.00 is very misleading? The number is certainly not 
zero.

Suggest either detecting this condition and printing "ovf" or similar, or 
amending the function printFloat to print the correct number.

It isn't much use providing a print for floats, which does not in fact handle 
large numbers.

Suggested fix:

if (number > 4294967040.0) return print ("ovf");  // constant determined 
empirically

Original issue reported on code.google.com by n...@gammon.com.au on 27 Jun 2012 at 5:16

GoogleCodeExporter commented 9 years ago
https://github.com/arduino/Arduino/commit/6036846ce311470845d8bb0e69b6dd3a5a86c0
da

We should just fix the function so it can print these numbers, but this seems 
like a reasonable interim fix.

Original comment by dmel...@gmail.com on 29 Jun 2012 at 3:23