Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimals are not percise [duplicate]

Tags:

javascript

If I do 0.3 - 0.2 it gives 0.9999999998 not 0.1 is there a way to make it give a percise decimal? Can't trust a calculator if it's not percise.

like image 550
VocalFan Avatar asked Nov 07 '22 03:11

VocalFan


1 Answers

You could try this solution: https://30secondsofinterviews.org/#what-does-0-1-0-2-0-3-evaluate-to-

A solution to this problem would be to use a function that determines if two numbers are approximately equal by defining an error margin (epsilon) value that the difference between two values should be less than.

const approxEqual = (n1, n2, epsilon = 0.0001) => Math.abs(n1 - n2) < epsilon
approxEqual(0.1 + 0.2, 0.3) // true
like image 179
arkhi Avatar answered Nov 14 '22 23:11

arkhi