tolerance

package
v0.9.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

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

func AssertAllApproxEqual[T float32 | float64](a, b []T, tol *Tolerance[T]) error

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

func AssertApproxEqual[T float32 | float64](a, b T, tol *Tolerance[T]) error

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 TolType

type TolType int

TolType specifies the kind of tolerance check to apply.

const (
	// RelAbs uses both absolute and relative tolerance, passing if either check passes (default).
	RelAbs TolType = iota
	// Rel uses only relative tolerance.
	Rel
	// Abs uses only absolute tolerance.
	Abs
)

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

func NewDefaultTolerance[T float32 | float64]() *Tolerance[T]

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL