Skip to content

Commit 4d1b9e5

Browse files
Park-SMhunterstich
authored andcommittedMar 21, 2024
[RangeSlider] Resolve issues that crash when assigning a large value to valueTo
Resolves #3979 GIT_ORIGIN_REV_ID=d09a42095cda8342b1f2a0fdf3a9ab9899cace45 PiperOrigin-RevId: 613196003 (cherry picked from commit ac77b4c)
1 parent 10484df commit 4d1b9e5

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed
 

‎lib/java/com/google/android/material/slider/BaseSlider.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static java.lang.Math.abs;
3131
import static java.lang.Math.max;
3232
import static java.lang.Math.min;
33+
import static java.math.MathContext.DECIMAL64;
3334

3435
import android.animation.Animator;
3536
import android.animation.AnimatorListenerAdapter;
@@ -101,7 +102,6 @@
101102
import java.lang.annotation.Retention;
102103
import java.lang.annotation.RetentionPolicy;
103104
import java.math.BigDecimal;
104-
import java.math.MathContext;
105105
import java.util.ArrayList;
106106
import java.util.Collections;
107107
import java.util.Iterator;
@@ -606,14 +606,18 @@ private void validateValueTo() {
606606

607607
private boolean valueLandsOnTick(float value) {
608608
// Check that the value is a multiple of stepSize given the offset of valueFrom.
609-
return isMultipleOfStepSize(value - valueFrom);
609+
double result =
610+
new BigDecimal(Float.toString(value))
611+
.subtract(new BigDecimal(Float.toString(valueFrom)), DECIMAL64)
612+
.doubleValue();
613+
return isMultipleOfStepSize(result);
610614
}
611615

612-
private boolean isMultipleOfStepSize(float value) {
616+
private boolean isMultipleOfStepSize(double value) {
613617
// We're using BigDecimal here to avoid floating point rounding errors.
614618
double result =
615-
new BigDecimal(Float.toString(value))
616-
.divide(new BigDecimal(Float.toString(stepSize)), MathContext.DECIMAL64)
619+
new BigDecimal(Double.toString(value))
620+
.divide(new BigDecimal(Float.toString(stepSize)), DECIMAL64)
617621
.doubleValue();
618622

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

0 commit comments

Comments
 (0)
Please sign in to comment.