gohugoio/hugo · error

the math.Abs function requires a numeric argument

Error message

the math.Abs function requires a numeric argument

What it means

Thrown by math.Abs (tpl/math/math.go:50) when its single argument cannot be converted to a float64 via cast.ToFloat64E. math.Abs needs a numeric input; passing a non-numeric value (string that is not a number, bool, nil, struct, slice) makes the conversion fail. The message is Abs-specific, unlike the generic math functions.

Solutions

  1. Ensure the argument is a number (int or float) before calling math.Abs.
  2. Provide a numeric default: {{ math.Abs (.Param "score" | default 0) }}.
  3. Cast with a check, or guard with {{ if where }} style conditionals on the value type.
  4. Fix the source data so the front-matter field is numeric, not quoted.

Example fix

// before
{{ $abs := math.Abs .Params.score }}

// after
{{ $score := .Params.score | default 0 }}
{{ $abs := math.Abs $score }}
Defensive patterns

Strategy: type-guard

Validate before calling

// Ensure a numeric value before math.Abs:
{{ $v := .Param "score" | default 0 }}
{{ math.Abs $v }}

Type guard

// Go caller:
func isNumeric(v any) bool {
    switch reflect.ValueOf(v).Kind() {
    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
        reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
        reflect.Float32, reflect.Float64:
        return true
    }
    // cast also accepts numeric strings, so check that too:
    if s, ok := v.(string); ok {
        _, err := strconv.ParseFloat(s, 64)
        return err == nil
    }
    return false
}

Prevention

When it happens

Trigger: Calling {{ math.Abs .Param "score" }} where the param is a string like "N/A"; passing nil from an unset variable; passing a bool; passing a slice or struct.

Common situations: Front matter value is a quoted string that isn't numeric; a param is optional and unset (nil); type drift after a content migration.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/8a28528130c055f0. Report an issue: GitHub.

Appendix: 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 52c9bd7908)