Skip to content

Note atan2 can return -PI #140487

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions library/std/src/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,10 +898,12 @@ impl f128 {

/// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
///
/// * `x = 0`, `y = 0`: `0`
/// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
/// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
/// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
/// * `x >= +0`, `y >= +0` -> `[+0, pi/2]`
/// * `x <= -0`, `y >= +0` -> `[pi/2, pi]`
/// * `x <= -0`, `y <= -0` -> `[-pi, -pi/2]`
/// * `x >= +0`, `y <= -0` -> `[-pi/2, -0]`
Comment on lines -901 to +904
Copy link
Member

@workingjubilee workingjubilee Apr 30, 2025

Choose a reason for hiding this comment

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

println!("{}", f32::atan2(-0.0, 0.0)); // -0
println!("{}", f32::atan2(0.0, -0.0)); // 3.1415927
println!("{}", f32::atan2(0.0, 0.0)); // 0
println!("{}", f32::atan2(-0.0, -0.0)); // -3.1415927

...that's kinda fucked up huh.

I think placing the comment about negative versus positive zero after this table-ish-thing makes it harder to read. We should lead with the comment that the sign of 0.0 can affect the result, rather than being treated as equal, even though (0.0 == -0.0) == true.

///
/// Note that positive and negative 0 are distinct floating point values.
Copy link
Member

Choose a reason for hiding this comment

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

This is always true, what's more important is that this description is not treating them as equal.

///
/// # Unspecified precision
///
Expand Down
10 changes: 6 additions & 4 deletions library/std/src/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,12 @@ impl f16 {

/// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
///
/// * `x = 0`, `y = 0`: `0`
/// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
/// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
/// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
/// * `x >= +0`, `y >= +0` -> `[+0, pi/2]`
/// * `x <= -0`, `y >= +0` -> `[pi/2, pi]`
/// * `x <= -0`, `y <= -0` -> `[-pi, -pi/2]`
/// * `x >= +0`, `y <= -0` -> `[-pi/2, -0]`
///
/// Note that positive and negative 0 are distinct floating point values.
///
/// # Unspecified precision
///
Expand Down
10 changes: 6 additions & 4 deletions library/std/src/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,12 @@ impl f32 {

/// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
///
/// * `x = 0`, `y = 0`: `0`
/// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
/// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
/// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
/// * `x >= +0`, `y >= +0` -> `[+0, pi/2]`
/// * `x <= -0`, `y >= +0` -> `[pi/2, pi]`
/// * `x <= -0`, `y <= -0` -> `[-pi, -pi/2]`
/// * `x >= +0`, `y <= -0` -> `[-pi/2, -0]`
///
/// Note that positive and negative 0 are distinct floating point values.
///
/// # Unspecified precision
///
Expand Down
10 changes: 6 additions & 4 deletions library/std/src/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,12 @@ impl f64 {

/// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
///
/// * `x = 0`, `y = 0`: `0`
/// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
/// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
/// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
/// * `x >= +0`, `y >= +0` -> `[+0, pi/2]`
/// * `x <= -0`, `y >= +0` -> `[pi/2, pi]`
/// * `x <= -0`, `y <= -0` -> `[-pi, -pi/2]`
/// * `x >= +0`, `y <= -0` -> `[-pi/2, -0]`
///
/// Note that positive and negative 0 are distinct floating point values.
///
/// # Unspecified precision
///
Expand Down
Loading