-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Don't show unexpected decimals for range slider value #6699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking at this! It'd be helpful to make sure we know the root cause so we can identify the best fix. More inline.
@@ -92,7 +93,13 @@ public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) | |||
|
|||
private void setUpActualValueLabel(BigDecimal actualValue) { | |||
if (actualValue != null) { | |||
currentValue.setText(String.valueOf(actualValue.doubleValue())); | |||
double doubleValue; | |||
if (slider.getStepSize() < 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's nothing special about a step size less than 1 as far as I can tell! Floating point precision issues can happen with any step sizes that involve decimal values. I don't know how likely this is but it would be possible to have a step size of 1.3, for example.
Have you been able to determine the underlying issue? I originally thought it was an arithmetic issue but looking at the example on the forum, I'm not sure about that anymore. Is it possible that we are calling BigDecimal(double)
somewhere? If so, that could be the issue and we might need to call BigDecimal(string)
instead. See https://stackoverflow.com/questions/46828044/bigdecimal-not-giving-exact-output-in-java
I think that's the most likely culprit and that it can be fixed upstream of this method. If you determine that it is in fact an arithmetic issue, then the setScale
approach may be appropriate. I think we'd want to use the scale of the stepsize instead of hardcoding it to 3, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for alerting me to the root cause of the issue being the BigDecimal
created in RangeWidgetUtils
by getActualValue
, which is currently called from both RangeDecimalWidget
and RangeIntegerWidget
.
Since the code of these classes is more or less identical, it can be pushed up into superclass RangeBaseWidget
with a constructor parameter to switch the detail differences.
This enables getActualValue
just to pass through the float received from the slider, and setUpActualValueLabel
can then be adjusted to suit.
Fixes #6424
Why is this the best possible solution? Were any other approaches considered?
Code improvement seems to fix issue; see Design detail below.
How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?
Issue behaviour has disappeared; passes new and all existing tests.
Do we need any specific form for testing your changes? If so, please attach one.
6424.xml.txt demonstrates issue and fix.
Does this change require updates to documentation? If so, please file an issue here and include the link below.
No.
Before submitting this PR, please make sure you have:
./gradlew connectedAndroidTest
(or./gradlew testLab
) and confirmed all checks still passDateFormatsTest
Design detail
In
RangeDecimalWidget
,setUpActualValueLabel
checks the sliderstepSize
and if less than 1 appliesBigDecimal
.setScale
toactualValue
; parameternewScale
is set to 3 to allow a one-eighth step ie 0.125.In
RangeDecimalWidgetTest
,setup
enables trapping the issue by settinggetRangeStep
to return 0.1, which doesn't affect any of the existing tests.New
changingSliderValueToAnIntermediate_setsTheValueCorrectly
is modelled onchangingSliderValueToAnyOtherThanTheMinOne_setsTheValueCorrectly
. By calling newclickOnFractionalValue
inSliderExt
it sets a value that traps the issue.