Skip to content

Commit

Permalink
[RangeSlider] Resolve issues that crash when assigning a large value …
Browse files Browse the repository at this point in the history
…to `valueTo`

Resolves #3979

GIT_ORIGIN_REV_ID=d09a42095cda8342b1f2a0fdf3a9ab9899cace45
PiperOrigin-RevId: 613196003

(cherry picked from commit ac77b4c)
  • Loading branch information
Park-SM authored and hunterstich committed Mar 21, 2024
1 parent 10484df commit 4d1b9e5
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Expand Up @@ -30,6 +30,7 @@
import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.math.MathContext.DECIMAL64;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
Expand Down Expand Up @@ -101,7 +102,6 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -606,14 +606,18 @@ private void validateValueTo() {

private boolean valueLandsOnTick(float value) {
// Check that the value is a multiple of stepSize given the offset of valueFrom.
return isMultipleOfStepSize(value - valueFrom);
double result =
new BigDecimal(Float.toString(value))
.subtract(new BigDecimal(Float.toString(valueFrom)), DECIMAL64)
.doubleValue();
return isMultipleOfStepSize(result);
}

private boolean isMultipleOfStepSize(float value) {
private boolean isMultipleOfStepSize(double value) {
// We're using BigDecimal here to avoid floating point rounding errors.
double result =
new BigDecimal(Float.toString(value))
.divide(new BigDecimal(Float.toString(stepSize)), MathContext.DECIMAL64)
new BigDecimal(Double.toString(value))
.divide(new BigDecimal(Float.toString(stepSize)), DECIMAL64)
.doubleValue();

// If the result is a whole number, it means the value is a multiple of stepSize.
Expand Down

0 comments on commit 4d1b9e5

Please sign in to comment.