Use sprintf to output floats in Print/dtostrf#7068
Use sprintf to output floats in Print/dtostrf#7068earlephilhower merged 2 commits intoesp8266:masterfrom
Conversation
Fixes esp8266#7043 Two slightly different custom routines were implemented by hand in dtostrf (an AVR-lib non-ISO function) and Print. This resulted in inconsistent output of float/double vars when rounding was needed. Replace them all with a call to sprintf(), removing the duplicated, not quite correct code. Print(String(float)) and Print(float) now generate the same output.
|
The example code in the original bug (with an INF and NAN added for checks) now reports the same results no matter if it's Print(float) or Print(String(float)): |
| // make sure the string is terminated | ||
| *out = 0; | ||
| char fmt[32]; | ||
| sprintf(fmt, "%%%d.%df", width, prec); |
There was a problem hiding this comment.
Might need to be labelled as breaking? See
https://github.com/arendst/Tasmota/blob/1598e6227a31be5f27081c5a660d22d21613c0ca/platformio.ini#L83
https://github.com/arendst/Tasmota/blob/development/pio/strip-floats.py
https://github.com/xoseperez/espurna/blob/dev/code/scripts/espurna_utils/float_support.py
It removes -u _printf_float from linker flags, so this might come as a surprise b/c code uses dtostrf routinely :)
There was a problem hiding this comment.
Ah, crap. Thank you for reminding me about that! I had a niggle about it, but it's been maybe 2 years since the whole -u print_float stuff.
I'll open a new issue. The solution is not clear to me now.
Fixes #7043
Two slightly different custom routines were implemented by hand in
dtostrf (an AVR-lib non-ISO function) and Print. This resulted in
inconsistent output of float/double vars when rounding was needed.
Replace them all with a call to sprintf(), removing the duplicated, not
quite correct code.
Print(String(float)) and Print(float) now generate the same output.