-
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
[BUG]: Rounding Values when Using String Type Cast #7043
Comments
Maybe the problem is located in
|
check our implementation of It may need some fixes and optimizations. |
Here you find my test file: |
@d-a-v do you have an idea? |
This boils down to imprecision with floating point numbers. The output discrepancy is because Even the PC will generate 1014 as output when printing 1014.5:
We should fix both Print and String (well, dtostrf) to just use |
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.
Hi,
|
Try again with |
Hi earlhilpower, have you tested the following?
rounding from 2 to 1 digit does't work too...
|
Again, please read up on IEEE floating point imprecision. FP is not base-10, it's base-2, so numbers that look nice and clean can not be exactly represented by the format. That's why applications dealing with money would use binary-coded decimal (BCD) or other tricks to ensure exact representation of base-10 numbers. |
Hi, ok yes this shows the behavior:
|
Basic Infos
Platform
Settings in IDE
Problem Description
When using a type cast to cast an float to a String like:
String(pressure, 0)
the result is not rounded correctly.If I make a type cast like this:
String(pressure, 1)
the result is rounded correctly.MCVE Sketch
Debug Messages
The text was updated successfully, but these errors were encountered: