Skip to content

f.sin().asin() sometimes returns NaN #4207

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
RalfJung opened this issue Feb 25, 2025 · 2 comments
Closed

f.sin().asin() sometimes returns NaN #4207

RalfJung opened this issue Feb 25, 2025 · 2 comments

Comments

@RalfJung
Copy link
Member

RalfJung commented Feb 25, 2025

This code prints some NaN with latest Miri (not yet synced into rustc):

fn main() {
    let f = std::f64::consts::FRAC_PI_2;
    for _ in 0..64 {
        println!("{}", (dbg!(f.sin()).asin() - std::f64::consts::FRAC_PI_2).abs());
    }
}

The reason is that f.sin() can return values slightly above 1 due to the imprecision we add, and then asin() returns NaN. That seems undesirable.

@tgross35 is there some sort of guarantee that f.sin(), while it can be imprecise, will always return values that strictly satisfy x <= 1.0? Or should asin be more accepting of values slightly above NaN?

@tgross35
Copy link
Contributor

I don't think anything is actually guaranteed here, assuming the implementations follow the C spec. But if this is causing problems it's probably fine for Miri to ensure the nondeterminism is still bounded by the output domain, I think this is a much less likely failure mode compared to general imprecision.

Cc #4208

@RalfJung
Copy link
Member Author

Closing in favor of #4208.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants