title: Detecting and adjusting for negative zero
body: <p>I have some code that has been port from Java to C++</p>
  
  <pre><code>// since this point is a vector from (0,0,0), we can just take the
  // dot product and compare
  double r = point.dot(normal);
  return (r&gt;=0.0);
  </code></pre>
  
  <p>But in C++ <code>r</code> can be either <code>+0.0</code> or <code>-0.0</code>, when <code>r</code> equals <code>-0.0</code> it fails the check.</p>
  
  <p>I've tried to adjust for negative zero in the code below but it never hits the DEBUG("Negative zero") line. But <code>r2</code> does print out equal to <code>+0.0</code>.</p>
  
  <pre><code>// since this point is a vector from (0,0,0), we can just take the
  // dot product and compare
  double r = point.dot(normal);
  if (std::signbit(r)){
      double r2 = r*-1;
      DEBUG("r=%f r=%f", r,r2);
      if (r2==0.0) {
          DEBUG("Negative zero");
          r = 0.0; //Handle negative zero
      }
  }
  return (r&gt;=0.0);
  </code></pre>
  
  <p>Any suggestion?</p>
  
  <p>TESTING CODE:</p>
  
  <pre><code>DEBUG("point=%s", point.toString().c_str());
  DEBUG("normal=%s", normal-&gt;toString().c_str());
  double r = point.dot(normal);
  DEBUG("r=%f", r);
  bool b = (r&gt;=0.0);
  DEBUG("b=%u", b);
  </code></pre>
  
  <p>TESTING RESULTS:</p>
  
  <pre><code>DEBUG - point=Vector3D[ x=1,y=0,z=0 ]
  DEBUG - normal=Vector3D[ x=0,y=-0.0348995,z=0.0348782 ]
  DEBUG - r=0.000000
  DEBUG - b=1
  DEBUG - point=Vector3D[ x=1,y=0,z=0 ]
  DEBUG - normal=Vector3D[ x=-2.78269e-07,y=0.0174577,z=-0.0174391 ]
  DEBUG - r=-0.000000
  DEBUG - b=0
  </code></pre>
  
  <p>GCC:</p>
  
  <pre><code>Target: x86_64-linux-gnu
  --enable-languages=c,c++,fortran,objc,obj-c++ 
  --prefix=/usr 
  --program-suffix=-4.6 
  --enable-shared 
  --enable-linker-build-id 
  --with-system-zlib 
  --libexecdir=/usr/lib 
  --without-included-gettext 
  --enable-threads=posix 
  --with-gxx-include-dir=/usr/include/c++/4.6 
  --libdir=/usr/lib 
  --enable-nls 
  --with-sysroot=/ 
  --enable-clocale=gnu 
  --enable-libstdcxx-debug 
  --enable-libstdcxx-time=yes 
  --enable-gnu-unique-object 
  --enable-plugin 
  --enable-objc-gc 
  --disable-werror 
  --with-arch-32=i686 
  --with-tune=generic 
  --enable-checking=release 
  --build=x86_64-linux-gnu 
  --host=x86_64-linux-gnu --target=x86_64-linux-gnu
  Thread model: posix
  gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
  </code></pre>
  
  <p>FLAGS:</p>
  
  <pre><code>CXXFLAGS += -g -Wall -fPIC
  </code></pre>
  
  <p>ANSWER:</p>
  
  <p>I've used @amit's answer to do the following.</p>
  
  <pre><code>return (r&gt;=(0.0-std::numeric_limits&lt;double&gt;::epsilon()));
  </code></pre>
  
  <p>Which seems to work.</p>