This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arithmatic Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arithmatic Exception" Watch "Arithmatic Exception" New topic
Author

Arithmatic Exception

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

Why doesn't it throw ArithmaticException?

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


Astha - OCPJP 6 (90%)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

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
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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

Joined: Oct 15, 2011
Posts: 245

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arithmatic Exception
 
Similar Threads
Why we live?
How to do a Comparator with two properties ?
Convert decimal value to binary
Find two elements in an array that sum to k
Returning array To Method