Skip to content

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

Closed

Description

@nagisa

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

  1. 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?

  2. 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.

  3. added a commit that references this issue on Jun 28, 2017
  4. added
    C-bugCategory: This is a bug.
    and removed on Jul 27, 2017
  5. hanna-kruppe commented on Nov 20, 2017

    @hanna-kruppe
    Contributor

    Isn't this fixed since #42431 was merged?

  6. added a commit that references this issue on Mar 1, 2018
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-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions