aspose file tools*
The moose likes Beginning Java and the fly likes Inconsistent division by zero Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inconsistent division by zero" Watch "Inconsistent division by zero" New topic
Author

Inconsistent division by zero

Dan Temple
Ranch Hand

Joined: Jul 10, 2001
Posts: 93
Hey everyone!
I love this site!!! This is my first post, so please be gentle ...
I have a very straightforward question, but I'm not sure whether it is a basic question or an intermediate one. My question is why is there a difference between integers and floating points when there is a division by zero? Integer division results in an arithmetic exception, while floating point division results in either the constants Float.POSITIVE_INFINITY or Double.POSITIVE_INFINITY. Why not just pick one method and consistently apply it for both integers and floating points?
Thanks,
Dan
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Because floats and doubles deal with decimals. To accurately track what a doubles value REALLY is you would need to have an infinite number of decimals.
If you have a 0.0 double or float it is unknown if about a zillion decimal places out there might actually be a value that makes in "not exactly" zero. Therefore it MIGHT actually be a decimal value that is just VERY VERY SMALL or BIG. Thus the POSITIVE_INFINITY or NEGATIVE-INFINITY.
With an integer there is NO doubt that you blew it, therefore the exception.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Inconsistent division by zero