1

two double values .

    double a=-324.000000
    double b= 0.000000
if(a*b<0)
{
//number is less than 0
 }
else
{
//number is greater than zero
}

It always gives an output as 'number less than zero'.When i multiply both of them i get the result as `-0.000000 .However the result should be 0.000000 .

james
  • 221
  • 5
  • 16
  • 0 and -0.0 are binary different numbers in float... but i think they are supposed to be equal... I just try to do my float math in a way that is idiot proof. – Grady Player Nov 22 '13 at 20:22
  • 1
    No, it doesn't, the compiler produces a syntax error. Once fixed, the program does **not** evaluate `a*b<0` to true. – Pascal Cuoq Nov 22 '13 at 20:22
  • I'm not seeing this problem. Is your compiler *really* equating (-0.0<0.0) to true? – r3mainer Nov 22 '13 at 20:29
  • 1
    What IDE/compiler do you use? – DarkWanderer Nov 22 '13 at 20:32
  • `-0` is **not** less than `0` in IEEE floating-point; ordinary comparisons treat `-0` and `+0` as the same value. The problem is somewhere else. – Pete Becker Nov 22 '13 at 20:39
  • Can you do this: `printf("%0.100f\n", (a*b));` and tell us what is says? – Fiddling Bits Nov 22 '13 at 20:48
  • Show a [self-contained compilable example](http://sscce.org). In the process of doing that, you will likely discover that, in your actual code, `b` is not 0.000000. Rather, you displayed `b` with something like a `printf`, and it showed “0.000000”. However, the output was limited due to the format used, and the true value is small but non-zero. – Eric Postpischil Nov 22 '13 at 21:00
  • 3
    @BitFiddlingCodeMonkey: `%0.100f` may be insufficient to display `b` as non-zero, since IEEE-754 64-bit binary (and other common floating-point formats) has values even smaller than 1e-100. `%g` would suffice. – Eric Postpischil Nov 22 '13 at 21:10
  • @ames My money is on: OP's `-0.000000` is not `-0.0`, but some small negative number like `-0.0000000001` that prints out a `-0.000000`. Try `printf("%e\n", b)` in original code to see what your true `b` is. – chux - Reinstate Monica Nov 22 '13 at 23:43

3 Answers3

1

This is not the result that I get. Are you sure the problem isn't somewhere else?

You said in a comment "i tried if(a==0.0){/*but it never enters here!!*/ }." It really looks like the number is not actually 0.

Dan
  • 12,409
  • 3
  • 50
  • 87
0

Consider this code:

#include<iostream>
int main() {
  double a=-324;
  double b= 0;
  double c = a * b;
  std::cout<<std::boolalpha
           <<"a     : "<<a<<std::endl
           <<"b     : "<<b<<std::endl
           <<"c     : "<<c<<std::endl
           <<"c < 0 : "<<(c < 0)<<std::endl
           <<"c > 0 : "<<(c > 0)<<std::endl
           <<"c == 0: "<<(c == 0)<<std::endl
      ;
}

With output:

a     : -324
b     : 0
c     : -0
c < 0 : false
c > 0 : false
c == 0: true

According to the IEEE Standard for Floating Point Arithmetic (also known as IEEE 754):

Comparisons shall ignore the sign of zero (so +0 = -0)

(quote from Section 5.11)

Your problem is somewhere else.

Escualo
  • 40,844
  • 23
  • 87
  • 135
0

Yes, it is possible to generate a value of -0.0. It does compare equal to positive 0.0 so you can do the following:

double c = a * b;
if (c == 0.0)
    c = 0.0;

I've actually used this code in Visual Studio to fix a formatting bug. Make sure you comment it thoroughly so people don't think you're nuts.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Negative zero is clearly not the problem here. If `a*b` evaluated to negative zero, `a*b < 0` would evaluate to false. – Eric Postpischil Nov 22 '13 at 21:01
  • @EricPostpischil, I'm assuming the problem statement is over-simplified and that there's a real problem somewhere behind it, as there was in my case. – Mark Ransom Nov 22 '13 at 21:04
  • The problem is almost certainly that `b` is not zero as the OP believes. There is no negative zero involved. Assigning and adjusting `c` as shown in this answer will not fix the problem. – Eric Postpischil Nov 22 '13 at 21:06