Why does division by zero in IEEE754 standard results in Infinite value?

10,482

Solution 1

It's a nonsense from the mathematical perspective.

Yes. No. Sort of.

The thing is: Floating-point numbers are approximations. You want to use a wide range of exponents and a limited number of digits and get results which are not completely wrong. :)

The idea behind IEEE-754 is that every operation could trigger "traps" which indicate possible problems. They are

  • Illegal (senseless operation like sqrt of negative number)
  • Overflow (too big)
  • Underflow (too small)
  • Division by zero (The thing you do not like)
  • Inexact (This operation may give you wrong results because you are losing precision)

Now many people like scientists and engineers do not want to be bothered with writing trap routines. So Kahan, the inventor of IEEE-754, decided that every operation should also return a sensible default value if no trap routines exist.

They are

  • NaN for illegal values
  • signed infinities for Overflow
  • signed zeroes for Underflow
  • NaN for indeterminate results (0/0) and infinities for (x/0 x != 0)
  • normal operation result for Inexact

The thing is that in 99% of all cases zeroes are caused by underflow and therefore in 99% of all times Infinity is "correct" even if wrong from a mathematical perspective.

Solution 2

I'm not sure why you would believe this to be nonsense.

The simplistic definition of a / b, at least for non-zero b, is the unique number of bs that has to be subtracted from a before you get to zero.

Expanding that to the case where b can be zero, the number that has to be subtracted from any non-zero number to get to zero is indeed infinite, because you'll never get to zero.

Another way to look at it is to talk in terms of limits. As a positive number n approaches zero, the expression 1 / n approaches "infinity". You'll notice I've quoted that word because I'm a firm believer in not propagating the delusion that infinity is actually a concrete number :-)

NaN is reserved for situations where the number cannot be represented (even approximately) by any other value (including the infinities), it is considered distinct from all those other values.

For example, 0 / 0 (using our simplistic definition above) can have any amount of bs subtracted from a to reach 0. Hence the result is indeterminate - it could be 1, 7, 42, 3.14159 or any other value.

Similarly things like the square root of a negative number, which has no value in the real plane used by IEEE754 (you have to go to the complex plane for that), cannot be represented.

Solution 3

In mathematics, division by zero is undefined because zero has no sign, therefore two results are equally possible, and exclusive: negative infinity or positive infinity (but not both).

In (most) computing, 0.0 has a sign. Therefore we know what direction we are approaching from, and what sign infinity would have. This is especially true when 0.0 represents a non-zero value too small to be expressed by the system, as it frequently the case.

The only time NaN would be appropriate is if the system knows with certainty that the denominator is truly, exactly zero. And it can't unless there is a special way to designate that, which would add overhead.

Share:
10,482

Related videos on Youtube

Evgeny Lazin
Author by

Evgeny Lazin

Updated on June 06, 2022

