Bug in Print::printFloat

jrossouw

New member
Hi,

I think I have come across a bug in Print.cpp in the printFloat method. When printing float values the size returned only seems to include the digits before the decimal point. The (default) two digits after the decimal point, and the decimal point itself is not counted. For instance printing the float 123.45 returns a size of 3, not 6!

I have tried this on the teensy 3.1 using Teensyduino V1.19

Regards,
Johan
 
Found the issue. The count variable name is reused inside the loop printing the digits. I have changed the code to use a different name inside the loop and now it works correctly:

Code:
        // Print the decimal point, but only if there are digits beyond
        if (digits > 0) {
                uint8_t n, buf[8], [B]cnt[/B]=1;
                buf[0] = '.';

                // Extract digits from the remainder one at a time
                if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;

                while (digits-- > 0) {
                        remainder *= 10.0;
                        n = (uint8_t)(remainder);
                        buf[[B]cnt[/B]++] = '0' + n;
                        remainder -= n;
                }
                count += write(buf, [B]cnt[/B]);
        }
 
Last edited:
Back
Top