+ 3
Why is dividing 0.0 by 0.0 equal to NaN? Why is there no arithmetic exception?
example float x=0.0,y=x;System. out. println(x/y); Output : NaN but int x=0,y=x;System. out. println(x/y); This causes Arithmetic Exception.
5 ответов
+ 5
Floats and doubles use float point arithmetic, and are not technically real numbers. (They are able to return Infinity and NaN 'not a number')
*The float point system may not be 100% accurate* and to assume 0.0 is 0 is to assume the accuracy of the number, which may not be correct.
It sort of makes sense for something to pop up as 1/x as x gets closer to 0 becomes closer to infinity. Although without precision we can't make the assertion of whether or not 1/0.0 is infinity, undefined, or just a very large inaccurate number. Hence returns NaN instead.
+ 4
Because anything divded by 0 is actually infinity. Hence it is wrong to really say its undefined or an error. Neither can the systen give an infinity value. So it just gives NaN.
+ 4
@Wen Qin
This is not entirely true, 1/0 is not infinite. You can only make that claim when you get closer to 0 from the positive side. (from the negative side it would be -infinity, which is the exact opposite). Therefore It would actually be correct to say its undefined.
Floats and doubles are also capable of returning 'infinity' values.
Try the following:
System.out.println((float) Math.pow(Integer.MAX_VALUE, Integer.MAX_VALUE));
output: Infinity
+ 3
Prakhar, because float has a special value for NaN, while int does not
0
you are still technically dividing by 0 😂 thats not possible