> ## 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.

# Rust f32

`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`.

```rust theme={"dark"}
// 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:

```rust theme={"dark"}
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:

```rust theme={"dark"}
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.

```rust theme={"dark"}
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.

```rust theme={"dark"}
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)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
