In school, we learn that the division is the same as multiplying the inverse (or reciprocal), that is x / y = x * (1/y). In software, this does not work as-is when working with integers since 1/y is not an integer when y is an integer (except for y = 1 or y = -1). However, computers have floating-point numbers which are meant to emulate real numbers. It is reasonable to expect that x / y = x * (1/y) will work out with floating-point numbers. It does not.
I woke up this morning reading a blog post by Reynolds on how to compute the inverse quickly when the divisor is known ahead of time. Reynolds is concerned with performance, but his post reminded me of the fact that, in general, multiplying by the inverse is not the same as dividing.
For example, dividing by 3.1416 and multiplying by 1/3.1416 do not result in the same numbers, when doing the computation on a computer.
> x = 651370000000 > x / 3.1416 * 3.1416 651370000000 > invd = 1 / 3.1416 > x * invd * 3.1416 651370000000.0001
>>> x = 651370000000 >>> x / 3.1416 * 3.1416 651370000000.0 >>> invd = 1 / 3.1416 >>> x * invd * 3.1416 651370000000.0001
Always keep in mind thatÂ floating-point numbers are different from real numbers… for example, half of all floating-point numbers are in the interval [-1,1].