gohugoio/hugo · error
can't divide the value by 0
Error message
can't divide the value by 0
What it means
In DoArithmetic's '/' branch, the division is only performed when the denominator is non-zero for the resolved type class (int, float, or uint). A zero denominator falls through to this error at common/math/math.go:129, so Hugo's `div` template function reports division by zero instead of panicking.
Source
Thrown at common/math/math.go:129
return af - bf, nil
}
return au - bu, nil
case '*':
if isInt {
return ai * bi, nil
} else if isFloat {
return af * bf, nil
}
return au * bu, nil
case '/':
if isInt && bi != 0 {
return ai / bi, nil
} else if isFloat && bf != 0 {
return af / bf, nil
} else if isUint && bu != 0 {
return au / bu, nil
}
return nil, errors.New("can't divide the value by 0")
default:
return nil, errors.New("there is no such an operation")
}
}
View on GitHub ↗ (pinned to 8a468df065)
Solutions
- Guard the division: `{{ if gt $n 0 }}{{ div $total $n }}{{ end }}`.
- Use `default` to substitute a safe non-zero divisor when the value may be unset.
- Trace why the divisor is zero (empty section, missing param) and fix the data or the collection filter.
Example fix
{{/* before */}}
{{ div $sum (len $pages) }}
{{/* after */}}
{{ with len $pages }}{{ div $sum . }}{{ else }}0{{ end }} Defensive patterns
Strategy: validation
Validate before calling
if divisor == 0 { // handle explicitly: skip, error, or use a guarded expression
}
// template: {{ if ne $n 0 }}{{ div $x $n }}{{ end }} Try / catch
v, err := math.DoArithmetic(x, n, '/')
if err != nil {
// zero divisor: surface the error; don't silently substitute 0
} Prevention
- Guard every division whose divisor comes from data (page counts, lengths) with a zero check.
- Use `len` checks before computing averages over possibly-empty collections.
- Note integer division by zero errors while float division may not — normalize types deliberately.
When it happens
Trigger: `{{ div x y }}` (or `math.Div`) where y evaluates to 0 or 0.0 — including a missing/unset param defaulting to 0, `len` of an empty collection, or a computed counter that is zero.
Common situations: Computing percentages or averages where the divisor is `len` of a possibly-empty page collection or slice; pagination/ratio math on sites with no matching content; params that default to 0 when absent.
Related errors
- errMustTwoNumbersError
- errMustOneNumberError
- the math.Abs function requires a numeric argument
- Ceil operator can't be used with non-float value
- Floor operator can't be used with non-float value
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/c2d46987f9afe765.json.
Report an issue: GitHub ↗.