Skip to content

Instantly share code, notes, and snippets.

@tpps88206
Created November 20, 2020 10:36
Show Gist options
  • Save tpps88206/b4aa6f1fc135a2b2d2f95f307ddff5ca to your computer and use it in GitHub Desktop.
Save tpps88206/b4aa6f1fc135a2b2d2f95f307ddff5ca to your computer and use it in GitHub Desktop.
在 Javascript 中,透過取一個很小的誤差當作容許值,解決 0.1 + 0.2 不等於 0.3 的問題
function equal(n1, n2) {
return Math.abs(n1 - n2) < Number.EPSILON;
}
var a = 0.1 + 0.2;
var b = 0.3;
equal(a, b); // true
equal(0.0000001, 0.0000002); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment