• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you think there is any specific reason for different standards?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hasn't Henry already explained that?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic