# `latmath.scalar` — Scalar Utilities Mathematical constants, error measures, tolerance-based comparison, and approximate equality for scalar values. Pure stdlib, zero dependencies. --- ## Constants ```python from latpy.latmath.scalar import pi, e, tau, inf, nan ``` | Symbol | Value | Description | |--------|-------|-------------| | `pi` | `3.141592653589793` | Archimedes' constant (π) | | `e` | `2.718281828459045` | Euler's number | | `tau` | `6.283185307179586` | τ = 2π | | `inf` | `float('inf')` | IEEE 754 positive infinity | | `nan` | `float('nan')` | IEEE 754 Not-a-Number | All constants are Python `float` values, safe for use in any expression. --- ## Error Measures ```python from latpy.latmath.scalar import relative_error, absolute_error ``` | Signature | Description | |-----------|-------------| | `relative_error(a, b) -> float` | \|a − b\| / \|b\|; returns 0.0 if both are 0, inf if b = 0 and a ≠ 0 | | `absolute_error(a, b) -> float` | \|a − b\| | **Edge cases:** - `relative_error(0.0, 0.0)` → `0.0` (not undefined) - `relative_error(5.0, 0.0)` → `inf` - `relative_error(0.0, 3.0)` → `0.0` --- ## Approximate Equality ```python from latpy.latmath.scalar import isclose, allclose ``` | Signature | Description | |-----------|-------------| | `isclose(a, b, rel_tol=1e-9, abs_tol=0.0) -> bool` | Delegates to `math.isclose` | | `allclose(a, b, rel_tol=1e-9, abs_tol=0.0) -> bool` | Pairwise `isclose` over iterables | **Edge cases:** - `isclose(nan, nan)` → `False` (IEEE 754 semantics) - `isclose(inf, inf)` → `True` - `allclose` stops early on first mismatch --- ## Robust Comparison ```python from latpy.latmath.scalar import ( approx_eq, approx_ne, approx_lt, approx_le, approx_gt, approx_ge, ) ``` | Signature | Description | |-----------|-------------| | `approx_eq(a, b, rel_tol, abs_tol) -> bool` | True if a ≈ b within tolerance | | `approx_ne(a, b, rel_tol, abs_tol) -> bool` | True if a ≉ b | | `approx_lt(a, b, rel_tol, abs_tol) -> bool` | True if a < b and a ≉ b | | `approx_le(a, b, rel_tol, abs_tol) -> bool` | True if a < b or a ≈ b | | `approx_gt(a, b, rel_tol, abs_tol) -> bool` | True if a > b and a ≉ b | | `approx_ge(a, b, rel_tol, abs_tol) -> bool` | True if a > b or a ≈ b | **Rationale:** Standard `<` / `>` break down when values differ by floating-point rounding. `approx_lt(1.0, 1.0 + 1e-16)` is `False` because the two are considered equal within tolerance — avoiding spurious branch misbehavior in numerical algorithms. **Edge cases:** - `approx_eq(1.0, 1.0 + 1e-10)` → `True` (default rel_tol=1e-9) - `approx_lt(1.0, 1.0 + 1e-10)` → `False` (within tolerance, so not strictly less) - `approx_ge(1.0 + 1e-10, 1.0)` → `True` (within tolerance, so "greater or equal")