gohugoio/hugo · error

the math.Abs function requires a numeric argument

Error message

the math.Abs function requires a numeric argument

What it means

math.Abs in Hugo casts its argument to float64 with cast.ToFloat64E; if the cast fails (the value isn't a number or a numeric string), this error is returned. It signals a type problem in the template input, not a math problem.

Source

Thrown at tpl/math/math.go:50

)

// 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)
	if err != nil {
		return 0, errors.New("the math.Abs function requires a numeric argument")
	}

	return math.Abs(af), nil
}

// Acos returns the arccosine, in radians, of n.
func (ns *Namespace) Acos(n any) (float64, error) {
	af, err := cast.ToFloat64E(n)
	if err != nil {
		return 0, errors.New("requires a numeric argument")
	}
	return math.Acos(af), nil
}

// Add adds the multivalued addends n1 and n2 or more values.
func (ns *Namespace) Add(inputs ...any) (any, error) {
	return ns.doArithmetic(inputs, '+')
}

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Pass a numeric value or numeric string: `{{ math.Abs -3.5 }}`.
  2. Sanitize/convert first: `{{ math.Abs (float .Params.offset) }}` after stripping units, or use `default 0` for missing params.
  3. Fix the front matter so the value is a plain number, not a decorated string.

Example fix

<!-- before -->
{{ math.Abs .Params.offset }}  {{/* offset: "-3px" */}}
<!-- after -->
{{ math.Abs (float (strings.TrimSuffix "px" .Params.offset)) }}
Defensive patterns

Strategy: type-guard

Validate before calling

{{ $v := .Params.delta }}
{{ if reflect.IsSlice $v }}{{ warnf "expected number" }}{{ else }}{{ math.Abs (float $v) }}{{ end }}

Type guard

{{ with try (float $v) }}{{ if not .Err }}{{ math.Abs .Value }}{{ end }}{{ end }}

Try / catch

{{ with try (math.Abs .Params.delta) }}{{ if .Err }}{{ warnf "math.Abs: %s" .Err }}{{ else }}{{ .Value }}{{ end }}{{ end }}

Prevention

When it happens

Trigger: `{{ math.Abs "abc" }}`, `{{ math.Abs .Params.offset }}` where the param is a non-numeric string, a map, a slice, or nil — anything spf13/cast cannot convert to float64.

Common situations: Front-matter values quoted as non-numeric strings (e.g. "−3" with a unicode minus, "3px"); passing a whole object instead of its numeric field; nil params on pages missing the key.

Related errors


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