{"record":{"id":"a38706a58b923e7e","repo":"gohugoio/hugo","slug":"the-number-can-t-be-divided-by-zero-at-modulo-oper","errorCode":null,"errorMessage":"the number can't be divided by zero at modulo operation","messagePattern":"the number can't be divided by zero at modulo operation","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/math/math.go","lineNumber":170,"sourceCode":"\treturn math.MaxInt64\n}\n\n// Min returns the smaller of all numbers in inputs. Any slices in inputs are flattened.\nfunc (ns *Namespace) Min(inputs ...any) (minimum float64, err error) {\n\treturn ns.applyOpToScalarsOrSlices(\"Min\", math.Min, inputs...)\n}\n\n// Mod returns n1 % n2.\nfunc (ns *Namespace) Mod(n1, n2 any) (int64, error) {\n\tai, erra := cast.ToInt64E(n1)\n\tbi, errb := cast.ToInt64E(n2)\n\n\tif erra != nil || errb != nil {\n\t\treturn 0, errors.New(\"modulo operator can't be used with non integer value\")\n\t}\n\n\tif bi == 0 {\n\t\treturn 0, errors.New(\"the number can't be divided by zero at modulo operation\")\n\t}\n\n\treturn ai % bi, nil\n}\n\n// ModBool returns the boolean of n1 % n2.  If n1 % n2 == 0, return true.\nfunc (ns *Namespace) ModBool(n1, n2 any) (bool, error) {\n\tres, err := ns.Mod(n1, n2)\n\tif err != nil {\n\t\treturn false, err\n\t}\n\n\treturn res == int64(0), nil\n}\n\n// Mul multiplies the multivalued numbers n1 and n2 or more values.\nfunc (ns *Namespace) Mul(inputs ...any) (any, error) {\n\treturn ns.doArithmetic(inputs, '*')","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/math/math.go#L152-L188","documentation":"Thrown by math.Mod (tpl/math/math.go:170) when the second operand (n2, the divisor) is zero. Modulo by zero is mathematically undefined and would panic in raw Go, so Hugo returns this error instead. The check runs after both operands are confirmed to be integers.","triggerScenarios":"Calling {{ math.Mod .N 0 }}, or {{ math.Mod .N .M }} where .M resolves to 0; looping over a collection where the divisor field is 0 for some entries.","commonSituations":"Divisor comes from a count that is zero for empty categories; hardcoded cycle length of 0 by mistake; a computed divisor (e.g. len of an empty slice) that evaluates to 0.","solutions":["Guard the divisor: use {{ if gt $divisor 0 }}{{ math.Mod $n $divisor }}{{ end }}.","Provide a safe non-zero default for the divisor.","When using len-based divisors, handle the empty case separately before calling Mod.","Validate that the divisor field is > 0 in source data."],"exampleFix":"// before\n{{ $col := math.Mod $i (.Items | len) }}\n\n// after\n{{ $len := .Items | len }}\n{{ if gt $len 0 }}\n  {{ $col := math.Mod $i $len }}\n{{ end }}","handlingStrategy":"validation","validationCode":"// Guard the divisor before math.Mod:\n{{ $div := .Items | len }}\n{{ if gt $div 0 }}{{ math.Mod $i $div }}{{ end }}","typeGuard":"// Go caller:\nfunc nonZeroInt(v any) bool {\n    i, err := cast.ToInt64E(v)\n    return err == nil && i != 0\n}","tryCatchPattern":null,"preventionTips":["Always check the divisor is > 0 before math.Mod.","Handle empty collections (len 0) before using their length as a divisor.","Provide a safe non-zero default for the divisor."],"tags":["hugo","template","math","modulo","divide-by-zero"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}