Skip to content

Instantly share code, notes, and snippets.

@NV
Last active August 29, 2015 13:57
Show Gist options
  • Save NV/9613722 to your computer and use it in GitHub Desktop.
Save NV/9613722 to your computer and use it in GitHub Desktop.
Precise sum of float numbers.
function sumFloats(floats) {
var power = floats.reduce(function(acc, current) {
var power = getPrecision(current.toString());
return Math.max(power, acc);
}, 0);
var sum = floats.reduce(function(prev, current) {
return prev + current;
}, 0);
var precision = Math.pow(10, power);
return Math.round(sum * precision) / precision;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment