Last active
October 22, 2023 14:57
-
-
Save tientq64/82de744919b39dec835b03385ea152a0 to your computer and use it in GitHub Desktop.
Add 2 numbers, fix floating point problem, example: 0.1 + 0.2, not support Node.js :Đ
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 add(a, b) { | |
if (a === 0) return b; | |
if (b === 0) return a; | |
let sign = 1; | |
if (a < 0 && b < 0) { | |
a = -a; | |
b = -b; | |
sign = -1; | |
} | |
if (b < 0) { | |
[a, b] = [b, a]; | |
} | |
const el = document.createElement('input'); | |
el.type = 'number'; | |
el.min = a; | |
el.value = a; | |
el.step = b; | |
el.stepUp(); | |
return el.valueAsNumber * sign; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment