-2

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?

haemse
  • 3,971
  • 5
  • 28
  • 40

3 Answers3

1

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.

TescoOne
  • 315
  • 1
  • 6
0

If you're printing it in node, you can try (.8-.45).toFixed(2) or however many decimals you'd like

Retebitall
  • 475
  • 5
  • 8
0

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.

bgld
  • 31
  • 5
  • Also, just "ignoring it" isn't a good way to conduct your business haha. You should take the time out to learn why it happens and how you can avoid the problems that can arise from floating point precision. – bgld Nov 09 '17 at 17:22