#testing #approximate #error #comparison #float

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

#1590 in Rust patterns

Download history 510/week @ 2024-01-01 736/week @ 2024-01-08 570/week @ 2024-01-15 587/week @ 2024-01-22 533/week @ 2024-01-29 453/week @ 2024-02-05 552/week @ 2024-02-12 585/week @ 2024-02-19 561/week @ 2024-02-26 690/week @ 2024-03-04 651/week @ 2024-03-11 728/week @ 2024-03-18 1038/week @ 2024-03-25 849/week @ 2024-04-01 758/week @ 2024-04-08 804/week @ 2024-04-15

3,468 downloads per month
Used in 87 crates (15 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