Get the latest tech news

Why Does Integer Addition Approximate Float Multiplication?


Here is a rough approximation of float multiplication (source): float rough_float_multiply(float a, float b) { constexpr uint32_t bias = 0x3f76d000; return bit_cast<float>(bit_cast<uint32_…

Clang compiles this to a branchless version that doesn’t perform too far off from float multiplication. I tried a few analytic ways to arrive at a good constant, but they all gave terrible results when I actually tried this on a bunch of floats. So I just tried many different constants and stuck with the one that gave the least error on 10,000 randomly generated test floats.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of integer addition

integer addition