Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the multiplicative inverse of 0 result in infinity?

I am writing a program in Swift that takes the multiplicative inverse of random bytes. Sometimes, the byte is 0, and when the multiplicative inverse is taken, it results in inf.

The multiplicative inverse is being determined using

powf(Float(byte), -1.0)

byte is of type UInt8. If byte is equal to 0, the result is inf as mentioned earlier. How would the multiplicative inverse of 0 be infinity? Wouldn't the multiplicative inverse also be 0 since 0/0's multiplicative inverse is 0/0?

like image 771
Todd Avatar asked Apr 13 '21 15:04

Todd


People also ask

Is the inverse of 0 infinity?

In other words, ∞ is an undefined symbol. If you're using the projectively extended real line or the Riemann sphere, then the reciprocal of zero is infinity, and the reciprocal of infinity is zero. In other words, 1/0=∞ and 1/∞=0.

Why is it impossible to find the multiplicative inverse of zero?

The product of any real number with zero is zero. So we can say that multiplicative inverse of 0 does not exist or undefined since division by zero is not defined.

What is the multiplicative inverse of 0?

So, the multiplicative inverse of 0 doesn't exist.

What is the multiplicative inverse of infinity?

The multiplicative inverse of 0 is infinity. The number 0 does not have reciprocal because the product of any number and zero is equal to zero.


2 Answers

Short answer: By definition. In Swift (and many other languages), floating point numbers are backed by IEEE-754 definition of floats, which is directly implemented by the underlying hardware in most cases and thus quite fast. And according to that standard, division by 0 for floats is defined to be Infinity, and Swift is merely returning that result back to you. (To be precise, 0/0 is defined to be NaN, any positive number divided by 0 is defined to be Infinity, and any negative number divided by 0 is defined to be -Infinity.)

An interesting question to ask might be "why?" Why does IEEE-754 define division by 0 to be Infinity for floats, where one can reasonably also expect the machine to throw an error, or maybe define it as NaN (not-a-number), or perhaps maybe even 0? For an analysis of this, you should really read Kahan's (the designer of the semantics behind IEEE-754) own notes regarding this matter. Starting on page 10 of the linked document, he discusses why the choice of Infinity is preferable for division-by-zero, which essentially boils down to efficient implementation of numerical algorithms since this convention allows skipping of expensive tests in iterative numerical analysis. Start reading on page 10, and go through the examples he discusses, which ends on top of page 14.

To sum up: Floating point division by 0 is defined to be Infinity by the IEEE-754 standard, and there are good reasons for making this choice. Of course, one can imagine different systems adopting a different answer as well, depending on their particular need or application area; but then they wouldn't be IEEE-754 compliant.

like image 147
alias Avatar answered Oct 13 '22 16:10

alias


Plugging in 0 just means it is 0 divided by some positive number. Then, the multiplicative inverse will be dividing by 0. As you probably know, this is undefined in mathematics, but in swift, it tries to calculate it. Essentially, it keeps subtracting 0 from the number, but never gets a result, so it will output infinity.

Edit: As Alias pointed out, Swift is not actually going through that process of continually subtracting 0. It will just return infinity anytime it is supposed to divide by 0.

like image 1
lizard_heart Avatar answered Oct 13 '22 16:10

lizard_heart