Documentation
¶
Overview ¶
Package tolerance provides helpers for approximate floating-point equality using absolute, relative, or combined tolerance checks.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertAllApproxEqual ¶
AssertAllApproxEqual checks a and b element-wise for approximate equality.
Returns an error describing the first element that fails, or a length mismatch error if the slices differ in length.
func AssertApproxEqual ¶
AssertApproxEqual checks whether a and b are approximately equal according to the given tolerance. Passes if the values are exactly equal, both +/-Inf, or both NaN. Returns an error if tol.Rel > 1.0 or tol.Abs < 0.0. Follows Burn's Tolerance.approx_eq implementation.
Absolute tolerance: passes when |a-b| < tol.Abs. Relative tolerance: passes when |a-b| < tol.Rel * max(|a|, |b|). Combined (RelAbs): passes when |a-b| < max(tol.Rel * max(|a|, |b|), tol.Abs).
Returns nil if the values are approximately equal, or an error describing which tolerance check failed.
Types ¶
type Tolerance ¶
type Tolerance[T float32 | float64] struct { TolType TolType Abs T // absolute tolerance Rel T // relative tolerance factor }
Tolerance holds parameters for approximate floating-point equality checks.
Rel is constrained to < 1.0 in validation checks. Abs is constrained to >= 0.0 in validation checks.
func NewDefaultTolerance ¶
NewDefaultTolerance returns a Tolerance with sensible defaults.
The tolerance type is set to RelAbs with the following values:
- float32: rel = 1e-2, abs = 1e-5
- float64: rel = 1e-4, abs = 1e-5