4 ms·
Mandatory additional detailed info: Division by zero in floating point equals to Infinity because in a floating point data structure there is space to represent
by aliswe 6y ago
Mandatory additional detailed info: Division by zero in floating point equals to Infinity because in a floating point data structure there is space to represent such a value. For integers, there is no such information and therefore an exception should be thrown.
For a 32-bit float, the following are true:
Positive infinity: 011111111(and then 23 zeroes)
Negative infinity: 111111111(and then 23 zeroes)
NaN: x11111111(and then anything except all zeroes)
... x being either 0 or 1.
Source: https://en.wikipedia.org/wiki/IEEE_754-1985#Positive_and_negative_infinity https://en.wikipedia.org/wiki/IEEE_754-1985#Positive_and_neg...
- AnIdiotOnTheNet 6y agoMathematically speaking though, infinity is not the correct answer to x/0 anyway. It's just a usefully incorrect result in a lot of situations.
- colejohnson66 6y agoWell, it’s incorrect when dividing by 0, yes. But when you do the limit as the bottom factor approaches 0, it makes sense. So, below, the first one is defined, while the second one isn’t. lim_{x->0+} 1/x == +Infinity 1/0 != (anything)
- corty 6y agoYes, but thats only a small part of the story, your limit only works if the numerator is constant, positive and > 0. Actually, the correct answer for lim{x->0} a/x is really "anything" and "strongly depends on a and da/dx"... So in general, assuming anything like \infty for a division by zero is wrong wrong wrong.
- BeetleB 6y agoIEEE 754 handles all the cases you mentioned - it doesn't always prescribe Inf.
- colejohnson66 6y agoSorry if I wasn’t clear. I was not implying that any a/b where b approaches 0 is always +infinity. I was simply saying that a/0 isn’t defined, but that in the case of 1/x (positive 0), it is somewhat defined as positive infinity.
- nybble41 6y agoThat depends on the numerator: lim_{x->1+} (x^2-1)/(x-1) == 2 Of course ideally evaluating this particular formula for x == 0 would give 0/0 which IEEE FP defines as NaN, but you could also get a non-zero finite number divided by zero due to rounding for x ≅ 1 and then the default result of +Infinity would be very far from the correct answer. Any situation where you might come close to a 0/0 or ∞/∞ term requires very careful consideration since there is very little separation between a NaN result and ±Infinity.
- renewiltord 6y agoThat's true for a lot of IEEE-754. 1/0 is defined in it in a useful way. Just as 1/3 is defined in a useful way that is not equivalent to its mathematical property.
- rcoveson 6y agoIt's not the correct answer for division of the reals. For division of IEEE 754 floats, it is the correct answer, because division in that context is defined that way.