Decode floats bit by bit in numpy
CPU · your Macnotebook90 minrequiredGoal · Write your own FP32, FP16 and BF16 decoder and verify it against numpy and torch on thousands of random values.
- 1Use np.frombuffer / view to get the raw uint bits of a float32 and float16 array.
- 2Write decode(bits, exp_bits, man_bits) that returns the value, handling zero, inf, NaN and subnormals.
- 3Check your decoder against np.float32 and np.float16 on 10,000 random values with np.allclose.
- 4Implement BF16 by truncating or rounding the top 16 bits of an FP32 and decode with 8 exponent / 7 mantissa bits; compare with torch.bfloat16.
- 5Tabulate for each format the max finite value, min positive normal, min subnormal and machine epsilon.
- 6Plot the spacing between adjacent representable values versus magnitude for all three formats on a log-log chart.
- Deliverable
- A notebook with the decoder, the comparison table and the spacing plot.
- Check
- Your decoder agrees with numpy on every random value; your table shows FP16 max 65504 and BF16 epsilon 0.0078125.