# Platform Notes

> NumKong for Rust

### Tolerance Comparison

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

$$
|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.

```rust
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));
```

`use numkong::AllCloseOps;`

`use numkong::*;`

## AllCloseOps

Extension trait: tolerance-based equality for any [`TensorRef`](https://www.unum.cloud/docs/numkong/rust/tensors-and-views.md#TensorRef) implementor.

```rust
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: - `impl<C, Scalar: [FloatConvertible](https://www.unum.cloud/docs/numkong/rust/scalar-types.md#FloatConvertible), const R: usize> [AllCloseOps](#AllCloseOps)<Scalar, R> for C where C: [TensorRef](https://www.unum.cloud/docs/numkong/rust/tensors-and-views.md#TensorRef)<Scalar, R>, Scalar::DimScalar: [NumberLike](https://www.unum.cloud/docs/numkong/rust/scalar-types.md#NumberLike)`

## allclose

Returns `true` iff both tensors share a shape and every element pair is within tolerance.

```rust
fn allclose(&self, other: &impl TensorRef<Scalar, MAX_RANK> + ?Sized, atol: f64, rtol: f64) -> bool
```

## is_close

Check whether two values are within tolerance: |a − b| ≤ atol + rtol × |b|.

```rust
pub fn is_close(a: f64, b: f64, atol: f64, rtol: f64) -> bool
```

