Categories
Computer Science

JavaScript (jQuery AngularJS) Notes

Better way to compare two floats

When you compare two floats, you’d better not directly compare them. Because in the calculation handled by computer, some minor difference will be introduced into infinities. Try to compare floats if they are equal using:

Math.abs(1 / 3 - (1 - 2 / 3)) < 0.0000001; // true