TheAlgorithms/Go · error

interval boundaries should be finite numbers

Error message

interval boundaries should be finite numbers

What it means

TernaryMax performs ternary search for the maximum of a unimodal function over [a, b], which requires finite bounds. The library rejects infinite boundaries (a == -Inf or b == +Inf) because the recursive interval-splitting would never converge. It is thrown as a sentinel-style validation error at the top of the function.

Source

Thrown at search/ternary.go:12

package search

import (
	"fmt"
	"math"
)

// TernaryMax is a function to search for maximum value of a uni-modal function `f`
// in the interval [a, b]. a and b should be finit numbers
func TernaryMax(a, b, epsilon float64, f func(x float64) float64) (float64, error) {
	if a == math.Inf(-1) || b == math.Inf(1) {
		return -1, fmt.Errorf("interval boundaries should be finite numbers")
	}
	if math.Abs(a-b) <= epsilon {
		return f((a + b) / 2), nil
	}
	left := (2*a + b) / 3
	right := (a + 2*b) / 3
	if f(left) < f(right) {
		return TernaryMax(left, b, epsilon, f)
	}
	return TernaryMax(a, right, epsilon, f)
}

// TernaryMin is a function to search for minimum value of a uni-modal function `f`
// in the interval [a, b]. a and b should be finit numbers.
func TernaryMin(a, b, epsilon float64, f func(x float64) float64) (float64, error) {
	if a == math.Inf(-1) || b == math.Inf(1) {
		return -1, fmt.Errorf("interval boundaries should be finite numbers")
	}

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Pass finite numeric bounds a and b that actually bracket the unimodal region (e.g. -1e9/1e9 instead of ±Inf).
  2. If the domain is unbounded, first run an exponential/search expansion phase to find finite brackets, then call TernaryMax.
  3. Validate inputs with math.IsInf(a, ...) / math.IsInf(b, ...) before calling and return a clearer domain-specific error.

Example fix

// before
max, err := search.TernaryMax(math.Inf(-1), 100, 1e-6, f)
// after
max, err := search.TernaryMax(-1000, 100, 1e-6, f)
Defensive patterns

Strategy: validation

Validate before calling

if math.IsInf(a, -1) || math.IsInf(b, 1) {
    return errors.New("ternary search requires finite interval bounds")
}
max, err := search.TernaryMax(a, b, eps, f)

Type guard

func finiteBounds(a, b float64) bool {
    return !math.IsInf(a, -1) && !math.IsInf(b, 1) && !math.IsNaN(a) && !math.IsNaN(b)
}

Try / catch

max, err := search.TernaryMax(a, b, eps, f)
if err != nil && strings.Contains(err.Error(), "finite") {
    // fall back to finite bracketing, then retry
}

Prevention

When it happens

Trigger: Calling TernaryMax(math.Inf(-1), b, eps, f) or TernaryMax(a, math.Inf(1), eps, f) — i.e. passing an unbounded interval such as searching over the entire real line or using -Inf/+Inf placeholders for 'no limit'.

Common situations: Developers modeling 'search from negative infinity' for an unbounded optimization, mistakenly defaulting unset config values to math.Inf, or porting code from pseudo-code that assumes infinite bounds are supported.

Related errors


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/0043ba5fbbfdd40b. Report an issue: GitHub.