{"record":{"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":"exception","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/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/common/math/math.go#L111-L134","documentation":"In the division (/) case, if the divisor is zero for int, float, or uint, Hugo refuses rather than producing a divide-by-zero panic or NaN. The check is exact-zero (bf != 0 / bu != 0 / bi != 0).","triggerScenarios":"{{ div $total 0 }}, or a divisor expression that evaluates to 0 such as the length of an empty list or a zeroed front-matter value.","commonSituations":"Computing averages or ratios where the denominator is an empty collection's count; pagination math with 0 items-per-page; missing divisor param defaulting to 0.","solutions":["Guard the divisor with a non-zero check (if gt $denom 0) before dividing.","Provide a sensible non-zero default for divisor params.","Validate collection lengths before computing ratios."],"exampleFix":"// before\n{{ div $sum $count }}\n// after\n{{ if gt $count 0 }}{{ div $sum $count }}{{ else }}0{{ end }}","handlingStrategy":"validation","validationCode":"// Guard divisor before dividing\nswitch dv := reflect.ValueOf(b); dv.Kind() {\ncase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n    if dv.Int() == 0 { return 0, errors.New(\"divisor is zero\") }\ncase reflect.Float32, reflect.Float64:\n    if dv.Float() == 0 { return 0, errors.New(\"divisor is zero\") }\ncase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:\n    if dv.Uint() == 0 { return 0, errors.New(\"divisor is zero\") }\n}\nmath.DoArithmetic(a, b, '/')","typeGuard":"func isNonZeroNumber(v any) bool {\n    dv := reflect.ValueOf(v)\n    switch dv.Kind() {\n    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n        return dv.Int() != 0\n    case reflect.Float32, reflect.Float64:\n        return dv.Float() != 0\n    case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:\n        return dv.Uint() != 0\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Wrap divisions in a non-zero check on the denominator.","Provide non-zero defaults for divisor params.","Validate list lengths before computing ratios/averages."],"tags":["math","go","template","hugo","division"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}