What is "-0.0"

M

Morgan Cheng

In java 6.0, I have below code.
double x = 0.0;
double y = -10.0;
double z = x / y;
System.out.println("z = " +z);

The result is "-0.0". This is wired, why it is negative zero?
 
P

Patricia Shanahan

Morgan said:
In java 6.0, I have below code.
double x = 0.0;
double y = -10.0;
double z = x / y;
System.out.println("z = " +z);

The result is "-0.0". This is wired, why it is negative zero?

I agree that it is weird, but there are reasons why IEEE 754 floating
point arithmetic distinguishes positive and negative zero.

A zero may be a stand-in for a number of very small absolute magnitude.
There are some algorithms where intermediate results overflow and
underflow, but the final answer is representable.

Preserving the sign on underflow, and through arithmetic using a zero,
also preserves the sign on division by the result of the underflow,
leading to the correct choice between positive and negative infinity.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top