This for sure is a duplicate, however didn't find an answer quickly so far. When I calculate 0.8 - 0.45 in my node script as variables or directly in the console it results in:
0.35000000000000003
Why? And how can I calculate this correctly?
This for sure is a duplicate, however didn't find an answer quickly so far. When I calculate 0.8 - 0.45 in my node script as variables or directly in the console it results in:
0.35000000000000003
Why? And how can I calculate this correctly?
The problem is the way floats are stored in memory. The result of floating point operation is always an approximation of the expected result.
You should just ignore it. In most languages, when printed the value of a float is truncated.
If you're printing it in node, you can try (.8-.45).toFixed(2)
or however many decimals you'd like
Floating point arithmetic in binary is not a perfect representation of decimal arithmetic a lot of the time. Similarly to decimal mathematics encountering infinite deimals, "irrational numbers" (square root of 2, pi, etc), binary also encounters irrational or otherwise "huge" numbers, sometimes in situations where the decimal number is very short it can be confusing because the operation looks simple enough while the binary representation of the numbers could be extremely large.
https://en.wikipedia.org/wiki/Floating-point_arithmetic
Because the computer only assigns a certain number of bits to the number this means that the number is essentially rounded.
You should never rely on the results of a floating point operation to be perfect, you can however more bits to the variable increasing the precision.