Code Example Template
let speed: f32 = 1.0;
let pi: f64 = 3.1415926535;

Deep-Dive System Documentation

Floating-Point Primitives

Rust supports two primitive types for floating-point numbers, represented by the IEEE 754 standard:

  • f32: Single-precision float (4 bytes). Good for low-level math or graphics shaders where memory is crucial.
  • f64: Double-precision float (8 bytes). Default float type in Rust, offering high-fidelity calculations.
let speed: f32 = 3.402e38; // Max limit of single-precision float
let precision: f64 = 0.1 + 0.2; // 0.30000000000000004 due to binary float representation

Useful Methods

.abs() -> Self

Computes the absolute value of the floating-point number.

let negative = -5.0f32;
assert_eq!(negative.abs(), 5.0f32);

.sqrt() -> Self

Computes the square root of the number. Returns NaN if the value is negative.

let val = 9.0f64;
assert_eq!(val.sqrt(), 3.0f64);

.floor() -> Self

Returns the largest integer less than or equal to the number.

let val = 3.7f64;
assert_eq!(val.floor(), 3.0f64);

.ceil() -> Self

Returns the smallest integer greater than or equal to the number.

let val = 3.1f64;
assert_eq!(val.ceil(), 4.0f64);

.round() -> Self

Returns the nearest integer to the number. Rounds half-way cases away from $0$.

let val = 3.5f64;
assert_eq!(val.round(), 4.0f64);

.is_nan() -> bool

Returns true if the number is NaN (Not a Number).

let nan = f64::NAN;
assert!(nan.is_nan());

Quick Reference Guide

let val: f64 = 4.0;
assert_eq!(val.sqrt(), 2.0);