latmath.scalar — Scalar Utilities
Mathematical constants, error measures, tolerance-based comparison, and approximate equality for scalar values. Pure stdlib, zero dependencies.
Constants
from latpy.latmath.scalar import pi, e, tau, inf, nan
Symbol |
Value |
Description |
|---|---|---|
|
|
Archimedes’ constant (π) |
|
|
Euler’s number |
|
|
τ = 2π |
|
|
IEEE 754 positive infinity |
|
|
IEEE 754 Not-a-Number |
All constants are Python float values, safe for use in any expression.
Error Measures
from latpy.latmath.scalar import relative_error, absolute_error
Signature |
Description |
|---|---|
|
|a − b| / |b|; returns 0.0 if both are 0, inf if b = 0 and a ≠ 0 |
|
|a − b| |
Edge cases:
relative_error(0.0, 0.0)→0.0(not undefined)relative_error(5.0, 0.0)→infrelative_error(0.0, 3.0)→0.0
Approximate Equality
from latpy.latmath.scalar import isclose, allclose
Signature |
Description |
|---|---|
|
Delegates to |
|
Pairwise |
Edge cases:
isclose(nan, nan)→False(IEEE 754 semantics)isclose(inf, inf)→Trueallclosestops early on first mismatch
Robust Comparison
from latpy.latmath.scalar import (
approx_eq, approx_ne,
approx_lt, approx_le,
approx_gt, approx_ge,
)
Signature |
Description |
|---|---|
|
True if a ≈ b within tolerance |
|
True if a ≉ b |
|
True if a < b and a ≉ b |
|
True if a < b or a ≈ b |
|
True if a > b and a ≉ b |
|
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”)