Skip to content
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

NaN behaviour of is_sign_positive and is_sign_negative is not like for primitive types #3

Closed
tspiteri opened this issue Dec 29, 2017 · 0 comments

Comments

@tspiteri
Copy link
Contributor

extern crate half;
use half::f16;
use std::f32;
use std::mem;
fn main() {
    let nan32 = f32::NAN;
    println!(
        "nan32.is_sign_positive() == nan32.is_sign_negative() -> {}",
        nan32.is_sign_positive() == nan32.is_sign_negative()
    );
    let unan32: u32 = unsafe { mem::transmute(nan32) };
    let negated32: f32 = unsafe { mem::transmute(unan32 ^ 0x8000_0000) };
    println!(
        "nan32.is_sign_positive() == negated32.is_sign_positive() -> {}",
        nan32.is_sign_positive() == negated32.is_sign_positive()
    );

    let nan16 = half::consts::NAN;
    println!(
        "nan16.is_sign_positive() == nan16.is_sign_negative() -> {}",
        nan16.is_sign_positive() == nan16.is_sign_negative()
    );
    let unan16: u16 = unsafe { mem::transmute(nan16) };
    let negated16: f16 = unsafe { mem::transmute(unan16 ^ 0x8000) };
    println!(
        "nan16.is_sign_positive() == negated16.is_sign_positive() -> {}",
        nan16.is_sign_positive() == negated16.is_sign_positive()
    );
}

Output:

nan32.is_sign_positive() == nan32.is_sign_negative() -> false
nan32.is_sign_positive() == negated32.is_sign_positive() -> false
nan16.is_sign_positive() == nan16.is_sign_negative() -> true
nan16.is_sign_positive() == negated16.is_sign_positive() -> true

Note that in Rust version 1.20.0, rust-lang/rust#42431, the behaviour of is_sign_positive and is_sign_negative for NaN was updated to conform to IEEE754.

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

No branches or pull requests

1 participant