gohugoio/hugo · error

Sqrt operator can't be used with non integer or float value

Error message

Sqrt operator can't be used with non integer or float value

What it means

Returned by math.Sqrt when its argument cannot be converted to float64. Despite the message mentioning integer or float, the implementation uses cast.ToFloat64E which accepts ints, floats, and numeric strings; non-numeric values are rejected. Note: passing a negative number will not error here but math.Sqrt returns NaN.

Solutions

  1. Cast the input: {{ math.Sqrt (float $x) }}.
  2. Default nil values: {{ math.Sqrt ($x | default 0) }}.
  3. Ensure front matter uses numeric types.

Example fix

// before
{{ math.Sqrt .Params.value }}
// after
{{ math.Sqrt (float .Params.value) }}
Defensive patterns

Strategy: validation

Validate before calling

{{ math.Sqrt (float $x) }}

Prevention

When it happens

Trigger: Calling {{ math.Sqrt "foo" }}, {{ math.Sqrt $nil }}, or {{ math.Sqrt $slice }}. Passing a non-numeric .Params value is the most common cause.

Common situations: Statistical/layout computations in templates; params typed as strings; accidentally passing a resource or page object instead of a measurement.

Related errors


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

Appendix: source

Thrown at tpl/math/math.go:244

	}

	return _round(xf), nil
}

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

// Sqrt returns the square root of the number n.
func (ns *Namespace) Sqrt(n any) (float64, error) {
	af, err := cast.ToFloat64E(n)
	if err != nil {
		return 0, errors.New("Sqrt operator can't be used with non integer or float value")
	}

	return math.Sqrt(af), nil
}

// Sub subtracts multivalued.
func (ns *Namespace) Sub(inputs ...any) (any, error) {
	return ns.doArithmetic(inputs, '-')
}

// Sum returns the sum of all numbers in inputs. Any slices in inputs are flattened.
func (ns *Namespace) Sum(inputs ...any) (sum float64, err error) {
	fn := func(x, y float64) float64 {
		return x + y
	}
	return ns.applyOpToScalarsOrSlices("Sum", fn, inputs...)
}

View on GitHub (pinned to 52c9bd7908)