Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#10721 closed bug (fixed)

Slider: Cannot reach max value with step of 0.1

Reported by: mikedehaan Owned by:
Priority: blocker Milestone: 1.11.3
Component: ui.slider Version: 1.11.2
Keywords: regression Cc:
Blocked by: Blocking:

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:

http://jsfiddle.net/v13t8cmj/2/

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

Change History (9)

comment:1 Changed 3 years ago by Scott González

Keywords: regression added
Milestone: none1.12.0
Priority: minorblocker
Status: newopen
Summary: Unable to slide to max value when using range sliderSlider: Cannot reach max value with step of 0.1

Confirmed that this regressed in 1.11.2.

comment:2 Changed 3 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;
}

reference : https://github.com/dekajp/jquery-ui/commit/8932df74a70ea2e91cd7a6eb2f3c546f7f8c7597

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

Last edited 3 years ago by dekajp (previous) (diff)

comment:4 in reply to:  2 ; Changed 3 years ago by Kamilche

Replying to dekajp:

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 3 years ago by dekajp

Please look at the latest fix in the pull request.

https://github.com/jquery/jquery-ui/pull/1398#issuecomment-68391087

Replying to Kamilche:

Replying to dekajp:

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 3 years ago by Jyoti Deka

Resolution: fixed
Status: openclosed

Slider: Fix max calculation, when step is float

Fixes #10721 Closes gh-1398

Changeset: ae1d6d5f90236405023964bb3061eccd6c625e39

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

#11039 is a duplicate of this ticket.

comment:8 Changed 3 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 3 years ago by Scott González

Milestone: 1.12.01.11.3
Note: See TracTickets for help on using tickets.