Hello.
Can someone simply explain me strange results I got when adding simple floats? For example:
let x = 200.16;
let y = 60;
console.log(x + y) // logs: 260.15999999999997
How to do it properly. I tried almost everything (changing int (y) into float, parse both variable as floats ect).
Thanks in advance.
Best regards.
Kamil
260.15999999999997
should be considered the same as 260.16
That’s an issue with floating point numbers (and the numbers being stored as binary in a computer)
The .16
would be a decimal that would go on forever in binary digits (much like 1/3 = 0.33333… goes on forever in our base-10 number system) so it can’t be stored perfectly as a binary number in a limited amount of space (which means a limited number of digits).