#approximate #equality #comparison

approx_collections

Collections using approximate floating-point comparison

7 stable releases (3 major)

Uses new Rust 2024

4.0.0 Jan 29, 2026
3.2.0 Jan 25, 2026
3.1.0 Jul 20, 2025
2.0.0 Jul 17, 2025
1.1.0 Jul 15, 2025

#799 in Data structures


Used in cga2d

MIT/Apache

74KB
1.5K SLoC

approx_collections

Collections using approximate floating-point comparison.

See the docs for a list of collections and crate features.

Testing

I recommend setting the environment variable PROPTEST_CASES to 1_000_000 to test more cases (default is only 256) and running tests using cargo test --release.


lib.rs:

Data structures using approximate floating-point comparisons.

Precision is the basic struct used by everything in this crate.

FloatPool is used for interning floats via ApproxInternable to reduce accumulated numerical error and allow direct comparison and hashing via ApproxHash.

ApproxHashMap is used for looking up approximate values.

For implementing approximate comparison on your own types, see ApproxEq, ApproxEqZero, and ApproxOrd.

Example

const APPROX: Precision = Precision::DEFAULT;

assert_ne!(0.1 + 0.2, 0.3_f64);
assert!(APPROX.eq(0.1 + 0.2, 0.3_f64));

Implementation

See Precision for details about how floats are compared.

Features

The rustc-hash feature is enabled by default, and uses a faster hashing algorithm for the hash map inside FloatPool.

The derive feature is enabled by default, and provides derive macros for ApproxEq, ApproxEqZero, and ApproxInternable.

Dependencies

~180KB