-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use sprintf to output floats in Print/dtostrf #7068
Use sprintf to output floats in Print/dtostrf #7068
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.