From 756131d5c01e3a578d827af60aa950cc12371722 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 10 Sep 2022 12:35:00 +0200 Subject: [PATCH] Doc when splitting a f32. --- ch5-data-in-depth/src/decode_f32.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ch5-data-in-depth/src/decode_f32.rs b/ch5-data-in-depth/src/decode_f32.rs index 74bb478..b4fe497 100644 --- a/ch5-data-in-depth/src/decode_f32.rs +++ b/ch5-data-in-depth/src/decode_f32.rs @@ -16,9 +16,9 @@ pub fn print_f32(n: f32) { 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) } @@ -30,7 +30,7 @@ fn decode(sign: u32, exponent: u32, fraction: u32) -> (f32, f32, f32) { 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; -- 2.45.2