• Post Reply Bookmark Topic Watch Topic
  • New Topic

Operators modulo and division  RSS feed

 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can someone please explain me that following results:

int i = 5;

i % 0 //ArithmeticException
i % 0.0 //NaN....why is this not Arithmetic exception?
i % -0.0 //NaN..why is this not Arithmetic exception?


i / 0 //ArithmeticException
i / 0.0 //Infinity..why is this not Arithmetic exception?
i / -0.0 //-Infinity..why is this not Arithmetic exception?


Thanks
 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the Java Language Specification, especially 15.17.2 Division Operator and 15.17.3 Remainder Operator %.

Regards,
Uli
 
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

i % 0.0 //NaN....why is this not Arithmetic exception?
i % -0.0 //NaN..why is this not Arithmetic exception?
i / 0.0 //Infinity..why is this not Arithmetic exception?
i / -0.0 //-Infinity..why is this not Arithmetic exception?


Java follows the IEEE floating point standard. This is also the same standard used by practically every modern processor and programming language today.

The result is defined by IEEE. As for integers, there is no define value of NaN or Infinity for ints, so they have to throw an exception.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!