approx_det 2.0.0

Approximate floating point equality comparisons and assertions.
Documentation
// Copyright (C) 2020-2025 glam-det authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::AbsDiffEq;
use core::cell;
#[cfg(feature = "num-complex")]
use num_complex::Complex;

/// Equality comparisons between two numbers using both the absolute difference and ULPs
/// (Units in Last Place) based comparisons.
pub trait UlpsEq<Rhs = Self>: AbsDiffEq<Rhs>
where
    Rhs: ?Sized,
{
    /// The default ULPs to tolerate when testing values that are far-apart.
    ///
    /// This is used when no `max_ulps` value is supplied to the [`ulps_eq`] macro.
    fn default_max_ulps() -> u32;

    /// A test for equality that uses units in the last place (ULP) if the values are far apart.
    fn ulps_eq(&self, other: &Rhs, epsilon: Self::Epsilon, max_ulps: u32) -> bool;

    /// The inverse of [`UlpsEq::ulps_eq`].
    fn ulps_ne(&self, other: &Rhs, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
        !Self::ulps_eq(self, other, epsilon, max_ulps)
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Base implementations
///////////////////////////////////////////////////////////////////////////////////////////////////

fn f32_signum(x: &f32) -> f32 {
    #[cfg(all(feature = "std", not(feature = "libm_force")))]
    let ret = x.signum();
    #[cfg(any(
        feature = "libm_force",
        all(feature = "libm_fallback", not(feature = "std"))
    ))]
    let ret = if x.is_nan() {
        f32::NAN
    } else {
        libm::copysignf(1.0_f32, *x)
    };
    ret
}

fn f64_signum(x: &f64) -> f64 {
    #[cfg(all(feature = "std", not(feature = "libm_force")))]
    let ret = x.signum();
    #[cfg(any(
        feature = "libm_force",
        all(feature = "libm_fallback", not(feature = "std"))
    ))]
    let ret = if x.is_nan() {
        f64::NAN
    } else {
        libm::copysign(1.0_f64, *x)
    };
    ret
}

// Implementation based on: [Comparing Floating Point Numbers, 2012 Edition]
// (https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/)
impl UlpsEq for f32 {
    #[inline]
    fn default_max_ulps() -> u32 {
        4
    }

    #[inline]
    fn ulps_eq(&self, other: &f32, epsilon: f32, max_ulps: u32) -> bool {
        // For when the numbers are really close together
        if f32::abs_diff_eq(self, other, epsilon) {
            return true;
        }

        // Trivial negative sign check
        if f32_signum(self) != f32_signum(other) {
            return false;
        }

        // ULPS difference comparison
        let int_self: u32 = self.to_bits();
        let int_other: u32 = other.to_bits();

        // To be replaced with `abs_sub`, if
        // https://github.com/rust-lang/rust/issues/62111 lands.
        if int_self <= int_other {
            int_other - int_self <= max_ulps
        } else {
            int_self - int_other <= max_ulps
        }
    }
}

impl UlpsEq for f64 {
    #[inline]
    fn default_max_ulps() -> u32 {
        4
    }

    #[inline]
    fn ulps_eq(&self, other: &f64, epsilon: f64, max_ulps: u32) -> bool {
        // For when the numbers are really close together
        if f64::abs_diff_eq(self, other, epsilon) {
            return true;
        }

        // Trivial negative sign check
        if f64_signum(self) != f64_signum(other) {
            return false;
        }

        // ULPS difference comparison
        let int_self: u64 = self.to_bits();
        let int_other: u64 = other.to_bits();

        // To be replaced with `abs_sub`, if
        // https://github.com/rust-lang/rust/issues/62111 lands.
        if int_self <= int_other {
            int_other - int_self <= max_ulps as u64
        } else {
            int_self - int_other <= max_ulps as u64
        }
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Derived implementations
///////////////////////////////////////////////////////////////////////////////////////////////////

impl<'a, T: UlpsEq + ?Sized> UlpsEq for &'a T {
    #[inline]
    fn default_max_ulps() -> u32 {
        T::default_max_ulps()
    }

    #[inline]
    fn ulps_eq(&self, other: &&'a T, epsilon: T::Epsilon, max_ulps: u32) -> bool {
        T::ulps_eq(*self, *other, epsilon, max_ulps)
    }
}

impl<'a, T: UlpsEq + ?Sized> UlpsEq for &'a mut T {
    #[inline]
    fn default_max_ulps() -> u32 {
        T::default_max_ulps()
    }

    #[inline]
    fn ulps_eq(&self, other: &&'a mut T, epsilon: T::Epsilon, max_ulps: u32) -> bool {
        T::ulps_eq(*self, *other, epsilon, max_ulps)
    }
}

impl<T: UlpsEq + Copy> UlpsEq for cell::Cell<T> {
    #[inline]
    fn default_max_ulps() -> u32 {
        T::default_max_ulps()
    }

    #[inline]
    fn ulps_eq(&self, other: &cell::Cell<T>, epsilon: T::Epsilon, max_ulps: u32) -> bool {
        T::ulps_eq(&self.get(), &other.get(), epsilon, max_ulps)
    }
}

impl<T: UlpsEq + ?Sized> UlpsEq for cell::RefCell<T> {
    #[inline]
    fn default_max_ulps() -> u32 {
        T::default_max_ulps()
    }

    #[inline]
    fn ulps_eq(&self, other: &cell::RefCell<T>, epsilon: T::Epsilon, max_ulps: u32) -> bool {
        T::ulps_eq(&self.borrow(), &other.borrow(), epsilon, max_ulps)
    }
}

impl<A, B> UlpsEq<[B]> for [A]
where
    A: UlpsEq<B>,
    A::Epsilon: Clone,
{
    #[inline]
    fn default_max_ulps() -> u32 {
        A::default_max_ulps()
    }

    #[inline]
    fn ulps_eq(&self, other: &[B], epsilon: A::Epsilon, max_ulps: u32) -> bool {
        self.len() == other.len()
            && Iterator::zip(self.iter(), other)
                .all(|(x, y)| A::ulps_eq(x, y, epsilon.clone(), max_ulps))
    }
}

#[cfg(feature = "num-complex")]
impl<T: UlpsEq> UlpsEq for Complex<T>
where
    T::Epsilon: Clone,
{
    #[inline]
    fn default_max_ulps() -> u32 {
        T::default_max_ulps()
    }

    #[inline]
    fn ulps_eq(&self, other: &Complex<T>, epsilon: T::Epsilon, max_ulps: u32) -> bool {
        T::ulps_eq(&self.re, &other.re, epsilon.clone(), max_ulps)
            && T::ulps_eq(&self.im, &other.im, epsilon, max_ulps)
    }
}