liveBook · Manning

FP32

As explained in Rust in Action: Systems programming concepts and techniques

A single-precision floating-point type is a floating-point type represented with the precision associated with Rust's f32 type.

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

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Decodes sign, exponent, and significand with bit operations, while treating subnormal values and NaN separately from the ordinary-value decoding loop.
Examples
Works through the fields of 42.42 and converts a u8 into an f32 probability with and without division.
What you can do after
Apply Rust bitwise operations to inspect and reconstruct ordinary f32 values.
How it connects
Connects floating-point representation to bitwise operations, scientific notation, type interpretation, and equality.

Also explained in

Compare how 6 books explain FP32 →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.