Comments

  • Evgeny Lazin
    Evgeny Lazin almost 2 years

    I'm just curious, why in IEEE-754 any non zero float number divided by zero results in infinite value? It's a nonsense from the mathematical perspective. So I think that correct result for this operation is NaN.

    Function f(x) = 1/x is not defined when x=0, if x is a real number. For example, function sqrt is not defined for any negative number and sqrt(-1.0f) if IEEE-754 produces a NaN value. But 1.0f/0 is Inf.

    But for some reason this is not the case in IEEE-754. There must be a reason for this, maybe some optimization or compatibility reasons.

    So what's the point?

    • Kirby
      Kirby about 11 years
      There is some interesting discussion on this in this pdf: cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF.
    • Michael Pankov
      Michael Pankov about 11 years
      Both limits, approaching from left and from right, are defined. That's why it's commonly accepted that result is infinity.
    • phuclv
      phuclv over 10 years
      why nonsense? From a mathematical perspective lim[x→0](1/x) = ∞
    • Evgeny Lazin
      Evgeny Lazin over 10 years
      Right limit is ∞, left limit is -∞ and function f(x) = 1/x doesn't exist at f(0).
  • Evgeny Lazin
    Evgeny Lazin about 11 years
    On the other hand if 1/0 is Inf than Inf*0 must be equal to 1 but this is not the case :)
  • paxdiablo
    paxdiablo about 11 years
    @Lazin, while that is humorous, it doesn't actually work that way since "infinity" is a concept meaning "beyond number", it isn't actually a number itself. So, while 1/0 gives you something beyond number, an infinite number of zeros will always be 0. Now my head is about to explode because all that transfinite rubbish I forgot so long ago is flooding my cranium - I'll have to invoice you for removing the gore and brain matter from my keyboard :-)
  • Evgeny Lazin
    Evgeny Lazin about 11 years
    The last sentence sound very pragmatic for me.
  • supercat
    supercat about 11 years
    The level of numerical "sensibility" could be improved if literal zero, as well as the results of positive underflow, negative underflow, and subtraction of indistinguishable values, were all distinct, but adding such additional forms of "zero" would have complicated floating-point hardware. The IEEE standard merges the above into two forms of zero, which causes some things that should be NaN to become +INF. I suspect the rationale was that having division by +/- zero yield +/- INF instead of NaN wouldn't add much hardware, and would occasionally be useful.
  • Patricia Shanahan
    Patricia Shanahan about 11 years
    @paxdiablo Whether infinity is part of an arithmetic system depends entirely on how that system is defined. Consider projective and extended real line arithmetic.
  • Evgeny Lazin
    Evgeny Lazin about 11 years
    Yes, and this clarifies rationale behind signed zero in IEEE standard.
  • Jason
    Jason over 8 years
    Just to add, Numberphile (youtube.com/watch?v=BRRolKTlF6Q) has a great video on why x / 0 should not be considered infinity including addressing the thought that "I can take 0 from x an infinite number of times". The summary is that form a algebra point of view, you are are never supposed to say something is "equal to infinity", since at you point out it's not a number.
  • Daan Wilmer
    Daan Wilmer over 8 years
    "The number of 0s that has to be subtracted from any non-zero number to get to zero is indeed infinite." — no, because you /still/ don't get to zero after infinitely many zero-subtractions.
  • paxdiablo
    paxdiablo over 8 years
    @Daan, introducing infinity into the mix is usually a game changer in the real world (not necessarily in a "pure" maths arena but certainly when you need it to apply to reality of some description). I considered it a given that you'll never get to zero since doing something an infinite number of times will last for eternity. However, that's yet another troubling concept for reality :-) I'll clarify your point in the answer.
  • 2072
    2072 over 8 years
    "since doing something an infinite number of times will last for eternity." That's the wrong way to think, time is not part of the equation here. You don't do something an infinite number of times, there are an infinite number of things, they already all exist - that's the important thing to grasp about the infinity concept in maths. It's the same problem when people debate on whether 0.99... is equals to 1 or not. If you simply accept that the number of 9 is infinite then it's easy to understand that there is no other number between 0.99.... and 1 and so 0.99... is just another way to write 1
  • paxdiablo
    paxdiablo over 8 years
    @2072, if you subtract zero from ten continuously until you reach zero, that is indeed an infinite number of times (or actions or operations, if you'd prefer a different term - I wanted to make sure it wasn't being misconstrued as something to do with time itself). It's not an infinite number of things at all.
  • Walter Tross
    Walter Tross over 8 years
    @supercat you should definitely write your comment as an answer
  • Rob
    Rob over 8 years
    @paxdiablo your answer puts into words the arithmetically correct property that a / b = c is the same as a - c*b = 0. However, your argument that if b = 0 then c = +Inf, does not seem logical. This would suggest that c or +Inf is the exact number of 0s that must be subtracted from a to arrive at zero (and not past zero into negatives). That means that +Inf must change its value depending on the context (i.e. as a changes). No number has that property, so therefore +Inf is not a number: +Inf == NaN
  • Rob
    Rob over 8 years
    I think the technical argument from @Thorsten makes sense, but the mathematical argument is, indeed, nonsense.
  • paxdiablo
    paxdiablo over 8 years
    @Rob, you're making the same mistake that so many do in suggesting that infinity is some sort of concrete number. It is not. Instead, it is a concept meaning 'beyond any number'. That is why I prefer to say that something is infinite rather than it being 'equal' to infinity - the latter term has been corrupted in common usage. c will always be infinite regardless of the value of a (assuming the latter is non-zero of course).
  • Rob
    Rob over 8 years
    My point is that your answer suggests: The simplistic definition of a / b is the number of bs that has to be subtracted from a before you get to zero. Yet the point being made is that Infinity is N ot a N umber
  • paxdiablo
    paxdiablo over 8 years
    @Rob, okay, I see where you're coming from. I've changed the answer to clarify that bit. Hopefully it makes more sense.
  • Cubic
    Cubic about 6 years
    "When n==0, then you have no limit at all since, approached form the left, you get negative infinity and, approached from the right, you get positive infinity. From calculus, when the left and right limits do not match you have no limit at all" I'm astonished that you do remember this much, and somehow still get it wrong in your first sentence. Indeed, the limit is undefined if the "left" and "right" limits disagree... but this is the case for any n, not just 0. It's not uncommon to get these wrong, but please don't lecture people on subjects you don't understand.
  • Brick
    Brick about 6 years
    @Cubic There is something wrong here, so I'll either correct or delete shortly. Interestingly though, especially considering your nasty tone, your comment is also wrong. When n==0 in the expression that I wrote, the limit is 0. That's a special case where the limit is defined because, in that case, the left and right limits do agree.
  • Cubic
    Cubic about 6 years
    Haha, maybe I should've taken my own advice then ;) Not sure what you mean by nasty tone though.
  • Cubic
    Cubic about 6 years
    Again, I don't quite see it. I meant it quite literally, don't answer questions you don't have the answer to, otherwise you'll just perpetuate common misconceptions. I try to abide by that myself, but obviously sometimes you're just wrong without realising it (like I helpfully demonstrated here myself).
  • Brick
    Brick about 6 years
    @Cubic See if you like this one version. If so, I propose that we each delete our comments on the old answer. (I'll take an up-vote or the disappearance of your comments as agreement!) :) If still not precise enough, I'm open to another revision or abandoning it. (Despite my mistake, I do know something here....)