Skip to content

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

Closed
@rosekunkel

Description

@rosekunkel

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.

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions