printFloat bug

defmacro

New member
Hi Paul,

I found the following weird behaviour in Teensyduino, I guess this is a bug. A sample source code to reproduce the bug is:

PHP:
#include <Arduino.h>

void setup() {
  int c = Serial.print(123.45);
  Serial.println();
  Serial.println(c);
}

void loop() {
}

Running this program yields the following result:

PHP:
123.45
3

I don't think this is the correct result. Since the first line has 6 characters, the second line should be 6 instead of 3, isn't it?

I tracked the source code to printFloat function in Print.cpp file, this function has 2 variables named count, I think the reason is that these 2 variables are messed up.

Let me know what you think, thanks a lot!
 
In many years, I don't think I ever used or depended on the return value from print or printf or sprintf, et al.
The Arduino comment on the return value suggests (fuzzy) that the return would be how many ASCII chars were sent, inluding \n or \n\r etc. The number of bytes for end of line might vary by platform/library.

To pretty-print, column-aligned, etc. I'd use sprintf() and a format spec like "%2f".
 
Last edited:
To pretty-print, column-aligned, etc. I'd use sprintf() and a format spec like "%2f".
That will work for Teensy 3.0/3.1. It will not work for Teensy 2.0/2.0++, since the printf libraries are compiled without floating point support in AVR, and sprintf %.2f is not available.
 
Back
Top