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
?
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.
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.
So, the multiplicative inverse of 0 doesn't exist.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With