• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why doesn't Java throw an Exception when dividing by 0.0?  RSS feed

 
Rahul mir
Ranch Hand
Posts: 38
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, please someone explain me why in java divide by 0.0 is not generating any exception, is it will cause any serious problem when in code this type of situation comes.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's because thats a runtime evaluation and as such the onus is on the developer, just like null pointers.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:I think it's because thats a runtime evaluation and as such the onus is on the developer, just like null pointers.


I'm not sure this explanation is logical. At run time if one tries to de-reference a null pointer a NullPointerException is thrown but if one performs a divide by zero then no exception is thrown!

I do a lot of work with doubles and every now and again I would like to be able to indicate to the JRE that a particular double operation may produce a NEGATIVE_INFINITY or POSITIVE_INFINITY and I would like to be informed of the fact without me having to check. One of the advantages of throwing exceptions is that code is not littered with the operation validity checks that characterize other languages such as C.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As RT has hinted, the reason is to do with the range of the values. A double supports ±∞ values, so it can be divided by 0. If you try dividing an integer primitive by 0.0, which is a double, you promote that integer to a double, again getting ±∞. More strangely still, a double supports “NaN” values, too. you should be able to find how those values are defined here, remembering that 0.0, 0d and 0D are all ways you can write a double literal for 0 in Java.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if it takes you > 5 seconds to think of a more efficient version of that method, I shall be very surprised!
 
Rahul mir
Ranch Hand
Posts: 38
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou all for reply. :-)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure, but I would guess that the reason is that, due to the inherent imprecision in FP numbers, when you divide by 0.0, it might be that the actual value isn't 0.0, but that's the closest double could come to representing the true value. So rather than throw an exception, it says, "You're dividing by a really tiny number, tinier than I can represent, so the result is going to be bigger than I can represent, so here's my catch-all really big value."
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is not a Java specific behavior, as divide by zero behavior is specified by the IEEE 754 specification.... A positive number that is divided by positive zero must result in positive infinity.

Henry
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
A positive number that is divided by positive zero must result in positive infinity.


I know why they do it like that, but that sort of description does make the mathematician in me cry a little .
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I know why they do it like that, but that sort of description does make the mathematician in me cry a little .

Well...since the mathematicians chose not to define "divide by zero", the CS folks had to pick up the slack.

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0.0 is one of the numbers you can be absolutely sure a double represents accurately and precisely!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:0.0 is one of the numbers you can be absolutely sure a double represents accurately and precisely!

Well, it depends which way you're going. If you know that 0 is what you're trying to represent, then 0.0 is guaranteed to represent it accurately as a double However if you get a 0.0 result, you don't know for sure that it should really represent 0. It might also "really" be a number very close to 0 that got rounded off to 0.0.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you mean. I meant if you write 0.0 as a literal.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!