Skip to content

Instantly share code, notes, and snippets.

@tientq64
Last active October 22, 2023 14:57
Show Gist options
  • Save tientq64/82de744919b39dec835b03385ea152a0 to your computer and use it in GitHub Desktop.
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 :Đ
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