Created
November 20, 2020 10:36
-
-
Save tpps88206/b4aa6f1fc135a2b2d2f95f307ddff5ca to your computer and use it in GitHub Desktop.
在 Javascript 中,透過取一個很小的誤差當作容許值,解決 0.1 + 0.2 不等於 0.3 的問題
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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