SmartGridready / SGrPython

Development of SGr PyPi package
Other
1 stars 1 forks source link

Float value is converted to int in sgr_encode #36

Open d1msk1y opened 6 days ago

d1msk1y commented 6 days ago

In payload decode in sgr_encode, all values, even the float ones, are converted to int: https://github.com/SmartGridready/SGrPython/blob/b838830850a0643f24849d567e9f13de69ef0b2b/sgr_library/payload_decoder.py#L95

Of course, I tried removing the rounding for the float cases, which did resolve the issue, however the written float values, when read, are not as precise, as they are before the encoding.

For example: With now resolved float to int conversion, when I write 3.6, I get 3.5999999046325684 when I try to read it with datapoint.read() https://github.com/SmartGridready/SGrPython/blob/b838830850a0643f24849d567e9f13de69ef0b2b/sgr_library/modbus_interface.py#L47 As far as I know, this behavior is common for float values, due to the way they are built, as they don't always truly represent the exact decimal value

So the best solution I have came up with so far, is simply rounding the precise digits

round(await data_point.read(), 2)

However, this way we have some limitations regarding the precision of the value

robin-schoch commented 4 days ago

this is kind of the limitation of floating point numbers. I do not think that we could do anything about it.

I do not think that the library should handle those imprecision. Furthermore I think the library should not round floating point values at all.

How are these imprecision limiting you right now?