File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Floating point divide by zero

 
Srinivas Kumar
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody explain whether the following statement is true or not.
If true, please explain.

Floating point modulo (%) by zero produces NaN , but floating point divide by zero does not.
 
Harshada Deshmukh
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Its true
Floating point divide by zero produces Infinity & Floating point modulo by zero produces NaN(not a number)
 
JB Ramesh
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does anyone know the reason behind this ?

Why arithmetic exception happens for int and not for float ?
[ March 28, 2007: Message edited by: JB Ramesh ]
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by JB Ramesh:
does anyone know the reason behind this ?

Why arithmetic exception happens for int and not for float ?


The behavior of float is actually defined by the IEEE standard. Java follows the same standard used by most floating point processors in use today.

As for int, it throws an exception, because there is no representation for infinity or NaN.

Henry
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My actual doubt is that floating point module zero also does divide by zero internally. So why Nan is getting printed instead of Infinity?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic