Skip to content

Commit

Permalink
fix(slider): max value had js floating point issues
Browse files Browse the repository at this point in the history
Sometimes a calculation of max value or number of labels went into JS floating point issues while dividing
  • Loading branch information
lubber-de authored Aug 23, 2023
1 parent 26b15d2 commit 4ae1b43
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/definitions/modules/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@
var
step = module.get.step(),
min = module.get.min(),
quotient = step === 0 ? 0 : Math.floor((settings.max - min) / step),
precision = module.get.precision(),
quotient = step === 0 ? 0 : Math.floor(Math.round(((settings.max - min) / step) * precision) / precision),
remainder = step === 0 ? 0 : (settings.max - min) % step
;

Expand All @@ -684,7 +685,9 @@
return settings.step;
},
numLabels: function () {
var value = Math.round((module.get.max() - module.get.min()) / (module.get.step() === 0 ? 1 : module.get.step()));
var step = module.get.step(),
precision = module.get.precision(),
value = Math.round(((module.get.max() - module.get.min()) / (step === 0 ? 1 : step)) * precision) / precision;
module.debug('Determined that there should be ' + value + ' labels');

return value;
Expand All @@ -699,7 +702,9 @@

switch (settings.labelType) {
case settings.labelTypes.number: {
return Math.round(((value * (module.get.step() === 0 ? 1 : module.get.step())) + module.get.min()) * precision) / precision;
var step = module.get.step();

return Math.round(((value * (step === 0 ? 1 : step)) + module.get.min()) * precision) / precision;
}
case settings.labelTypes.letter: {
return alphabet[value % 26];
Expand Down

0 comments on commit 4ae1b43

Please sign in to comment.