gohugoio/hugo · error

errMustTwoNumbersError

errMustTwoNumbersError

Error message

must provide at least two numbers

What it means

errMustTwoNumbersError is a sentinel in tpl/math/math.go returned by variadic math functions that need at least two operands to be meaningful (e.g. math.Max/math.Min via applyOpToScalarsOrSlices). Calling with zero or one scalar value gives nothing to compare, so Hugo fails fast instead of returning an arbitrary result.

Source

Thrown at tpl/math/math.go:30

// limitations under the License.

// Package math provides template functions for mathematical operations.
package math

import (
	"errors"
	"fmt"
	"math"
	"math/rand"
	"reflect"

	_math "github.com/gohugoio/hugo/common/math"
	"github.com/gohugoio/hugo/deps"
	"github.com/spf13/cast"
)

var (
	errMustTwoNumbersError = errors.New("must provide at least two numbers")
	errMustOneNumberError  = errors.New("must provide at least one number")
)

// New returns a new instance of the math-namespaced template functions.
func New(d *deps.Deps) *Namespace {
	return &Namespace{
		d: d,
	}
}

// Namespace provides template functions for the "math" namespace.
type Namespace struct {
	d *deps.Deps
}

// Abs returns the absolute value of n.
func (ns *Namespace) Abs(n any) (float64, error) {
	af, err := cast.ToFloat64E(n)

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Pass at least two numbers: `{{ math.Max .Weight 10 }}`.
  2. When comparing against a slice, ensure the flattened total is ≥2, or add a fixed bound: `{{ math.Max $values 0 }}`.
  3. Guard dynamic inputs with a length check before calling.

Example fix

<!-- before -->
{{ math.Max .Params.weight }}
<!-- after -->
{{ math.Max (.Params.weight | default 0) 1 }}
Defensive patterns

Strategy: validation

Validate before calling

{{ $nums := .Params.values }}
{{ if ge (len $nums) 2 }}{{ math.Max (index $nums 0) (index $nums 1) }}{{ end }}

Try / catch

{{ with try (math.Sub $a $b) }}{{ if .Err }}{{ warnf "math op failed: %s" .Err }}{{ else }}{{ .Value }}{{ end }}{{ end }}

Prevention

When it happens

Trigger: `{{ math.Max 5 }}` or `{{ math.Min }}` — calling a two-plus-operand math function with fewer than two numbers after flattening any slice arguments (a single one-element slice also triggers it).

Common situations: Piping a single value into max/min expecting a no-op (`{{ .Weight | math.Max }}`); passing a slice that turned out to hold only one element (or an empty slice plus one scalar); loops that build argument lists dynamically and end up too short.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/076f86e97021f4df.json. Report an issue: GitHub ↗.