This week's book giveaway is in the Java 9 forum.We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Inconsistent division by zero

Dan Temple
Ranch Hand
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
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.

 Don't get me started about those stupid light bulbs.