r/programming • • 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

34 comments sorted by

View all comments

Show parent comment

3

u/vytah Aug 13 '26

A minor nitpick: to actually prevent truncation errors, you need to prepare a little bigger constant than that.

For example, (0x4000'0000*0x13B1'3B14)>>32 is not 0x4000'0000/13, it's 0x4000'0000/13+1.

The compilers will use (x*0x4EC4'EC4F)>>34 instead (0x4EC4'EC4F being 0x4'0000'0000/13+1)