Skip to content

Commit

Permalink
Fix for floating point additon problem with JS when adding up variati…
Browse files Browse the repository at this point in the history
…on weights
  • Loading branch information
Auz committed Mar 14, 2022
1 parent 3972b22 commit 7f172eb
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/front-end/services/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ export function validateFeatureRule(
let totalWeight = 0;
ruleValues.forEach((val, i) => {
if (val.weight < 0) throw new Error("Percents cannot be negative");
totalWeight += val.weight;
// without this rounding here, JS floating point messes up simple addition.
totalWeight = Math.round((val.weight + totalWeight) * 100000) / 100000;
isValidValue(valueType, val.value, "Value #" + (i + 1));
});
if (totalWeight > 1) {
Expand Down

0 comments on commit 7f172eb

Please sign in to comment.