Rust · Platform Notes

Platform Notes

use numkong::AllCloseOps;use numkong::*; 8.0.0-rc.1 · b1f866ab

§ Tolerance Comparison

Exact floating-point equality is rarely what you want after arithmetic. allclose() checks every element pair with the formula:

∣a−b∣≤atol+rtol⋅∣b∣|a - b| \leq \text{atol} + \text{rtol} \cdot |b|

Available on Vector, VectorView, VectorSpan, Tensor, TensorView, and TensorSpan. For tensors, allclose is provided by the AllCloseOps trait — import it if calling on a TensorRef implementor. Shape mismatch returns false. The scalar helper is_close is re-exported at crate root.

use numkong::{is_close, Vector, Tensor};

// Scalar check
assert!(is_close(1.0, 1.0 + 1e-8, 1e-6, 0.0));

// Vector tolerance check
let a = Vector::<f32>::full(3, 1.0).unwrap();
let b = Vector::<f32>::full(3, 1.0 + 1e-7).unwrap();
assert!(a.allclose(&b, 1e-6, 0.0));

// Tensor tolerance check
let ta = Tensor::<f32>::full(&[2, 3], 1.0).unwrap();
let tb = Tensor::<f32>::full(&[2, 3], 1.0 + 1e-7).unwrap();
assert!(ta.allclose(&tb, 1e-6, 0.0));

API

TraitAllCloseOpsExtension trait: tolerance-based equality for any TensorRef implementor.1
each.rs:4154 ↗
pub trait AllCloseOps<Scalar: FloatConvertible, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>
where
    Scalar::DimScalar: NumberLike,

Uses the formula |a − b| ≤ atol + rtol × |b| per element. Returns false if shapes differ.

Implementors

allcloseReturns true iff both tensors share a shape and every element pair is within tolerance.
each.rs:4159 ↗
fn allclose(&self, other: &impl TensorRef<Scalar, MAX_RANK> + ?Sized, atol: f64, rtol: f64) -> bool

Functions

is_closeCheck whether two values are within tolerance: |a − b| ≤ atol + rtol × |b|.
types.rs:99 ↗
pub fn is_close(a: f64, b: f64, atol: f64, rtol: f64) -> bool