FP32, or single-precision floating point, represents decimal values using 32 bits. In Rust, the type is f32; its IEEE 754 layout is called binary32. section 5.4.1 section 5.8
An f32 has three fields: a sign bit, an 8-bit exponent, and a 23-bit fraction. For ordinary values, the sign, decoded exponent, and significand combine like binary scientific notation: sign × significand × 2^exponent. section 5.4.1 section 5.4.5
Looking inside 42.42
The f32 value written as 42.42 has the bit pattern 01000010001010011010111000010100, or 0x4229AE14. Its sign bit is 0, and its exponent field is 132; subtracting the bias of 127 gives 5. The significand is approximately 1.325625, so the decoded value is approximately 1 × 1.325625 × 2^5 = 42.42. section 5.4.1 section 5.4.3
Listing 5.8 Isolating and decoding the exponent from an f32
1 let n: f32 = 42.42; 2 let n_bits: u32 = n.to_bits(); 3 let exponent_ = n_bits >> 23; 4 let exponent_ = exponent_ & 0xff; 5 let exponent = (exponent_ as i32) - 127;
Use to_bits() to inspect the representation as a u32, then shift and mask to extract the exponent. section 5.4.3
To decode an ordinary significand, start at 1.0 for its implicit leading bit. For each set fraction bit at position i, add 2^(i−23). This procedure does not cover special cases. section 5.4.4
Listing 5.9 Isolating and decoding the mantissa from an f32
let n: f32 = 42.42;
let n_bits: u32 = n.to_bits();
let mut mantissa: f32 = 1.0;
for i in 0..23 {
let mask = 1 << i;
let one_at_bit_i = n_bits & mask;
if one_at_bit_i != 0 {
let i_ = i as f32;
let weight = 2_f32.powf( i_ - 23.0 );
mantissa += weight;
}
}
This Rust loop tests each fraction bit and adds its weight to the significand. section 5.4.4
Some bit patterns need special handling. Subnormal values extend the representable range near zero; other patterns represent infinity or NaN. Equality can also be surprising: positive and negative zero compare equal despite different bits, while a NaN compares unequal even to itself. section 5.4.4 section 5.8
Turn a byte into a probability
A simple conversion divides a u8 by 255.0 to produce an f32 between 0 and 1. An alternative fixes the exponent, places the byte in the fraction, and adjusts the result; its maximum input produces 0.99609375 rather than 1.0. section 5.6
Listing 5.16 Generating f32 values in interval [0,1] from a u8
fn mock_rand(n: u8) -> f32 {
let base: u32 = 0b0_01111110_00000000000000000000000;
let large_n = (n as u32) << 15; #1
let f32_bits = base | large_n; #2
let m = f32::from_bits(f32_bits); #3
2.0 * ( m - 0.5 ) #4
}
See the bit-based conversion using f32::from_bits(). section 5.6





