#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
Keywords: | regression added |
---|---|
Milestone: | none → 1.12.0 |
Priority: | minor → blocker |
Status: | new → open |
Summary: | Unable to slide to max value when using range slider → Slider: Cannot reach max value with step of 0.1 |
comment:2 follow-up: 4 Changed 3 years ago by
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
comment:4 follow-up: 5 Changed 3 years ago by
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 Changed 3 years ago by
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
Resolution: | → fixed |
---|---|
Status: | open → closed |
Slider: Fix max calculation, when step is float
Fixes #10721 Closes gh-1398
Changeset: ae1d6d5f90236405023964bb3061eccd6c625e39
comment:8 Changed 3 years ago by
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
Milestone: | 1.12.0 → 1.11.3 |
---|
Confirmed that this regressed in 1.11.2.