Crates.io | ballpark |
lib.rs | ballpark |
version | |
source | src |
created_at | 2025-05-07 23:38:20.190756+00 |
updated_at | 2025-05-07 23:38:20.190756+00 |
description | Approximate comparisons for floating-point numbers |
homepage | |
repository | https://github.com/SludgePhD/Ballpark |
max_upload_size | |
id | 1664690 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This crate provides approximate equality comparisons for floating-point values and structures composed of floating-point values.
#![no_std]
support, no alloc
needed (just disable the default features).This crate does not aim to provide the following features:
ballpark
themselves and provide their own implementations (potentially behind a Cargo feature).Perform assertions with the assert_approx_eq!
macro:
use ballpark::assert_approx_eq;
// Infamously, 0.1 + 0.2 == 0.30000000000000004 when using double precision.
assert_ne!(0.1 + 0.2, 0.3);
// But they are *approximately* equal.
assert_approx_eq!(0.1 + 0.2, 0.3);
// Like `assert_eq!`, you can provide your own panic message:
let tolerance = 0.25;
assert_approx_eq!(1.0, 1.1, "not within tolerance of {tolerance}").abs(tolerance);
// The type of comparison, and threshold value, can be configured by calling methods on
// the returned assertion guard.
// 3 types of comparison are supported:
// Absolute difference threshold: the difference between the values must be at most X
assert_approx_eq!(0.9, 1.0).abs(0.1);
// Relative difference threshold: the difference must be within a fraction of the bigger input.
assert_approx_eq!(99.0, 100.0).rel(0.01); // Difference must be within 1% of the bigger input.
// Units in the last place: there must be at most N distinct float values between the numbers.
assert_approx_eq!(1.0, 1.0 + f32::EPSILON).ulps(1); // 1 float apart
// If none of these methods are called, a "default" comparison is performed: a `ulps` comparison
// with a threshold of 4 ULPs.
Perform comparisons with approx_eq
:
use ballpark::approx_eq;
// The `approx_eq` function can be used to perform a comparison *without* an assertion:
if !approx_eq(1.0, 1.0) {
eprintln!("something's wrong!");
std::process::abort();
}
let input = 1e-8;
if *approx_eq(input, 0.0).abs(1e-7) {
return; // close enough
}
For full API documentation, please refer to https://docs.rs/ballpark.
This library targets the latest Rust version.
Older Rust versions are supported by equally older versions of this crate. For example, to use a version of Rust that was succeeded 6 months ago, you'd also use an at least 6 month old version of this library.
Compatibility with older Rust versions may be provided on a best-effort basis.