GithubHelp home page GithubHelp logo

isclose's Introduction

isclose   Test StatusCrate VersionRust Version

This crate provides a set of traits and macros for comparing arbitrary types.

The trait IsClose is implemented by default for f32 and f64.

Additional implementations are also hidden behind the following features:

  • half implements IsClose for half's f16 and bf16
  • euclid implements IsClose for euclid's geometric types

Usage:

use isclose::{IsClose, assert_is_close};

// This will fail!
// assert_eq!(0.1 + 0.2, 0.3)

// This will pass
assert!((0.1 + 0.2).is_close(0.3));

// Equivalent, but gives better error messages
assert_is_close!(0.1 + 0.2, 0.3);

You can also implement IsClose for custom types

use isclose::{IsClose, assert_is_close};
use std::borrow::Borrow;

#[derive(Debug)]
struct Vector { x: f32, y: f32 }

impl IsClose<f32> for Vector {
    // Use the same default tolerances as f32
    // You can override the defaults here if necessary
    const ABS_TOL: f32 = <f32 as IsClose>::ABS_TOL;
    const REL_TOL: f32 = <f32 as IsClose>::REL_TOL;

    fn is_close_tol(
        &self,
        other: impl Borrow<Self>,
        rel_tol: impl Borrow<f32>,
        abs_tol: impl Borrow<f32>,
    ) -> bool {
        let (other, rel_tol, abs_tol) = (other.borrow(), rel_tol.borrow(), abs_tol.borrow());
        self.x.is_close_tol(other.x, rel_tol, abs_tol) &&
            self.y.is_close_tol(other.y, rel_tol, abs_tol)
    }
}

assert_is_close!(
    Vector {
        x: 0.1 + 0.2,
        y: 0.2 + 0.4,
    },
    Vector {
        x: 0.3,
        y: 0.6,
    }
)

Licence

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

isclose's People

Contributors

staticintlucas avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.