Is division by zero possible in the following case due to the floating point error in the subtraction?
float x, y, z;
...
if (y != 1.0)
z = x / (y - 1.0);
In other words, is the following any safer?
float divisor = y - 1.0;
if (divisor != 0.0)
z = x / divisor;
This will prevent you from dividing by exactly zero, however that does not mean still won’t end up with
+/-infas a result. The denominator could still be small enough so that the answer is not representable with adoubleand you will end up with aninf. For example:In this program
smallis non-zero, but it is so small thatlargeexceeds the range ofdoubleand isinf.