The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Division by zero floating point question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Division by zero floating point question" Watch "Division by zero floating point question" New topic

Division by zero floating point question

Vanitha Naidu

Joined: Jun 20, 2004
Posts: 2
I am going through Java 2 Sun Certification Study guide by Sierra/Bates.

On Page 163 (last paragraph) the authors discuss division by zero and state that "Floating-point numbers divided by zero return either positive or negative infinity, depending on whether or not the zero is positive or negative!"

When I tested this in code I get -infinity only when the dividend is negative regardless of whether 0(divisor) is positive or negative

i.e. -6.25/0 or -6.25/-0 is -infinity and
6.25/0 or 6.25/-0 is infinity

so the answer is dependant on the sign of the dividend and not on whether 0 is positive or negative.

Please help, is the book incorrect or am I interpreting this incorrectly.

Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
I created a small program to test the above problem. Here is the code:

The output is as follows:
Value is:-Infinity
Value is:-Infinity
Value is:Infinity
Value is:Infinity

I am guessing that your divisor is not a floating point number (either a float or double) but an integer which is accounting for the incorrect result.
Sanyev Babu
Ranch Hand

Joined: Dec 18, 2003
Posts: 132
in 6.25/-0 the -0 is an integer. When it gets pormoted to a float it becomes 0.0 not -0.0. That's why you are getting positive infinity for 6.25/-0
Vanitha Naidu

Joined: Jun 20, 2004
Posts: 2
Thank you for that explanation, I was not using a floating point zero for my divisor.

Vanitha Naidu
I agree. Here's the link: http://aspose.com/file-tools
subject: Division by zero floating point question
Similar Threads
Floating Point Number and dividebyzero arithmatic exception
if(0.0 == -0.0) - Sample question
Rules Roundup - answer wrong?
Arithmetic Exception vs Infinity upon dividing by "0" and "0.0"