I recently ran into an issue where I wasn't getting the numerical result I expected. I tracked it down to the problem that is illustrated by the following example:
#include <stdio.h>
int main()
{
double sample = .5;
int a = (int)(sample * (1 << 31));
int b = (int)(sample * (1 << 23) * (1 << 8));
printf("a = %#08x, b = %#08x\n", a, b);
}
// Output is: a = 0xc0000000, b = 0x40000000
Why is the result of multiplying by (1 << 31) different than the result of multiplying by (1 << 23) * (1 << 8)? I expected the two to give the same answer but they don't.
I should note that all my floating point values are in the range [-1, 1).