In order to prevent DivideByZeroException in C#, people often write things like
double f(double x) {
if (x != 0.0) return 1000.0/x;
else return 0.0;
}
Given the fact that floating-point arithmetic always has imprecisions, I wonder whether it is guaranteed that this function never throws a DivideByZeroException.
The documentation says:
So yes, “it is guaranteed that this function never throws a DivideByZeroException.” – even without any checking, but it may return positive infinity, negative infinity, or Not-a-Number (NaN) even if you check for
0.0, for example when you divide a rather large value by a really small value so that the result exceeds the range covered by double.