What is "-0.0"

Discussion in 'Java' started by Morgan Cheng, Oct 13, 2007.

  1. Morgan Cheng

    Morgan Cheng Guest

    In java 6.0, I have below code.
    double x = 0.0;
    double y = -10.0;
    double z = x / y;
    System.out.println("z = " +z);

    The result is "-0.0". This is wired, why it is negative zero?
    Morgan Cheng, Oct 13, 2007
    #1
    1. Advertising

  2. "Morgan Cheng" <> wrote in message
    news:...
    > In java 6.0, I have below code.
    > double x = 0.0;
    > double y = -10.0;
    > double z = x / y;
    > System.out.println("z = " +z);
    >
    > The result is "-0.0". This is wired, why it is negative zero?
    >

    http://en.wikipedia.org/wiki/−0_(number)
    Richard Reynolds, Oct 13, 2007
    #2
    1. Advertising

  3. Morgan Cheng wrote:
    > In java 6.0, I have below code.
    > double x = 0.0;
    > double y = -10.0;
    > double z = x / y;
    > System.out.println("z = " +z);
    >
    > The result is "-0.0". This is wired, why it is negative zero?
    >


    I agree that it is weird, but there are reasons why IEEE 754 floating
    point arithmetic distinguishes positive and negative zero.

    A zero may be a stand-in for a number of very small absolute magnitude.
    There are some algorithms where intermediate results overflow and
    underflow, but the final answer is representable.

    Preserving the sign on underflow, and through arithmetic using a zero,
    also preserves the sign on division by the result of the underflow,
    leading to the correct choice between positive and negative infinity.

    Patricia
    Patricia Shanahan, Oct 13, 2007
    #3
  4. Morgan Cheng

    Stefan Ram Guest

    Stefan Ram, Oct 13, 2007
    #4
  5. Morgan Cheng

    Roedy Green Guest

    On Sat, 13 Oct 2007 05:06:35 -0700, Morgan Cheng
    <> wrote, quoted or indirectly quoted someone
    who said :

    >
    >The result is "-0.0". This is wired, why it is negative zero?


    Because floating point has both a positive and negative 0, signed
    magnitude, unlike ints. see http://mindprod.com/jgloss/ieee754.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Oct 14, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page