liveBook · Manning

Exponent

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

An exponent is the component of a floating-point representation that determines the scale or power of two applied to its significand.

An exponent sets a number’s scale. In scientific notation, 1.898 × 10²⁷ and 3.801 × 10⁻⁴ use exponents to describe vastly different sizes without writing long strings of digits. A floating-point number uses the same idea, but its radix is 2 rather than 10. Its bits hold a sign, an exponent, and a mantissa (also called a significand); the radix does not need its own bits. section 5.4

The exponent inside 42.42

As an f32, 42.42 has the bit pattern 0x4229AE14. Its stored exponent is 132, but f32 uses a bias of 127: subtracting the bias gives 5. With a mantissa of 1.325625 and a positive sign, the value is 1.325625 × 2⁵ = 42.42. The stored exponent and the power used to calculate the value are therefore not the same number. section 5.4.1

You can extract an ordinary f32 exponent with a few bit operations. Convert the value to its u32 bit pattern, shift right by 23 places to move past the mantissa, and mask with 0xff to keep only the exponent’s eight bits. Subtract 127 to get the power of two. 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;

The Rust listing performs those steps on 42.42 using to_bits(), a right shift, an AND mask, and bias subtraction. section 5.4.3

Check the exponent bits before applying the ordinary decoding steps. All-zero exponent bits change how the mantissa represents numbers near zero; all-one bits indicate infinities or NaN values instead of an ordinary scaled number. section 5.4.4

Choosing a scale for byte-sized input

A bit-built f32 can hold its exponent at −1 while placing an input byte into the mantissa. This initially produces values from 0.5 to 0.998; subtraction and multiplication then produce outputs from 0 to 0.99609375 without division. A fixed exponent controls the scale while the byte changes the significand. section 5.6

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

How deep it goes
Works through biased f32 decoding and bit extraction, then identifies exponent patterns that require special treatment.
Examples
Decodes 42.42 from f32 bits and fixes an exponent while converting input bytes to floating-point values.
What it assumes
Introduces scale through scientific notation before using Rust f32, u32, shifts, and masks to inspect stored bits.
What you can do after
Build it from scratch: extract an f32 exponent with Rust bit operations and use a fixed exponent in a byte-conversion exercise.
How it connects
Connects exponents to scientific notation, significands, radix, exponent bias, subnormal numbers, infinities, and NaN.

Also explained in

Compare how 2 books explain exponent →

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.