Skip to content
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

Closed
wants to merge 1 commit into from
Closed

fix(material/slider): change calculatePercent method, to calculate pr… #24138

wants to merge 1 commit into from

Conversation

lukonik
Copy link

@lukonik lukonik commented Dec 27, 2021

…opertly when min,max,value are 0

Fixes #23913

@lukonik lukonik requested a review from mmalerba as a code owner December 27, 2021 17:41
@@ -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) {
Copy link
Contributor

@DMezhenskyi DMezhenskyi Dec 27, 2021

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.maxare 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

Copy link
Author

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);
Copy link
Member

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?

Copy link
Author

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

Copy link
Member

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.

@googlebot
Copy link

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 @googlebot I consent. in this pull request.

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 cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added the cla: no PR author must sign Google's Contributor License Agreement: https://opensource.google.com/docs/cla label Dec 29, 2021
@lukonik
Copy link
Author

lukonik commented Dec 29, 2021

I'm closing this PR. @DMezhenskyi will push it's own version soon.
thx for feedback

@lukonik lukonik closed this Dec 29, 2021
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: no PR author must sign Google's Contributor License Agreement: https://opensource.google.com/docs/cla
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(mat-slider): Max value not updating correctly when new value is 0
4 participants