Doubles and zero/negative zero

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    #1

    Doubles and zero/negative zero

    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)cybers pace.org | don't, I need to know. Flames welcome.
  • P.J. Plauger

    #2
    Re: Doubles and zero/negative zero

    "Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
    news:cbv0n3$k0e $1@chessie.cirr .com...
    [color=blue]
    > How does one determine whether a double is equal to -0.0000...?[/color]

    You have to use a C99 function such as signbit or copysign to
    see the sign bit, because -0 == +0.
    [color=blue]
    > Will
    > fabs() from math.h return 0.000... on -0.0000...?[/color]

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

    P.J. Plauger
    Dinkumware, Ltd.



    Comment

    • Neil Kurzman

      #3
      Re: Doubles and zero/negative zero



      Christopher Benson-Manica wrote:
      [color=blue]
      > 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)cybers pace.org | don't, I need to know. Flames welcome.[/color]

      One way would be:

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


      Comment

      • Richard Bos

        #4
        Re: Doubles and zero/negative zero

        Neil Kurzman <nsk@mail.asb.c om> wrote:
        [color=blue]
        > Christopher Benson-Manica wrote:
        >[color=green]
        > > How does one determine whether a double is equal to -0.0000...? Will
        > > fabs() from math.h return 0.000... on -0.0000...?[/color]
        >
        > One way would be:
        >
        > if( (Number < 0) &&( Number > -0.00001))[/color]

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

        Richard

        Comment

        • Walter

          #5
          Re: Doubles and zero/negative zero


          "Neil Kurzman" <nsk@mail.asb.c om> wrote in message
          news:40E3A0C8.3 317A006@mail.as b.com...[color=blue]
          > Christopher Benson-Manica wrote:[color=green]
          > > How does one determine whether a double is equal to -0.0000...? Will
          > > fabs() from math.h return 0.000... on -0.0000...?[/color]
          > One way would be:
          >
          > if( (Number < 0) &&( Number > -0.00001))
          > You get to figure out how many zeros work for you[/color]

          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:


          Here are some math functions, and what they do with -0:


          -Walter
          www.digitalmars.com free C/C++/D compilers



          Comment

          Working...