File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Floating point divide by zero Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Floating point divide by zero" Watch "Floating point divide by zero" New topic
Author

Floating point divide by zero

Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
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

Joined: Mar 25, 2007
Posts: 12
Hi
Its true
Floating point divide by zero produces Infinity & Floating point modulo by zero produces NaN(not a number)
JB Ramesh
Greenhorn

Joined: Jan 09, 2007
Posts: 20
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
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
My actual doubt is that floating point module zero also does divide by zero internally. So why Nan is getting printed instead of Infinity?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Floating point divide by zero