Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 365
Evgeny Lazin Avatar asked Feb 04 '13 07:02

Evgeny Lazin


People also ask

Why does dividing by zero gives infinity?

The thing is something divided by 0 is always undefined because the value has not been defined yet.

What happens if you divide something by 0?

Instead, any number divided by zero is undefined. In fact, even zero divided by zero is undefined! That simply means we don't yet have an answer for the problem.

What is the result of dividing by 0 in R?

In R the following code produces a result of Inf (infinity). In mathematics the result of division by 0 is undefined.

Why is division by zero an error?

As much as we would like to have an answer for "what's 1 divided by 0?" it's sadly impossible to have an answer. The reason, in short, is that whatever we may answer, we will then have to agree that that answer times 0 equals to 1, and that cannot be ​true, because anything times 0 is 0. Created by Sal Khan.


1 Answers

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.

like image 70
Thorsten S. Avatar answered Oct 19 '22 11:10

Thorsten S.