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.

The f64 type in Rust is a primitive data type representing a 64-bit, double-precision floating-point number as defined by the IEEE 754-2008 standard. It serves as Rust’s default floating-point type during type inference.

Memory Layout

An f64 occupies exactly 8 bytes (64 bits) of memory. Its internal bitwise architecture is divided into three fields:
  • Sign bit: 1 bit (determines positive or negative).
  • Exponent: 11 bits (determines the magnitude).
  • Mantissa (Significand): 52 bits (determines the precision).

Syntax and Initialization

Rust provides multiple ways to declare an f64 literal. If a floating-point literal lacks a type suffix and the type is not explicitly annotated, the compiler infers f64.
let implicit = 3.14;               // Inferred as f64
let explicit: f64 = 3.1415926535;  // Explicit type annotation
let suffixed = 2.718_f64;          // Type suffix
let scientific = 6.022e23;         // Scientific notation (inferred f64)

Precision and Limits

Because of the 52-bit mantissa, an f64 guarantees a precision of 15 to 17 significant decimal digits. The std::f64 module defines its mathematical boundaries:
  • f64::MAX: ~1.7976931348623157 × 10³⁰⁸
  • f64::MIN: ~-1.7976931348623157 × 10³⁰⁸
  • f64::MIN_POSITIVE: ~2.2250738585072014 × 10⁻³⁰⁸ (smallest positive normal value)
  • f64::EPSILON: ~2.2204460492503131 × 10⁻¹⁶ (difference between 1.0 and the next representable value)

Special IEEE 754 Values

f64 supports special states representing undefined or unrepresentable mathematical operations:
let not_a_number = f64::NAN;          // Result of 0.0 / 0.0
let infinity = f64::INFINITY;         // Result of 1.0 / 0.0
let neg_infinity = f64::NEG_INFINITY; // Result of -1.0 / 0.0

Trait Implementations and Equivalence

A critical technical distinction in Rust is that f64 does not implement the Eq or Ord traits. It only implements PartialEq and PartialOrd. This is a direct consequence of IEEE 754 rules regarding NaN (Not-a-Number). NaN is never equal to anything, including itself.
let x = f64::NAN;
assert!(x != x); // Evaluates to true
Because f64 lacks total equality (Eq) and total ordering (Ord), it cannot be directly used as a key in a HashMap or BTreeMap, nor can a slice of f64 be sorted using the standard .sort() method.

Total Ordering (total_cmp)

To establish a strict weak ordering for floating-point numbers, Rust provides the f64::total_cmp method. This method implements the IEEE 754 totalOrder predicate, allowing f64 values (including NaN) to be safely sorted or used in ordered collections.
let mut floats = [3.1, 1.2, f64::NAN, 4.5];
// floats.sort(); // COMPILER ERROR: f64 does not implement Ord

// Idiomatic, safe approach for sorting f64:
floats.sort_unstable_by(|a, b| a.total_cmp(b));

Standard Library Methods

The f64 primitive has built-in mathematical methods provided by the standard library. Note that in no_std environments (using only the core library), complex mathematical functions (like trigonometry or logarithms) are unavailable by default because they rely on the system’s C math library (libm).
let val = -4.0_f64;
let abs_val = val.abs();           // 4.0
let square_root = abs_val.sqrt();  // 2.0
let power = square_root.powf(3.0); // 8.0
let rounded = 3.7_f64.floor();     // 3.0
Master Rust with Deep Grasping Methodology!Learn More