• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Arithmatic Exception

 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why doesn't it throw ArithmaticException?

Instead it prints infinity. Isn't java partial with int in this case
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Astha Sharma wrote:Why doesn't it throw ArithmaticException?

Instead it prints infinity. Isn't java partial with int in this case




Not sure what you mean by "partial with int", but Java supports the IEEE 754 floating point standard -- and that's how it is defined. A positive number divided by positive zero results in positive infinity.

Henry
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Arithmatic Exception is thrown when this is written-
int i=100/0;
Shouldn't the exception also be thrown when we devide a double with 0?
double i=100.0/0
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Astha Sharma wrote:But Arithmatic Exception is thrown when this is written-
int i=100/0;
Shouldn't the exception also be thrown when we devide a double with 0?
double i=100.0/0


Well, as Henry Wong has already mentioned, 'double' in Java follows IEEE 754 floating point standard. As per that, a positive floating point number devided by 0 results in positive infinity.

'int' in Java does not follow this standard, and hence we get ArithmeticException.

I hope this helps.
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Astha Sharma wrote:But Arithmatic Exception is thrown when this is written-
int i=100/0;
Shouldn't the exception also be thrown when we devide a double with 0?
double i=100.0/0



For ints, Java follows the two complement format -- and that format doesn't define infinity. Hence, it throws an exception. For floats, as mentioned, it follows the IEEE format.

Also, note that these two are the common format that are supported by most (if not all) modern day processors and languages.


Perhaps, you are asking why the standards don't agree? (which of course, we can only speculate)
Henry

 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Perhaps, you are asking why the standards don't agree? (which of course, we can only speculate)
Henry


This contradiction of statndards always astonish me but don't worry, i'm not going to ask this
Thankyou
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic