File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# need help

zaghal mohd
Ranch Hand

Joined: Jun 27, 2002
Posts: 43
any one please explain this only integer division by zero can throw an exception and floating point calculations can produce NaN
thanks

SCJP,SCWCD,CCNA,CCAI,MCP,MCSA,MCSE<br />working on CCNP+SCBCD
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
int i = 3;
int j = i/0; //will throw ArithmeticException
float f = 3.0f;
float g = f/0; //will not throw any ArithmeticException and g's value will be NaN
From JLS 15.17.2 Division Operator /

...
On the other hand, if the value of the divisor in an integer division is 0, then an ArithmeticException is thrown.
[...]
The result of a floating-point division is determined by the specification of IEEE arithmetic:
...
- Division of a nonzero finite value by a zero results in a signed infinity.
[...]
Despite the fact that overflow, underflow, division by zero, or loss of information may occur, evaluation of a floating-point division operator / never throws a run-time exception.

[ July 18, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
____________________________________________
float f = 3.0f;
float g = f/0; //will not throw any ArithmeticException and g's value will be NaN
________________________________________________
Isnt the value of this Infinity ???
and it would be NaN if it was
float f = 0.0f;
float g = f/0;
Deepali
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
you're right Deepali... I'm sorry
the result of
float f = 3.0f;
float g = f/0;
is Infinity and not NaN...
zaghal mohd
Ranch Hand

Joined: Jun 27, 2002
Posts: 43
so in which case we will got NaN
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
We get NaN when trying to divide float 0.0 with an integer 0.
as deepali pointed out earlier
Isnt the value of this Infinity ???
and it would be NaN if it was
float f = 0.0f;
float g = f/0;
[/QUOTE
zaghal mohd
Ranch Hand

Joined: Jun 27, 2002
Posts: 43
thanks friends i tried this today i got the answer as you mention above

see you later with more questions

I agree. Here's the link: http://aspose.com/file-tools

subject: need help