r/programming • u/mttd • Aug 11 '26
Moving integer division to floating-point is trivial
https://marc-b-reynolds.github.io/math/2026/08/10/IntDivByFP.html
112
Upvotes
r/programming • u/mttd • Aug 11 '26
View all comments
Show parent comment
11
u/Dwedit Aug 11 '26 edited Aug 11 '26
Example of dividing by 13:
0x100000000 / 13 = 0x13B13B13
Add 1 to the number to prevent truncation errors: 0x13B13B14
Let's try 160485 / 13 by using multiplication:
160485 * 0x13B13B14 = 0x30390000C0E4
Discard low 32 bits:
0x3039 = 12345
And compilers have done this for a long time, when you divide by a constant, it will generate reciprocal multiplication code instead.