• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Negative number divide by zero?

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The result of this code is InfinityException in thread "main" java.lang.ArithmeticException: / by zero at MyClass.main(Test.java)

Why doesn't throw the exception x? Is it because the negative number?
Because is an int, not a double?
 
Paul Clapham
Sheriff
Posts: 20983
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The little test you can do to answer that question is this:
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as Paul pointed out, your test cases are not equivalent. in one, you have a positive number with a decimal point. in the other, you have a negative number without.

having multiple variables (sign of the number, floating point vs integer literals) change across a single test is not a good plan.
 
John Call
Greenhorn
Posts: 20
Java MyEclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

Hi,
above gives an exception offcourse.
But why as OP asks

Please throw some light.
Thanks.
 
Paul Clapham
Sheriff
Posts: 20983
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code speaks for itself, doesn't it? Dividing an integer by zero throws an exception, dividing a double by zero returns infinity.
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation of the ArithmeticException class says:

Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.




gives an Exception.



gives infinity. The int '1' is treated as a double because '0.' is a double.

John
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic