almost

Trait AlmostEqual

source
pub trait AlmostEqual {
    type Float;

    const DEFAULT_TOLERANCE: Self::Float;
    const MACHINE_EPSILON: Self::Float;

    // Required methods
    fn almost_equals_with(self, rhs: Self, tol: Self::Float) -> bool;
    fn almost_zero_with(self, tol: Self::Float) -> bool;

    // Provided methods
    fn almost_zero(self) -> bool
       where Self: Sized { ... }
    fn almost_equals(self, rhs: Self) -> bool
       where Self: Sized { ... }
}
Expand description

A trait for comparing floating point numbers. Not broadly intended to be used by most code (instead, use the functions at the crate root), however it could be useful for generic code too.

Required Associated Constants§

source

const DEFAULT_TOLERANCE: Self::Float

The default tolerance value for this type. Typically equivalent to T::EPSILON.sqrt(), as we assume that around half of the precision bits of any arbitrary computation have been rounded away.

source

const MACHINE_EPSILON: Self::Float

The machine epsilon for this type. This generally should not be used as a tolerance value (it’s frequently too strict), however it can be useful when computing tolerances.

Required Associated Types§

source

type Float

The floating point type. For f32 and f64 this is Self, but for custom aggregate types it could be different.

Required Methods§

source

fn almost_equals_with(self, rhs: Self, tol: Self::Float) -> bool

Equivalent to almost::equal_with.

const MY_TOLERANCE: f32 = almost::F32_TOLERANCE / 2.0;
assert!(a.almost_equals_with(b, MY_TOLERANCE));
source

fn almost_zero_with(self, tol: Self::Float) -> bool

Equivalent to almost::zero_with.

assert!(0.01.almost_zero_with(0.05));

Provided Methods§

source

fn almost_zero(self) -> bool
where Self: Sized,

Equivalent to almost::zero.

assert!(v.almost_zero());
source

fn almost_equals(self, rhs: Self) -> bool
where Self: Sized,

Equivalent to almost::equal.

assert!(a.almost_equals(b));

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl AlmostEqual for f32

source§

const MACHINE_EPSILON: Self::Float = {transmute(0x34000000): <f32 as AlmostEqual>::Float}

source§

const DEFAULT_TOLERANCE: Self::Float = {transmute(0x39b504f3): <f32 as AlmostEqual>::Float}

source§

type Float = f32

source§

fn almost_equals_with(self, rhs: Self, tol: Self::Float) -> bool

source§

fn almost_zero_with(self, tol: Self::Float) -> bool

source§

impl AlmostEqual for f64

source§

const MACHINE_EPSILON: Self::Float = {transmute(0x3cb0000000000000): <f64 as AlmostEqual>::Float}

source§

const DEFAULT_TOLERANCE: Self::Float = {transmute(0x3e50000000000000): <f64 as AlmostEqual>::Float}

source§

type Float = f64

source§

fn almost_equals_with(self, rhs: Self, tol: Self::Float) -> bool

source§

fn almost_zero_with(self, tol: Self::Float) -> bool

Implementors§