Giter VIP home page Giter VIP logo

Comments (11)

arjenhiemstra avatar arjenhiemstra commented on July 18, 2024 1

that's interesting and unexpected! The change from doubles to floats cause these rounding issues? (I cannot check this because I do not have any devices with these kind of values)

from ithowifi.

TD-er avatar TD-er commented on July 18, 2024 1

Yep sure it can as there are some values which can't be exactly represented as float (or as double)
Only when printing a double, the print function often stops at less decimals than the resolution you can store in a double.
I think the default is 10 decimals for most string conversion functions if no nr of decimals is set.
Given you can store about 18 decimal values in a double, you need to have double values of over 1E9 (and some binary repeating fraction) before you ever encounter these issues.
With floats you can only store ~6 decimals (slightly more).

So you must do the formatting to N decimals yourself.

from ithowifi.

tomkooij avatar tomkooij commented on July 18, 2024 1

The problem seems to arise because ArduinoJson uses doubles internally. Set ‘’ARDUINOJSON_USE_DOUBLE’ to 0

bblanchon/ArduinoJson#1724 (comment)

from ithowifi.

arjenhiemstra avatar arjenhiemstra commented on July 18, 2024 1

Ok, cool stuff, I vaguely remembered something so I went to my release notes and found this:
"fix: correct rounding issues since ArduinoJSON library update"

history repeating itself but then with a different part of the code...

Ok, I would propose the following:

  1. to keep the structs ithoDeviceStatus and ithoDeviceMeasurements to floats and round the values using the existing round() function to 2 (?) decimals?
    That saves 4 bytes memory per instance of the struct, which on some devies with a lot of status labels can make a substantial difference.

to change these lines:

float cur = 0.0;
float min = 0.0;
float max = 0.0;

to doubles again. These would bring back the settings values to the correct values again.

from ithowifi.

tomkooij avatar tomkooij commented on July 18, 2024

Statusvalues I could just round in HomeAssistant, but in settings it is quite annoying:

image

from ithowifi.

TD-er avatar TD-er commented on July 18, 2024

Also if you have similar code like this, you should add a f next to the "magic" value.

 float max = 0.0f; 

Or else there will be an extra double to float conversion in the code. (not always as the compiler sometimes can detect this behavior and optimize for it)

from ithowifi.

tomkooij avatar tomkooij commented on July 18, 2024

I just tried master 6f5a113, sadly, it does not work as expected:

image

from ithowifi.

arjenhiemstra avatar arjenhiemstra commented on July 18, 2024

Thanks for trying! Will look into further

from ithowifi.

arjenhiemstra avatar arjenhiemstra commented on July 18, 2024

Ok, cool stuff, I vaguely remembered something so I went to my release notes and found this: "fix: correct rounding issues since ArduinoJSON library update"

history repeating itself but then with a different part of the code...

Ok, I would propose the following:

  1. to keep the structs ithoDeviceStatus and ithoDeviceMeasurements to floats and round the values using the existing round() function to 2 (?) decimals?
    That saves 4 bytes memory per instance of the struct, which on some devies with a lot of status labels can make a substantial difference.

to change these lines:

float cur = 0.0;
float min = 0.0;
float max = 0.0;

to doubles again. These would bring back the settings values to the correct values again.

my oh my @tomkooij, its even worse, sorry:
#142 (comment)

from ithowifi.

arjenhiemstra avatar arjenhiemstra commented on July 18, 2024

I just tried master 6f5a113, sadly, it does not work as expected:

image

and I think I now what causes it:

float round(float value, int precision)
{
float pow10 = pow(10.0, precision);
return static_cast<int>(value * pow10 + 0.5) / pow10;

this has to be changed to double again also of course!!

from ithowifi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.