With float a = ...;
and float inva = 1/a;
is x / a
the same as x * inva
?
And what is with this case:
unsigned i = ...;
float v1 = static_cast<float>(i) / 4294967295.0f;
float scl = 1.0f / 4294967295.0f;
float v2 = static_cast<float>(i) * scl;
Is v1
equal to v2
for all unsigned integers?
is
v1
equal tov2
for all unsigned integers?
Yes, because 4294967295.0f
is a power of two. Division and multiplication by the reciprocal are equivalent when the divisor is a power of two (assuming the computation of the reciprocal does not overflow or underflow to zero).
Division and multiplication by the reciprocal are not equivalent in general, only in the particular case of powers of two. The reason is that for (almost all) powers of two y
, the computation of 1 / y
is exact, so that x * (1 / y)
only rounds once, just like x / y
only rounds once.
No, the result will not always be the same. The way you group the operands in floating point multiplication, or division in this case, has an effect on the numerical accuracy of the answer. Thus, the product a*(1/b)
might differ from a/b
. Check the wikipedia article http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems.
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