• Post Reply Bookmark Topic Watch Topic
  • New Topic

0 / 0  RSS feed

 
Abigail Decan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my textbook told me that 0 / 0 returns an "NaN"
but my compiler gave me a runtime error.

is a runtime error same as an NaN.

i'm using ideone.com to compile & test my codes; could this be a reason?
does online compilers sometimes give different behaviors?

thanks!
 
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
Abe Dec wrote:my textbook told me that 0 / 0 returns an "NaN"
but my compiler gave me a runtime error.

is a runtime error same as an NaN.

i'm using ideone.com to compile & test my codes; could this be a reason?
does online compilers sometimes give different behaviors?


Hint: Try it again. This time, use a floating point variable.

Henry
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abigail Decan wrote: . . .
but my compiler gave me a runtime error.
No, the compiler does not give runtime errors. The runtime gives runtime Exceptions.
is a runtime error same as an NaN.
No.


i'm using ideone.com to compile & test my codes; could this be a reason?
does online compilers sometimes give different behaviors?

thanks!
Yes. You can get serious deviations from the behaviour required by the Java® Language Specification because online “compilers” do not handle errors correctly. Download and install a JVM as described here.

Integer arithmetic does not support ∞ nor “NaN”, so any attempt to divide by 0 causes an Exception. Note that System.out.println(123 % 0); counts as division by 0, too.
Floating‑point arithmetic supports both (±)∞ and “NaN”, so you can convert one of the numbers to a double (0.0 or 0d) and try System.out.println(0d / 0d);, System.out.println(1d / 0d); or System.out.println(-1d / 0d);
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this instance ideone is behaving correctly if it throws an ArithmeticException for integer division by 0.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Abigail Decan wrote:does online compilers sometimes give different behaviors?
Yes. You can get serious deviations from the behaviour required by the Java® Language Specification because online “compilers” do not handle errors correctly.

While I know nothing about this particular online compiler, I don't think this is a correct generalization. There are various ways to implement such a beast, and it's possible to implement one so it would act precisely as a local compiler would.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you can implement an online compiler to fulfil the requirements of the JLS. But those I have tried did not. Maybe this new one is better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!