fn to_parts(n: f32) -> (u32, u32, u32) {
let bits = n.to_bits();
- let sign = (bits >> 31) & 1;
- let exponent = (bits >> 23) & 0xFF;
- let fraction = bits & 0x7F_FF_FF;
+ let sign = (bits >> 31) & 1; // 1 bit.
+ let exponent = (bits >> 23) & 0xFF; // 8 bits.
+ let fraction = bits & 0x7F_FF_FF; // 23 bits.
(sign, exponent, fraction)
}
let mut mantissa: f32 = 1.0;
- // Mantissa is a sum of 1 + 2^(-1) + 2^(-2) + .. + 2^(-23)
+ // Mantissa is a sum of 1 + n1 * 2^(-1) + n2 * 2^(-2) + .. + n23 * 2^(-23)
for i in 0..23 {
let mask = 1 << i;
let one_at_bit_i = fraction & mask;