I was looking at how Numpy implements the random
module and saw the following function to generate a float32
from a random uint32
:
static NPY_INLINE float next_float(bitgen_t *bitgen_state) {
return (next_uint32(bitgen_state) >> 8) * (1.0f / 16777216.0f);
}
I don't get why they multiply by (1.0f / 16777216.0f)
here, instead of simply dividing by 16777216.0f
.
Edit: As we can see from compiling the two ways of writing this function, there seems to be no difference in the generated code. So, this does not seem to be a case of "float multiplication is faster than float division".