Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

f64::atan2 documentation error #136275

Closed

Description

@gantha-shiva

Location

library/std/src/f64.rs:793

Summary

The f64::atan2 function documentation specifies that the output could be in the range (-PI, PI]. However its not true. It could return -PI as well.
Following is from the function documentation:

/// * `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)`

Example case:
f64::atan2(-0.0, -1.0) returns -f64::PI

Activity

  1. added
    A-docs
    on Jan 30, 2025
  2. added
    needs-triage
    on Jan 30, 2025
  3. added
    C-bug
    T-libs
    and removed
    needs-triage
    on Jan 30, 2025
  4. added
    A-floating-point
    on Jan 30, 2025
  5. GrigorenkoPV commented on Feb 17, 2025

    @GrigorenkoPV
    Contributor

    From my testing, f32::atan2(y, x).is_sign_positive() == y.is_sign_positive(). I was even able to make kani prove that for the libm implementation.

    use kani::*;
    
    #[kani::proof]
    fn nan() {
        let x: f32 = any();
        let y: f32 = any();
        assume(x.is_nan() || y.is_nan());
        let res = libm::atan2f(y, x);
        assert!(res.is_nan());
    }
    
    #[kani::proof]
    fn atan2() {
        let x: f32 = any();
        assume(!x.is_nan());
        let y: f32 = any();
        assume(!y.is_nan());
        let res = libm::atan2f(y, x);
        assert!(res.is_finite());
        assert_eq!(res.is_sign_positive(), y.is_sign_positive());
        assert!(res <= core::f32::consts::PI);
        assert!(res >= -core::f32::consts::PI);
    }

    @rustbot claim

    I think I'll put up a PR adjusting documentation.

  6. lolbinarycat commented on Apr 9, 2025

    @lolbinarycat
    Contributor

    triage: @GrigorenkoPV still planning on working on this?

    also note that this should be updated for other float types too (f16, f32, and f128)

  7. GrigorenkoPV commented on Apr 14, 2025

    @GrigorenkoPV
    Contributor

    triage: @GrigorenkoPV still planning on working on this?

    I do, but not sure when I will have time, so I should probably unclaim this for now.

    @rustbot release-assignment

    Also, I think the fix here is pretty easy: as I see it, one just needs to mention that the sign of the result is the same as the sign of the first argument, and that -0 & +0 both exist with floats and are two slightly different things.

    So probably just specify the output ranges for four of the cases:
    x >= +0, y >= +0
    x <= -0, y >= +0
    x >= +0, y <= -0
    x <= -0, y <= -0

    @rustbot label +E-easy

  8. added
    E-easy
    on Apr 14, 2025
  9. as7erism commented on Apr 14, 2025

    @as7erism

    @rustbot claim

  10. DimitriiTrater commented on Sep 11, 2025

    @DimitriiTrater
    Contributor

    @rustbot claim

  11. QueenOfSquiggles commented on Sep 28, 2025

    @QueenOfSquiggles

    Even though this issue is basically solved from what I can tell. I'm gonna give it a shot at solving it myself for practice to learn the contribution process ❤️

  12. added 5 commits that reference this issue on Oct 4, 2025
    62f2373
    b3c9cd3
    5858572
    4d52fa8
    4d89574
  13. added 3 commits that reference this issue on Oct 4, 2025
    eb8e240
    a032678
    baea20b
  14. added a commit that references this issue on Oct 4, 2025
    0dc71b5
  15. added a commit that references this issue on Oct 9, 2025
    7dbfae2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-docsA-floating-pointC-bugE-easyT-libs

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions