concept f32 in category rust

This is an excerpt from Manning's book Rust in Action.
Table 2. 1. Rust types for representing scalar (single) numbers
i8,i16,i32,i64Signed integers ranging from 8-bit to 64-bit
u8,u16,u32,u64Unsigned integers ranging from 8-bit to 64-bit
f32,f64Floating point numbers in 32-bit and 64-bit variants
isize,usizeIntegers that assume CPU’s “native” width (e.g. in 64-bit CPUs,
usizeandisizewill be 64 bits wide)
To print a value as individual bits, the type of that value must implement
fmt::Display.f32doesn’t implementfmt::Binary, but integer types do. There are two integer types guaranteed to take up the same number of bits asf32,i32andu32. The decision about which to choose is somewhat arbitrary.[28] Let’s useu32. So, now we need a way to treat anf32as anu32, without affecting any of the underlying bits. That’s whatstd::mem::transmute()does. It allows us to tell the compiler that we know best and to treat one value’s data as if it is actually another type.
Listing 5.9.
deconstruct_f32()extract each field of a floating point value with bit manipulation techniques.decode_f32_parts()demonstrates how to convert those fields to the relevant number.f32_from_parts()combines them to create a single decimal number.const BIAS: i32 = 127; #1 const RADIX: f32 = 2.0; #2 fn main() { #3 let n: f32 = 42.42; let (signbit, exponent, fraction) = deconstruct_f32(n); #4 let (sign, exponent, mantissa) = decode_f32_parts(signbit, exponent, fraction); #5 let reconstituted_n = f32_from_parts(sign, exponent, mantissa); #6 println!("{} -> [sign:{}, exponent:{}, mantissa:{:?}] -> {}", n, signbit, exponent, mantissa, reconstituted_n); } fn deconstruct_f32(n: f32) -> (u32, u32, u32) { let n_: u32 = unsafe { std::mem::transmute(n) }; let sign = (n_ >> 31) & 1; #7 let exponent = (n_ >> 23) & 0xff; #8 let fraction = 0b00000000_01111111_11111111_11111111 & n_; #9 (sign, exponent, fraction) #10 } fn decode_f32_parts(sign: u32, exponent: u32, fraction: u32) -> (f32, f32, f32) { let signed_1 = (-1.0_f32).powf(sign as f32); #11 let exponent = (exponent as i32) - BIAS; #12 let exponent = RADIX.powf(exponent as f32); #13 let mut mantissa: f32 = 1.0; #14 for i in 0..23_u32 { #15 let one_at_bit_i = 1 << i; #16 if (one_at_bit_i & fraction) != 0 { #17 mantissa += 2_f32.powf((i as f32) - 23.0); #18 } } (signed_1, exponent, mantissa) } fn f32_from_parts(sign: f32, exponent: f32, mantissa: f32) -> f32 { #19 sign * exponent * mantissa }
Figure 5.1. An overview of the three components encoded within the bits of a floating point number. The layout corresponds to the
f32type in Rust, calledbinary32within the IEEE 754-2008 standard andsingleby its predecessor IEE 754-1985.![]()
The value 42.42 is encoded as
f32with the bit pattern01000010001010011010111000010100. That bit pattern is more compactly represented as0x4229AE14. Here are the values of each of the three fields and what they represent:Table 5.1. The components of 42.42 represented by the bit pattern
0x4229AE14as af32type
Component name (Variable in Equation 5.1)
Component in binary
Component as base-10 (
u32)Decoded Value
Sign bit (s)
00
1
Exponent (t)
10000100132
5
Mantissa/Significand (m)
010100110101110000101002,731,540
5.325625