+ 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.

12th Mar 2017, 3:26 AM
Prakhar
Prakhar - avatar
5 odpowiedzi
+ 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.
12th Mar 2017, 5:47 AM
Rrestoring faith
Rrestoring faith - avatar
+ 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.
12th Mar 2017, 5:41 AM
Wen Qin
Wen Qin - avatar
+ 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
12th Mar 2017, 3:56 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Prakhar, because float has a special value for NaN, while int does not
26th Mar 2017, 8:43 PM
Alx vSeti
Alx vSeti - avatar
0
you are still technically dividing by 0 😂 thats not possible
13th Mar 2017, 6:47 PM
Max Diamond
Max Diamond - avatar