• 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
  • 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
  • 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: 24204
44
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: 2610
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: 23928
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: 2610
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: 76856
366
  • 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?
 
Sheriff
Posts: 8521
615
Mac OS X VI Editor BSD Linux
  • 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.
 
Sheriff
Posts: 27451
88
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?
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic