Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

f32 is a primitive data type in Rust representing a 32-bit, single-precision floating-point number. It strictly adheres to the IEEE 754-2008 standard for floating-point arithmetic.

Memory Layout

At the hardware level, an f32 occupies exactly 4 bytes (32 bits) of memory, divided into three components:
  • Sign: 1 bit (determines positive or negative)
  • Exponent: 8 bits (determines the magnitude)
  • Mantissa (Fraction): 23 bits (determines the precision)

Syntax and Initialization

Rust provides multiple ways to instantiate an f32. If a floating-point literal lacks a type suffix or explicit annotation, Rust defaults to f64. Therefore, explicit typing is required to bind a literal to an f32.
// Explicit type annotation
let a: f32 = 3.14;

// Type suffix
let b = 3.14f32;

// Type suffix with an underscore for readability
let c = 3.14_f32;

// Scientific notation
let d = 2.5e-3_f32; // 0.0025

Precision and Constants

Because of the 23-bit mantissa, an f32 provides approximately 6 to 9 significant decimal digits of precision. Operations requiring higher precision will suffer from floating-point rounding errors. The std::f32 module provides associated constants for boundary values:
let max_val = f32::MAX; // 3.40282347e+38
let min_val = f32::MIN; // -3.40282347e+38
let epsilon = f32::EPSILON; // 1.19209290e-07 (Machine epsilon)

Special IEEE 754 Values

f32 supports special non-numeric states defined by the IEEE 754 standard:
let not_a_number = f32::NAN;
let positive_infinity = f32::INFINITY;
let negative_infinity = f32::NEG_INFINITY;

// Checking states
assert!(not_a_number.is_nan());
assert!(positive_infinity.is_infinite());

Trait Implementations and Type System Constraints

The f32 type implements standard mathematical operators (Add, Sub, Mul, Div, Rem) and bitwise copy semantics (Copy, Clone). Crucially, f32 implements PartialEq and PartialOrd, but does not implement Eq or Ord. This is because f32::NAN == f32::NAN evaluates to false, violating the reflexivity requirement of total equality.
let x = f32::NAN;
assert_eq!(x == x, false); // NaN is never equal to NaN
Because it lacks Eq and Ord:
  1. You cannot directly use f32 as a key in a std::collections::HashMap or HashSet.
  2. You cannot use the standard .sort() method on a Vec<f32>. You must use .sort_by() or .sort_unstable_by() with a custom comparator (e.g., f32::total_cmp).

Type Casting

Rust does not perform implicit type coercion. To convert between f32 and other numeric types, you must use the as keyword. Casting from an integer to an f32 may result in precision loss if the integer requires more than 24 bits to represent exactly.
let int_val: i32 = 100;
let float_val: f32 = int_val as f32;

let float_trunc: f32 = 3.99;
let int_trunc: i32 = float_trunc as i32; // Truncates towards zero (results in 3)
Master Rust with Deep Grasping Methodology!Learn More