Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Syam Krishna
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Monica Shiralkar
Ranch Hand
Posts: 919
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 919
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you think there is any specific reason for different standards?
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hasn't Henry already explained that?
 
Liutauras Vilda
Marshal
Posts: 4624
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!