Skip to content

IEEE754 non-conformance: is_sign_negative does not apply on NANs #42425

Closed
@nagisa

Description

@nagisa
Member

IEEE754 says

― boolean isSignMinus(source)
isSignMinus(x) is true if and only if x has negative sign. isSignMinus applies to zeros and NaNs
as well.

However our is_sign_negative/is_sign_positive do not properly inspect the sign for NaNs.

Activity

added
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Jun 4, 2017
Thiez

Thiez commented on Jun 4, 2017

@Thiez
Contributor

This program:

fn main() {
    let nums = [::std::f64::NEG_INFINITY, -1.0, -0.0, ::std::f64::NAN, 0.0, 1.0, ::std::f64::INFINITY];
    for &num in &nums[..] {
        println!("Number: {}\tSign negative: {}\tSign positive: {}", num, num.is_sign_negative(), num.is_sign_positive());
    }
}

prints the following output when compiled with current master and nightly:

Number: -inf	Sign negative: true	Sign positive: false
Number: -1	Sign negative: true	Sign positive: false
Number: 0	Sign negative: true	Sign positive: false
Number: NaN	Sign negative: false	Sign positive: false
Number: 0	Sign negative: false	Sign positive: true
Number: 1	Sign negative: false	Sign positive: true
Number: inf	Sign negative: false	Sign positive: true

What would be the expected result according to the standard? I would expect something that isn't a number to be neither negative nor positive, so I think the current results are correct?

nagisa

nagisa commented on Jun 4, 2017

@nagisa
MemberAuthor

My reading of the spec suggests the answer should be equivalent to is_sign_negative = signbit(x) != 0, where signbit is non-zero if the binary representation has sign bit set.

E.g. Section 6.2.1:

When encoded, all NaNs have a sign bit and a pattern of bits necessary to identify the encoding as a NaN and which determines its kind (sNaN vs. qNaN).

alludes to NaNs having a sign bit. The description of isSignMinus seems to quite clearly signal towards the fact that for NaNs the implementation ought to be inspecting this sign bit.

You can set the sign of NaN in rust with -::std::f64::NAN as well.

9 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.P-mediumMedium priorityT-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

        Participants

        @alexcrichton@Thiez@nagisa@hanna-kruppe@Mark-Simulacrum

        Issue actions

          IEEE754 non-conformance: is_sign_negative does not apply on NANs · Issue #42425 · rust-lang/rust