sensors-software icon indicating copy to clipboard operation
sensors-software copied to clipboard

Fixed Str2F conversion error in Offsetcalculation

Open Tiefseetauchner opened this issue 2 years ago • 0 comments

Description

When offsetting the temperature by any value between (but not including) -1.0 and 0, the offset would be calculated to the absolute value, introduced in #607

Example

When offsetting by -0.7, the offset would be calculated to 0.7

float r = float(strtol(correction, &pEnd, 10)); returns 0, thus check r >= 0.0f ? 1.0f : -1.0f returns 1.0f, 0.7 would be added instead of subtracted

Reproduction

Reproducable by going into configuration/sensors and changing the temperature offset to anything between -1.0 and 0, the temperature will be offset by the absolute value, not the negative value

Fix

Changed to check for a leading "-" (after spaces) instead, but it requires a loop or using std::string and trimming that, which would be a more major change (possibly trimming the string when it is put into the config would save on resources)

Btw

Just an idea I had, could we use fixed point for everything temperature? The software only reads one digit, the offset only does one digit, it feels like, especially on a microcontroller, we should avoid floats alltogether, because then we get issues like this. Would be quite a rewrite tho, I suspect.

Or, yk, use atof 😉

Tiefseetauchner avatar Jun 30 '22 11:06 Tiefseetauchner