Forums Register Login

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

+Pie Number of slices to send: Send
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
2
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
1
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
Do you think there is any specific reason for different standards?
+Pie Number of slices to send: Send
Hasn't Henry already explained that?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
Always look on the bright side of life. At least this ad is really tiny:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3317 times.
Similar Threads
Arithmetic Exception
NaN
please explain this Assertions problem
Operators modulo and division
Float numbers division
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:59:53.