Evaluating to negative zero

Don't know if anyone's tried this, but:

Code :

System.out.println((0*3*Math.sin(4)));

output:

even if you set it to a double variable:

Code :

double a = 0*3*Math.sin(4);
System.out.println(a);

output:

Code :

if (a == 0)
{
System.out.println("-0 equals 0");
}

any thoughts on why this is?

Re: Evaluating to negative zero

Hello helloworld922,

What made you do this? What output would you be expecting?

Re: Evaluating to negative zero

I was testing my parser with fairly random expressions to see if it was correct. When this spit out -0, i did a few tests in java itself to see what java did.

I expected it to be positive 0.

Re: Evaluating to negative zero

Re: Evaluating to negative zero

Turns out Java has a bit of a problem. When you multiply any float / double by 0 you will always get the sign of the original double / float, rather than plain positive 0.

Hence why 0*3*-0.75..... returns -0.

Chris

Re: Evaluating to negative zero

well, i guess at least they bothered to make -0 = 0 :P

Re: Evaluating to negative zero