0

Why does int/double result in infinity & not exception?

double d=0; syso(9/d) gives infinity

2nd Jun 2017, 7:29 PM
Bhavesh Sachanandani
Bhavesh Sachanandani - avatar
2 Antworten
+ 4
Doubles are able to return infinity and NaN so I guess there's no reason the throw an error. int on the other hand cannot throw infinity or NaN so dividing with an int should throw an error. As to why it returns infinity, this is my best guess: The binary form of 0 as a double is 000000...etc. However, in most languages the first binary digit is the sign of the number. (if there is a sign) 0 being positive. So, I assume when dividing by 0 the computer just keeps getting a larger and larger value when trying to evaluate the expression and thus throws infinity as the exception with no real errors. Since the sign is technically positive due to how numbers in binary work I can assume it would throw positive infinity. This would also mean -9.0/0 SHOULD throw negative infinity. Notice, this example throws negative infinity: double a = 2; a =- a/0; It SHOULD (maybe?) also mean diving by 0 takes longer to compute than dividing by some other number. No, it is not correct to say 9.0/0 is infinity, but I guess the creators of the language decided there's no reason to always have to check if you're dividing by zero in order to throw the NaN or something to say undefined. That would just add to performance cost. Note* What I've said may not be entirely true, this is just what I can make of it without any research.
2nd Jun 2017, 7:42 PM
Rrestoring faith
Rrestoring faith - avatar
0
thanks bro
3rd Jun 2017, 5:05 AM
Bhavesh Sachanandani
Bhavesh Sachanandani - avatar