Hello. Why when performing the following mathematical operation with float, the result is -0.0?
I don't make sense of the "-" sign.
In the following code you can see the result:
In C++, a floating-point literal defaults to type double. On the smaller Arduino platforms like Uno, double is the same as float. But on ESP32, it's the normal size, 64 bits.
It is a rounding error. The result of your calculation is very close to zero but slightly negative. Hence the - sign.
Remember that 0.1 cannot be represented in float representation as precise as in decimal presentation. Like 1/3 cannot be represented in decimal format (at least not without an infinite amount of threes after the .
You could add an if to test for very small negative value and replace with 0.0...
The IEEE 754 standard for floating-point arithmetic (presently used by most computers and programming languages that support floating-point numbers) requires both +0 and −0. Real arithmetic with signed zeros can be considered a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞; division is undefined only for ±0/±0 and ±∞/±∞.
Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0. The notation "−0" may be used informally to denote a negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines.