programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Arithmetic Exception vs Infinity upon dividing by "0" and "0.0"

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
Hi,

Why would we get an Arithmetic Exception if we divide by integer "0" and why would we get "inifinity" when we divide by double or float "0.0". What is the intrinsic reason behind this.

Thanks
Syam

author and iconoclast
Posts: 24204
44
• 2
• Number of slices to send:
Optional 'thank-you' note:
Hi,

Welcome to JavaRanch!

Simply enough, integer division by zero throws ArithmeticException because there's no way to represent "Infinity" as an integer. On the other hand, the IEEE floating-point format includes special values for positive and negative Infinity as well as a "Not a Number" value to represent things like 0/0 -- so floating-point division doesn't need to throw an exception to indicate a problem.

Ranch Hand
Posts: 2610
13
• Number of slices to send:
Optional 'thank-you' note:

Simply enough, integer division by zero throws ArithmeticException because there's no way to represent "Infinity" as an integer. On the other hand, the IEEE floating-point format includes special values for positive and negative Infinity as well as a "Not a Number" value to represent things like 0/0 -- so floating-point division doesn't need to throw an exception to indicate a problem.

But why does Java not provide such a feature in case of Integer. Why no provision of Infinity or NAN in case of Integer. Why does java provide this for floating point and not for integer?

thanks

author
Posts: 23928
142
• 1
• Number of slices to send:
Optional 'thank-you' note:

Monica Shiralkar wrote:
But why does Java not provide such a feature in case of Integer. Why no provision of Infinity or NAN in case of Integer. Why does java provide this for floating point and not for integer?

I don't think that it is a design issue of Java. Java is standardize on Twos Complement for whole numbers (integers) and IEEE 754 for floating point (float and double).

I guess you can argue that Java could have modified the standard to make it more similar, but then you will get the question of why Java didn't follow industry standards.

Henry

Monica Shiralkar
Ranch Hand
Posts: 2610
13
• Number of slices to send:
Optional 'thank-you' note:
Do you think there is any specific reason for different standards?

Marshal
Posts: 76856
366
• Number of slices to send:
Optional 'thank-you' note:

Sheriff
Posts: 8521
615
• Number of slices to send:
Optional 'thank-you' note:
Monica,

In addition to the explanations you were given by others, you could also look for an extra information in Java Language Specification (<- link).
Look for section 4.2.3.

Sheriff
Posts: 27451
88
• Number of slices to send:
Optional 'thank-you' note:
Here's another thing to consider: All Java implementations that I know of run on computers which implement those standards (for integers and floating-point numbers) in their hardware. So if Java had been designed with some different implementation of integers or floating-point numbers, you'd be here asking why they chose to do that, right?

 It would give a normal human mental abilities to rival mine. To think it is just a tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising