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

Clarify Math.abs return value description #35684

Merged
merged 3 commits into from
Sep 2, 2024
Merged

Conversation

thegiantbeast
Copy link
Contributor

@thegiantbeast thegiantbeast commented Sep 1, 2024

Description

The Math.abs return value description isn't clear, and there was already a previous issue open because of it (#21526).

Motivation

To describe the actual returning value in a more clear way.
Although the explanation in #21526 is correct, the way it's described is not clear at all. So I hope with this PR it can bring some clarity to the matter.

Additional details

Related issues and pull requests

Relates to #21526

@thegiantbeast thegiantbeast requested a review from a team as a code owner September 1, 2024 23:33
@thegiantbeast thegiantbeast requested review from Josh-Cena and removed request for a team September 1, 2024 23:33
@github-actions github-actions bot added Content:JS JavaScript docs size/xs [PR only] 0-5 LoC changed labels Sep 1, 2024
Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes its meaning. Math.abs(-0) returns 0, not -0. -0 and 0 are two distinct values.

@Josh-Cena
Copy link
Member

This PR basically just removes a sentence. Why does the extra sentence make this less clear?

@thegiantbeast
Copy link
Contributor Author

Because when people read it, it sounds like Math.abs(-1) === -1 which is not correct. And I understand that from a mathematical point-of-view are different, but from a validation one, this is true: Math.abs(-0) === 0

@Josh-Cena
Copy link
Member

Josh-Cena commented Sep 2, 2024

Because when people read it, it sounds like Math.abs(-1) === -1 which is not correct.

I don't understand how that conclusion can be reached from "If x is negative (including -0), returns -x." People are going to misread things but that doesn't mean we should remove it. I'm fine to clarify it in whatever way, but this PR in essence removes it altogether and only keeps the second sentence: "The result is therefore always a positive number or 0." so I fail to see how it's clearer.

And I understand that from a mathematical point-of-view are different, but from a validation one, this is true: Math.abs(-0) === 0

Nope, from a mathematical view, -0 and 0 are equal. They are distinct in floating point. The reason -0 === 0 is because === has this special behavior of conflating -0 with 0. You can try Object.is(-0, 0) and you will get false.

Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

Copy link
Contributor

github-actions bot commented Sep 2, 2024

@Josh-Cena Josh-Cena merged commit dc90e6a into mdn:main Sep 2, 2024
8 checks passed
@thegiantbeast thegiantbeast deleted the patch-1 branch September 2, 2024 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs size/xs [PR only] 0-5 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants