# need help

zaghal mohd
Ranch Hand
Posts: 43
any one please explain this only integer division by zero can throw an exception and floating point calculations can produce NaN
thanks

Valentin Crettaz
Gold Digger
Sheriff
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 ]

Deepali Pate
Ranch Hand
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
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
Posts: 43
so in which case we will got NaN

Ranch Hand
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
Posts: 43
thanks friends i tried this today i got the answer as you mention above

see you later with more questions