{"id":"c2d46987f9afe765","repo":"gohugoio/hugo","slug":"can-t-divide-the-value-by-0","errorCode":null,"errorMessage":"can't divide the value by 0","messagePattern":"can't divide the value by 0","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/math/math.go","lineNumber":129,"sourceCode":"\t\t\treturn af - bf, nil\n\t\t}\n\t\treturn au - bu, nil\n\tcase '*':\n\t\tif isInt {\n\t\t\treturn ai * bi, nil\n\t\t} else if isFloat {\n\t\t\treturn af * bf, nil\n\t\t}\n\t\treturn au * bu, nil\n\tcase '/':\n\t\tif isInt && bi != 0 {\n\t\t\treturn ai / bi, nil\n\t\t} else if isFloat && bf != 0 {\n\t\t\treturn af / bf, nil\n\t\t} else if isUint && bu != 0 {\n\t\t\treturn au / bu, nil\n\t\t}\n\t\treturn nil, errors.New(\"can't divide the value by 0\")\n\tdefault:\n\t\treturn nil, errors.New(\"there is no such an operation\")\n\t}\n}\n","sourceCodeStart":111,"sourceCodeEnd":134,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/common/math/math.go#L111-L134","documentation":"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.","triggerScenarios":"`{{ 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.","commonSituations":"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.","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."],"exampleFix":"{{/* before */}}\n{{ div $sum (len $pages) }}\n\n{{/* after */}}\n{{ with len $pages }}{{ div $sum . }}{{ else }}0{{ end }}","handlingStrategy":"validation","validationCode":"if divisor == 0 { // handle explicitly: skip, error, or use a guarded expression\n}\n// template: {{ if ne $n 0 }}{{ div $x $n }}{{ end }}","typeGuard":null,"tryCatchPattern":"v, err := math.DoArithmetic(x, n, '/')\nif err != nil {\n\t// zero divisor: surface the error; don't silently substitute 0\n}","preventionTips":["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."],"tags":["hugo","templates","math","division-by-zero"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}