• Post Reply Bookmark Topic Watch Topic
  • New Topic

not getting devide by zero error  RSS feed

 
Mamata Gelanee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is code :




now ideally this might throw Arithmetic Exception.. but it prints positive infinity ,.. why.. ??
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mamata,

First of all, a warm welcome to CodeRanch!

Mamata Gelanee wrote:now ideally this might throw Arithmetic Exception.. but it prints positive infinity ,.. why.. ??

Have a look at this thread. It will definitiely answer your question.

Nice to know: when posting code snippets, UseCodeTags (<- click). It will drastically improve the readibility of your code (and your post) increasing your chances of a response. Because it's your 1st post, I added them for you.

Best wishes for 2015!
Kind regards,
Roel
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you expected it to print negative infinity. Have you worked out why it dind't? By the way, you may find it simpler to write
System.out.println(10.0 / -0);
You can find the range of possible outputs here.

And welcome again
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, this does get a bit complicated.

1. There is a difference in data types -- which needs an understanding of two different standards. Probably not to full details, but enough to understand why exceptions versus "infinity".
2. There is a mixing of data types -- which basically, on the easy side, need an understanding that when you mix integers with doubles, the result is a doubles. However, this is made a bit more complicated, as it depends on the knowledge of operator precedence (to determine when the conversion to double happens)... so ... you need to know when one set of rules apply, and when it switches to the other set of rules.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mamata Gelanee wrote:now ideally this might throw Arithmetic Exception...

Not at all. Division by ±0 in programming is only an exception (arithmetic or otherwise) if the target type cannot hold a result that represents it; and in the case of a double, that is not true.

As for why it equals infinity and not -infinity. Not sure; but the others seem to have covered it.

I have to say that I don't like it though, so I'm with you there.

+0 and -0, on the other hand (which I think the link was referring to) are a different thing. And I suspect the fact that +0.0 == -0.0 might have something to do with it.

Winston

PS: It should also, perhaps, be added that Double.valueOf(+0.0).equals( Double.valueOf(-0.0) ) returns false, so clearly there's still some debate...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
PS: It should also, perhaps, be added that Double.valueOf(+0.0).equals( Double.valueOf(-0.0) ) returns false, so clearly there's still some debate...


Yeah. This is in violation of the IEEE 754 specification. In fact, I believe that the JavaDocs even mentions this. And the reasoning (according to the JavaDocs), is that the equals() method is mostly use for the collection classes, and the designers wanted a clear behavior. For example, NaN is never equal to NaN according to the spec, but if that was implemented, then it would be possible to have more than one NaN in a set (or as a key in a map)... so, the Double class tests for equality, if it is exact, and not what the specification says should be equal (or not).

Henry
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
+0 and -0, on the other hand (which I think the link was referring to) are a different thing. And I suspect the fact that +0.0 == -0.0 might have something to do with it.
. . .
The mathematicians will tell you there ain't no such thing as +0 or -0. If you try them as Java® ints, however, they are equal to each other.
The Double#equals method takes a different view of equality from the == operator applied to doubles. The primitive double datatype conforms to IEEE754 which has some differences from what is conventional mathematics.
The Java® Language Specification tells what operations are available for integers and floating‑point values; they are slightly different.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is what the method I linked says: so hash tables will work correctly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!