+ 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?

24th Aug 2019, 1:29 PM
Dominik Vladař
Dominik Vladař - avatar
4 Antworten
+ 5
Anton Böhler Ole113 Thank you 😊
24th Aug 2019, 2:10 PM
Dominik Vladař
Dominik Vladař - avatar
+ 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.
24th Aug 2019, 1:32 PM
Ole113
Ole113 - avatar
+ 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
24th Aug 2019, 1:48 PM
Anton Böhler
Anton Böhler - avatar
+ 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
26th Jan 2021, 1:36 PM
David Ordás
David Ordás - avatar