folpindo / arduino

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

Print::printFloat should print "nan" when appropriate #946

Closed GoogleCodeExporter closed 9 years ago

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

1. Set a float to NaN (eg. sqrt (-1) )
2. Print it
3. See 0.00000000

What is the expected output? 

I see 0.00000000. 

What do you see instead?

I expect to see "NaN".

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

IDE 1.0.1. On OS/X.
Board: not relevant

Please provide any additional information below.

Suggest changing Print::printFloat to add, near the start:

  if (isnan (number))
    return print ("NaN");

You may think "well don't do that" (use NaNs). But the problem can be obscured 
by intervening code. eg.

  float a = sqrt (-1);
  float b = 0.03511390;

  Serial.print ("a1 = ");
  Serial.println (a, 8);
  Serial.print ("b = ");
  Serial.println (b, 8);

  a = a + b;

  Serial.print ("a2 = ");
  Serial.println (a, 8); 

The output makes it look like we added zero to a non-zero number, giving zero. 
Which is perplexing for beginners. At least if it printed NaN that is a clue to 
find out how it became NaN.

Original issue reported on code.google.com by n...@gammon.com.au on 4 Jun 2012 at 3:50

GoogleCodeExporter commented 9 years ago
https://github.com/arduino/Arduino/commit/308d907694e30ad470e87433bae7dcb975ea60
6d

I used "nan" instead of "NaN" because that seemed to be the C/C++ standard.

Original comment by dmel...@gmail.com on 5 Jun 2012 at 3:35

GoogleCodeExporter commented 9 years ago
Thanks.

Original comment by n...@gammon.com.au on 5 Jun 2012 at 4:00