Repository navigation
Discrete range slider has no floating-point rounding errors - #187393
hannah-hyj wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the _lerp calculations in RangeSlider and Slider to handle discrete divisions when widget.divisions is specified, preventing floating-point rounding errors. Corresponding widget tests are added to verify this behavior. The reviewer suggests refactoring both _lerp methods to assign widget.divisions to a local variable, which leverages Dart's type promotion and avoids repeated null-assertion operators.
| double _lerp(double value) { | ||
| if (widget.divisions != null) { | ||
| return widget.min + | ||
| (value * widget.divisions!).round() * (widget.max - widget.min) / widget.divisions!; | ||
| } | ||
| return ui.lerpDouble(widget.min, widget.max, value)!; | ||
| } |
There was a problem hiding this comment.
Instead of repeatedly using the null-assertion operator ! on widget.divisions, we can assign it to a local variable. Dart's type promotion will then automatically promote it to a non-nullable int inside the if block, making the code cleaner and safer.
double _lerp(double value) {
final int? divisions = widget.divisions;
if (divisions != null) {
return widget.min +
(value * divisions).round() * (widget.max - widget.min) / divisions;
}
return ui.lerpDouble(widget.min, widget.max, value)!;
}| double _lerp(double value) { | ||
| assert(value >= 0.0); | ||
| assert(value <= 1.0); | ||
| if (widget.divisions != null) { | ||
| return widget.min + | ||
| (value * widget.divisions!).round() * (widget.max - widget.min) / widget.divisions!; | ||
| } | ||
| return value * (widget.max - widget.min) + widget.min; | ||
| } |
There was a problem hiding this comment.
Instead of repeatedly using the null-assertion operator ! on widget.divisions, we can assign it to a local variable. Dart's type promotion will then automatically promote it to a non-nullable int inside the if block, making the code cleaner and safer.
double _lerp(double value) {
assert(value >= 0.0);
assert(value <= 1.0);
final int? divisions = widget.divisions;
if (divisions != null) {
return widget.min +
(value * divisions).round() * (widget.max - widget.min) / divisions;
}
return value * (widget.max - widget.min) + widget.min;
}|
This pull request has been changed to a draft. The currently pending flutter-gold status will not be able to resolve until a new commit is pushed or the change is marked ready for review again. For more guidance, visit Writing a golden file test for Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
|
I've marked this PR as not ready to port to flutter/packages yet. |
|
This pull request contains changes to Material or Cupertino, which are currently frozen in this repository. Changes should be made in Please refer to #188444 for instructions. |
|
This PR is now ready to port over to flutter/packages! 🥳 |
…and RangeSlider Fixes floating-point rounding errors when calculating values for discrete `Slider` and `RangeSlider`. Ports flutter/flutter#187393 to `material_ui`. Fixes flutter/flutter#39510
…and RangeSlider Fixes floating-point rounding errors when calculating values for discrete `Slider` and `RangeSlider`. Ports flutter/flutter#187393 to `material_ui`. Fixes flutter/flutter#39510
…and RangeSlider (#13022) Fixes floating-point rounding errors when calculating values for discrete `Slider` and `RangeSlider`. Ports flutter/flutter#187393 to `material_ui`. Fixes flutter/flutter#39510 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
fix: #39510
Before:
value1 =29.0/35.0;
value2 =value1* 35.0;
we will get value1 = 29.000000000000004
After: value = 29.0 *35.0/ 35.0
we will get value =29.0; no more floating-point rounding errors;
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.