Doubles and zero/negative zero

Discussion in 'C Programming' started by Christopher Benson-Manica, Jun 30, 2004.

  1. How does one determine whether a double is equal to -0.0000...? Will
    fabs() from math.h return 0.000... on -0.0000...?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jun 30, 2004
    #1
    1. Advertising

  2. Christopher Benson-Manica

    P.J. Plauger Guest

    "Christopher Benson-Manica" <> wrote in message
    news:cbv0n3$k0e$...

    > How does one determine whether a double is equal to -0.0000...?


    You have to use a C99 function such as signbit or copysign to
    see the sign bit, because -0 == +0.

    > Will
    > fabs() from math.h return 0.000... on -0.0000...?


    That's what you get with IEEE conformance, but otherwise all bets
    are off.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
     
    P.J. Plauger, Jun 30, 2004
    #2
    1. Advertising

  3. Christopher Benson-Manica

    Neil Kurzman Guest

    Christopher Benson-Manica wrote:

    > How does one determine whether a double is equal to -0.0000...? Will
    > fabs() from math.h return 0.000... on -0.0000...?
    >
    > --
    > Christopher Benson-Manica | I *should* know what I'm talking about - if I
    > ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


    One way would be:

    if( (Number < 0) &&( Number > -0.00001))
    You get to figure out how many zeros work for you
     
    Neil Kurzman, Jul 1, 2004
    #3
  4. Christopher Benson-Manica

    Richard Bos Guest

    Neil Kurzman <> wrote:

    > Christopher Benson-Manica wrote:
    >
    > > How does one determine whether a double is equal to -0.0000...? Will
    > > fabs() from math.h return 0.000... on -0.0000...?

    >
    > One way would be:
    >
    > if( (Number < 0) &&( Number > -0.00001))


    And, erm... who guarantees that -0.000 < 0?

    Richard
     
    Richard Bos, Jul 1, 2004
    #4
  5. Christopher Benson-Manica

    Walter Guest

    "Neil Kurzman" <> wrote in message
    news:...
    > Christopher Benson-Manica wrote:
    > > How does one determine whether a double is equal to -0.0000...? Will
    > > fabs() from math.h return 0.000... on -0.0000...?

    > One way would be:
    >
    > if( (Number < 0) &&( Number > -0.00001))
    > You get to figure out how many zeros work for you


    That won't work, since (Number<0) will be false for Number=-0. The -0
    floating point bit pattern is all 0 bits except the sign bit, which is
    turned on. The only time the sign of zero matters is when determining which
    side of 0 you're on if you're on a singularity at 0 (called a branch cut).
    The IEEE floating point arithmetic routines carefully keep track of -0's,
    but to test for it you'll need to check directly for the sign bit, which is
    easy enough with:

    #include <math.h>

    if (signbit(x)) ...

    fabs, if implemented correctly, will convert -0 to 0.

    Here's a brief introduction to IEEE arithmetic:
    www.digitalmars.com/ctg/ctgNumerics.html

    Here are some math functions, and what they do with -0:
    www.digitalmars.com/rtl/math.html

    -Walter
    www.digitalmars.com free C/C++/D compilers
     
    Walter, Jul 1, 2004
    #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.
Similar Threads
  1. prem_eda
    Replies:
    5
    Views:
    7,991
    Pieter Hulshoff
    Oct 11, 2004
  2. dan
    Replies:
    1
    Views:
    2,353
    Jack Klein
    Nov 26, 2003
  3. Hallvard B Furuseth

    x == 0 && (x & -1) != 0 for negative zero?

    Hallvard B Furuseth, Sep 11, 2007, in forum: C Programming
    Replies:
    26
    Views:
    799
    Richard Bos
    Sep 24, 2007
  4. K. Frank

    Re: Negative zero.

    K. Frank, Jul 12, 2013, in forum: C++
    Replies:
    0
    Views:
    167
    K. Frank
    Jul 12, 2013
  5. Roberto Waltman

    Re: Negative zero.

    Roberto Waltman, Jul 12, 2013, in forum: C++
    Replies:
    0
    Views:
    160
    Roberto Waltman
    Jul 12, 2013
Loading...

Share This Page