Opened 7 years ago

Closed 7 years ago

# Slider: Cannot reach max value with step of 0.1

Reported by: Owned by: mikedehaan blocker 1.11.3 ui.slider 1.11.2 regression

### Description

I've created a slider with values from 50 to 60 with a 0.1 step. The lower bound works just fine, but the max value is never able to be reached via the slider.

Here's a jsfiddle example:

The slider in the example should go to 60, however it stops at 59.9.

### comment:1 Changed 7 years ago by Scott González

Keywords: regression added none → 1.12.0 minor → blocker new → open Unable to slide to max value when using range slider → Slider: Cannot reach max value with step of 0.1

Confirmed that this regressed in 1.11.2.

### comment:2 follow-up:  4 Changed 7 years ago by dekajp

I assume step should be a integer than a float

```_calculateNewMax: function() {
var remainder = ( this.options.max - this._valueMin() ) % this.options.step;
this.max = this.options.max - remainder;
}
```
Version 0, edited 7 years ago by dekajp (next)

### comment:4 in reply to:  2 ; follow-up:  5 Changed 7 years ago by Kamilche

I think the problem is floating point calculation . in chrome devtools type this 1.005 * 100 = 100.49999999999999

You're right, that's the problem. Modulo isn't mean to be used with floats, apparently. I got around it by multiplying by 100 to get larger numbers, performing the modulo function, then dividing by 100 at the end. Plus I rounded at the end to discard excess precision.

```        _calculateNewMax: function () {
var multiplier = 100;
var remainder = ((this.options.max - this._valueMin()) * multiplier) % (this.options.step * multiplier) / multiplier;
remainder = Math.round((remainder + 0.00001) * multiplier) / multiplier; // Round off excess decimals
this.max = this.options.max - remainder;
},
```

### comment:5 in reply to:  4 Changed 7 years ago by dekajp

Please look at the latest fix in the pull request.

I think the problem is floating point calculation . in chrome devtools type this 1.005 * 100 = 100.49999999999999

You're right, that's the problem. Modulo isn't mean to be used with floats, apparently. I got around it by multiplying by 100 to get larger numbers, performing the modulo function, then dividing by 100 at the end. Plus I rounded at the end to discard excess precision.

```        _calculateNewMax: function () {
var multiplier = 100;
var remainder = ((this.options.max - this._valueMin()) * multiplier) % (this.options.step * multiplier) / multiplier;
remainder = Math.round((remainder + 0.00001) * multiplier) / multiplier; // Round off excess decimals
this.max = this.options.max - remainder;
},
```

### comment:6 Changed 7 years ago by Jyoti Deka

Resolution: → fixed open → closed

Slider: Fix max calculation, when step is float

Fixes #10721 Closes gh-1398

Changeset: ae1d6d5f90236405023964bb3061eccd6c625e39

### comment:7 Changed 7 years ago by Scott González

#11039 is a duplicate of this ticket.

### comment:8 Changed 7 years ago by Jyoti Deka

Slider: Fix max calculation, when step is float

Fixes #10721 Closes gh-1398 (cherry picked from commit ae1d6d5f90236405023964bb3061eccd6c625e39)

Changeset: dfa3a9f8c983f5206d49000a170b42581fcc5478

### comment:9 Changed 7 years ago by Scott González

Milestone: 1.12.0 → 1.11.3
Note: See TracTickets for help on using tickets.