From: Greg Burri Date: Sat, 10 Sep 2022 10:35:00 +0000 (+0200) Subject: Doc when splitting a f32. X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=756131d5c01e3a578d827af60aa950cc12371722;p=rust_in_action.git Doc when splitting a f32. --- 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;