#testing #error #approximate #float #comparison

approx_eq

A macro for comparing equality of two values up to an arbitrary error in the relative difference

8 releases

0.1.8 Nov 1, 2020
0.1.7 Oct 28, 2020

#408 in Rust patterns

Download history 88/week @ 2022-06-07 44/week @ 2022-06-14 208/week @ 2022-06-21 60/week @ 2022-06-28 137/week @ 2022-07-05 93/week @ 2022-07-12 67/week @ 2022-07-19 64/week @ 2022-07-26 119/week @ 2022-08-02 251/week @ 2022-08-09 152/week @ 2022-08-16 280/week @ 2022-08-23 238/week @ 2022-08-30 444/week @ 2022-09-06 438/week @ 2022-09-13 238/week @ 2022-09-20

1,376 downloads per month
Used in 76 crates (5 directly)

MIT/Apache

6KB
82 lines

approx_eq

Build Status Crates.io Documentation

This crate provides a macro to check whether two numbers are approximately equal. It does so by checking that the relative difference between the two numbers is less than some upper limit.

To use this in your Rust program, add the following to your Cargo.toml file:

// Cargo.toml
[dependencies]
approx_eq = "0.1"

Using this macro is easy!

// main.rs
use approx_eq::assert_approx_eq;

fn main() {
  assert_approx_eq!(1., 0.99999999999); // should pass
  assert_approx_eq!(1., 0.99999999999, 1e-5); // should pass
  assert_approx_eq!(1., 0.99999999999, 1e-20); // should fail
  assert_approx_eq!(1., 2.) // should fail
}

No runtime deps