+ 5
Do you care about JS Math problem?
You probably know that JS math is inaccurate - for example, alert(0.1 + 0.2) outputs 0.30000000000000004. So, my qustion is: when you are counting with an number typed by client, do you care about this? If yes, how?
4 ответов
+ 5
Anton Böhler Ole113 Thank you 😊
+ 2
It really shouldnt matter unless you’re doing super exact calculations. If you need a longer float you can use some of the Node.js BigInt librarys.
+ 2
in general a computer will always think that
0.1 + 0.2 == 0.3
is false
(it's not only js😅)
it's because of the way floats are stored...
to go around this you can do sth like this:
0.3 - (0.2 + 0.1) < 0.00001
or
round the two numbers your testing
+ 1
The weird of floating point 😂😂😂
There are some libraries to solve it.
https://github.com/MikeMcl/big.js/wiki
Even a BigInt native type:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
In progress a ECMA proposal to establish a standard, maybe implemented on ES2020, ES2021
https://github.com/tc39/proposal-decimal