concept f32 in category rust

appears as: f32, n f32, f32
Rust in Action

This is an excerpt from Manning's book Rust in Action.

Table 2. 1. Rust types for representing scalar (single) numbers

i8, i16, i32, i64

Signed integers ranging from 8-bit to 64-bit

u8, u16, u32, u64

Unsigned integers ranging from 8-bit to 64-bit

f32, f64

Floating point numbers in 32-bit and 64-bit variants

isize, usize

Integers that assume CPU’s “native” width (e.g. in 64-bit CPUs, usize and isize will be 64 bits wide)

To print a value as individual bits, the type of that value must implement fmt::Display. f32 doesn’t implement fmt::Binary, but integer types do. There are two integer types guaranteed to take up the same number of bits as f32, i32 and u32. The decision about which to choose is somewhat arbitrary.[28] Let’s use u32. So, now we need a way to treat an f32 as an u32, without affecting any of the underlying bits. That’s what std::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
}

5.3.2 Looking inside an f32

Figure 5.1. An overview of the three components encoded within the bits of a floating point number. The layout corresponds to the f32 type in Rust, called binary32 within the IEEE 754-2008 standard and single by its predecessor IEE 754-1985.

ch5 f32 overview

The value 42.42 is encoded as f32 with the bit pattern 01000010001010011010111000010100. That bit pattern is more compactly represented as 0x4229AE14. 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 0x4229AE14 as a f32 type

Component name (Variable in Equation 5.1)

Component in binary

Component as base-10 (u32)

Decoded Value

Sign bit (s)

0

0

1

Exponent (t)

10000100

132

5

Mantissa/Significand (m)

01010011010111000010100

2,731,540

5.325625