Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Strange dummy float bug #61

Closed
clabnet opened this issue Nov 28, 2020 · 5 comments
Closed

Strange dummy float bug #61

clabnet opened this issue Nov 28, 2020 · 5 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@clabnet
Copy link

clabnet commented Nov 28, 2020

Hi Maakbaas,

I have applied your default config for testing.
Specifying a field as dummyFloat 1-2, I can correctly insert values as: 1, 2 NOT 5.
But I would insert 1.3. The result is 1.29999....
or 1.2 the result is 1.20000.....
esp8266-iotf-dummyfloat-bug

@maakbaas
Copy link
Owner

This is not a bug, but is to be expected. A 32-bit floating point datatype can not store exact representations of all numbers, and therefore it will be stored as the closest binary representation.

See this calculator to play around with this effect:
https://www.binaryconvert.com/convert_float.html

One way to resolve this is to add a new option into the JSON that specifies the number of decimals that should be shown in the interface, and to which the numbers should be rounded. I think that could be a nice addition, so for that reason I will leave this issue open. If anyone wants to create a pull request for this functionality that would be more than welcome.

@clabnet
Copy link
Author

clabnet commented Nov 28, 2020

I learning a lot of things from you.

@maakbaas maakbaas added enhancement New feature or request good first issue Good for newcomers labels Nov 29, 2020
@FSCSoft
Copy link

FSCSoft commented Nov 29, 2020

Hello friend, it's me again and I think what the solution would be instead of converting to pass a string in websoket and maybe in api dash set of json format with only the name and value of each object { name: dumyFloat, value: 2.1},{...} etc. example or maybe to reduce characters {dumy: {value: 2.2}, input {value: true}, etc}. to pass the data in real values ​​and not go through the conversion and it is already an ArduinoJSON dependency of some libs of the project. sorry for my bad English

@maakbaas
Copy link
Owner

Thanks for your input. I can agree that this makes sense from a front-end and web point of view, but it does not make sense from the embedded side of things. Parsing JSON on the ESP8266 is much more expensive than converting JSON to binary in the front-end. In the second case, which is how it is implemented now, the conversion to binary is done by your computer or smartphone, which has more than enough resources to do this simple conversion from JavaScript object to binary format.

If we would choose to send JSON data over the Websocket, the ESP8266 would need to parse this JSON data and store it in a C structure. This will cost more memory and computational power on the ESP8266, where it is much more limited. With the current implementation, all that the ESP8266 needs to do is a single memory copy action.

maakbaas added a commit that referenced this issue Feb 17, 2021
@maakbaas
Copy link
Owner

Added this functionality in 13988ca :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants