| Crates.io | floatguard |
| lib.rs | floatguard |
| version | 0.1.2 |
| created_at | 2025-07-10 05:03:12.704573+00 |
| updated_at | 2025-07-14 18:35:35.947827+00 |
| description | A library for checked floating-point arithmetic in Rust, safely elminating NaN and Infinity from floating-point computations. |
| homepage | https://github.com/stevethedev/floatguard-rs |
| repository | https://github.com/stevethedev/floatguard-rs |
| max_upload_size | |
| id | 1745889 |
| size | 218,867 |
floatguard)A low-cost, zero-overhead wrapper around f64 that eliminates the risk of NaN and infinity in floating-point
computations.
Floating-point math in Rust (and most languages) silently permits values like NaN, INFINITY, and -INFINITY. These
values conform to the IEEE 754 standard and exist to support interoperability with C libraries and hardware.
However, in many applications, they behave like floating-point landmines — silently propagating through computations and causing unexpected or confusing results. They're the numerical equivalent of a null pointer or an uncaught exception.
floatguard introduces two types to address this problem explicitly:
GuardedF64: A wrapper around f64 that guarantees the value is finite — it will never contain NaN,
INFINITY, or -INFINITY. Any operation that could produce an invalid value returns an UnguardedF64 instead.UnguardedF64: A lightweight wrapper that may contain invalid states like NaN or infinities. It allows chained
operations without incurring a validity check at every step. Before use, the value must be validated with .check(),
which either returns a valid GuardedF64 or an error.This model provides both safety and performance, by enabling deferred validation and avoiding silent propagation of invalid values.
use floatguard::GuardedF64;
fn main() {
let a = GuardedF64::new(1.0).unwrap();
let b = GuardedF64::new(2.0).unwrap();
// Valid arithmetic
let c = a + b; // c is UnguardedF64(3.0)
match c.check() {
Ok(valid) => println!("Valid result: {valid}"),
Err(e) => println!("Error: {e}"),
}
// Invalid arithmetic
let d = c / GuardedF64::new(0.0).unwrap(); // d is UnguardedF64(NaN)
match d.check() {
Ok(valid) => println!("Valid result: {valid}"),
Err(e) => println!("Error: {e}"),
}
}
Output:
Valid result: 3
Error: The floating-point value is poisoned
GuardedF64 and GuardedF32 never contain NaN or infinities.UnguardedF64 and UnguardedF32 allow efficient math, checked only when needed.+, -, *, /, +=, -=, etc.TryFrom<f64>, Into<f64>, and more.#![no_std] compatible.std (default) — Enables std-based functionality (currently unused but reserved for future expansion)..check() to convert an "unguarded" value into a "guarded" one, or catch an error if the value is invalid.Minimum Supported Rust Version: 1.85.1
You may choose between the following licenses for this crate: