#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

#1585 in Rust patterns

Download history 644/week @ 2023-12-06 815/week @ 2023-12-13 637/week @ 2023-12-20 611/week @ 2023-12-27 633/week @ 2024-01-03 648/week @ 2024-01-10 690/week @ 2024-01-17 513/week @ 2024-01-24 518/week @ 2024-01-31 369/week @ 2024-02-07 664/week @ 2024-02-14 573/week @ 2024-02-21 597/week @ 2024-02-28 642/week @ 2024-03-06 727/week @ 2024-03-13 621/week @ 2024-03-20

2,714 downloads per month
Used in 85 crates (13 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