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
Hugo's `math.Sqrt` casts its argument to float64 with `cast.ToFloat64E` before calling Go's `math.Sqrt`. If the value isn't an integer, float, or numeric string, the cast fails and Hugo returns this error. Note that negative inputs are NOT caught here — they return NaN, per Go semantics — only non-numeric types trigger this error.
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 8a468df065)
Solutions
- Verify the argument resolves to a number; cast with `{{ math.Sqrt (float $v) }}` for string-typed numeric values.
- Guard nil params: `{{ with .Params.area }}{{ math.Sqrt . }}{{ end }}`.
- If you later see NaN in output, also check the value isn't negative — that case passes this check silently.
Example fix
<!-- before -->
{{ math.Sqrt .Params.area }}
<!-- after -->
{{ with .Params.area }}{{ math.Sqrt (float .) }}{{ end }} Defensive patterns
Strategy: validation
Validate before calling
{{ $x := float $v }}{{ if ge $x 0 }}{{ math.Sqrt $x }}{{ end }} Type guard
func sqrtSafe(v any) (float64, error) {
f, err := cast.ToFloat64E(v)
if err != nil || f < 0 {
return 0, fmt.Errorf("invalid sqrt input %v", v)
}
return math.Sqrt(f), nil
} Try / catch
if _, err := ns.Sqrt(x); err != nil {
// non-numeric input — fix the data source
} Prevention
- Convert with `float` (or `int`) before `math.Sqrt`
- Also check for negative inputs — conversion succeeds but the result is NaN
- Validate numeric fields in data files with a schema check before the build
When it happens
Trigger: `{{ math.Sqrt X }}` with a non-numeric X: `{{ math.Sqrt "sixteen" }}`, `{{ math.Sqrt .Params.area }}` when the param is absent (nil), or passing a slice/map/dict by mistake.
Common situations: Values from data files or front matter quoted as strings with non-numeric content; nil from missing params; accidentally passing a whole object (e.g. `.Params` instead of `.Params.area`) due to a typo.
Related errors
- modulo operator can't be used with non integer value
- Pow operator can't be used with non-float value
- Round operator can't be used with non-float value
- the math.Abs function requires a numeric argument
- Ceil operator can't be used with non-float value
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/4cd8d3b4c28d4f3d.json.
Report an issue: GitHub ↗.