-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
fix(material/slider): change calculatePercent method, to calculate pr… #24138
Conversation
…opertly when min,max,value are 0 Fixes #23913
@@ -874,6 +874,12 @@ export class MatSlider | |||
|
|||
/** Calculates the percentage of the slider that a value is. */ | |||
private _calculatePercentage(value: number | null) { | |||
// when min,max,value are all zero, the fraction becomes NAN, because we divide 0/0 | |||
// so we have to explicitely return 1 in this case | |||
if (value === 0 && this.min === 0 && this.max === 0) { |
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.
I think we should avoid not only expression 0/0 but division by 0 in general, because If both min and max values are equal to 2 (for instance) then it will path the "if" guard but it will lead to division by 0 and return Infinity which can impact further calculations as well. Also, if we assume that all 3: value, this.min, and this.max
are equal to the same value (2 for instance) then it will also path the guard but eventually, it will end up with 0 / 0 resulting NaN
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.
Yes definitely that makes sense, this guard won't save us from every case.
I'll check between, numerator and dominator against 0 and not on min,max,value
sliderInstance.value = 0; | ||
sliderInstance.min = 0; | ||
sliderInstance.max = 0; | ||
expect(sliderInstance.percent).toBe(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.
This seems weird to me. Shouldn't the percent
be 0 if everything everything is 0?
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.
As I understand percent is how the value is proportional between min and max , so if you have value=0 and the min and max value=0 as well the percent should be 1 not 0, it's like to have value=2 and max=2 as well
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.
I'm thinking about it in terms of what would show up in the UI. As a user, it would be weird if the value was zero but the slider was full.
All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the ℹ️ Googlers: Go here for more info. |
I'm closing this PR. @DMezhenskyi will push it's own version soon. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
…opertly when min,max,value are 0
Fixes #23913