Skip to content

Documentation for {f32, f64}::hypot should clarify its behavior for negative values. #88944

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

Closed
rosekunkel opened this issue Sep 14, 2021 · 2 comments · Fixed by #110298
Closed
Assignees
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@rosekunkel
Copy link

rosekunkel commented Sep 14, 2021

The documentation for the f32::hypot / f64::hypot function reads

Calculates the length of the hypotenuse of a right-angle triangle given legs of length x and y.

and gives this example (and the analogous example for f64):

let x = 2.0f32;
let y = 3.0f32;

 // sqrt(x^2 + y^2)
let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();

assert!(abs_difference <= f32::EPSILON);

Since lengths are always positive, it's unclear what this function does for negative values. My expectation is that it gives the hypotenuse of the right triangle whose leg lengths are the absolute values of x and y, but I had to look up the relevant libc documentation to be sure, which reads

These functions return sqrt (x*x + y*y). This is the length of the hypotenuse of a right triangle with sides of length x and y, or the distance of the point (x, y) from the origin.

@GuillaumeGomez GuillaumeGomez added A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Sep 17, 2021
@ChiefMilesEdgeworth
Copy link
Contributor

This looks relatively simple, and I'd like to contribute. Could I take this issue?

@jmaargh
Copy link
Contributor

jmaargh commented Apr 13, 2023

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 14, 2023
…ses, r=thomcc

Cover edge cases for {f32, f64}.hypot() docs

Fixes rust-lang#88944

The Euclidean distance is a more general way to express what these functions do, and covers the edge cases of zero and negative inputs.

Does not cover the case of non-normal input values (as the [POSIX docs](https://pubs.opengroup.org/onlinepubs/9699919799.2008edition/) do), but the docs for the rest of the functions in these modules do not address this, I assumed it was not desired.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 14, 2023
…ses, r=thomcc

Cover edge cases for {f32, f64}.hypot() docs

Fixes rust-lang#88944

The Euclidean distance is a more general way to express what these functions do, and covers the edge cases of zero and negative inputs.

Does not cover the case of non-normal input values (as the [POSIX docs](https://pubs.opengroup.org/onlinepubs/9699919799.2008edition/) do), but the docs for the rest of the functions in these modules do not address this, I assumed it was not desired.
@bors bors closed this as completed in a7889d1 Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants