{"id":"3a7d926c1ef9b7b5","repo":"gohugoio/hugo","slug":"errmustonenumbererror","errorCode":"errMustOneNumberError","errorMessage":"must provide at least one number","messagePattern":"must provide at least one number","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/math/math.go","lineNumber":31,"sourceCode":"\n// Package math provides template functions for mathematical operations.\npackage math\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"math\"\n\t\"math/rand\"\n\t\"reflect\"\n\n\t_math \"github.com/gohugoio/hugo/common/math\"\n\t\"github.com/gohugoio/hugo/deps\"\n\t\"github.com/spf13/cast\"\n)\n\nvar (\n\terrMustTwoNumbersError = errors.New(\"must provide at least two numbers\")\n\terrMustOneNumberError  = errors.New(\"must provide at least one number\")\n)\n\n// New returns a new instance of the math-namespaced template functions.\nfunc New(d *deps.Deps) *Namespace {\n\treturn &Namespace{\n\t\td: d,\n\t}\n}\n\n// Namespace provides template functions for the \"math\" namespace.\ntype Namespace struct {\n\td *deps.Deps\n}\n\n// Abs returns the absolute value of n.\nfunc (ns *Namespace) Abs(n any) (float64, error) {\n\taf, err := cast.ToFloat64E(n)\n\tif err != nil {","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/math/math.go#L13-L49","documentation":"errMustOneNumberError is the companion sentinel for variadic math functions that require at least one operand, such as math.Sum and math.Product. If the flattened argument list is empty, there is nothing to aggregate and Hugo returns this error rather than a fabricated identity value.","triggerScenarios":"`{{ math.Sum slice }}` or `{{ math.Product }}` — calling an aggregate math function with no arguments, or with only empty slices that flatten to zero numbers.","commonSituations":"Summing a collected slice that is empty because a `where` filter matched nothing; iterating pages with no numeric param set and building an empty argument list; templates that run on sections with zero children.","solutions":["Guard for emptiness first: `{{ if $nums }}{{ math.Sum $nums }}{{ else }}0{{ end }}`.","Seed the call with an identity value: `{{ math.Sum 0 $nums }}` (or `1` for Product).","Check the upstream filter (`where`, `.Pages`) actually yields elements in all render contexts."],"exampleFix":"<!-- before -->\n{{ math.Sum $weights }}\n<!-- after -->\n{{ math.Sum 0 $weights }}","handlingStrategy":"validation","validationCode":"{{ $nums := .Params.scores | default slice }}\n{{ if gt (len $nums) 0 }}{{ math.Max $nums }}{{ end }}","typeGuard":null,"tryCatchPattern":"{{ with try (math.Min $nums) }}{{ if .Err }}{{ warnf \"no numbers: %s\" .Err }}{{ else }}{{ .Value }}{{ end }}{{ end }}","preventionTips":["When passing a slice to math.Max/math.Min/math.Sum/math.Product, verify it is non-empty first","Empty front-matter arrays and filtered-out results are the usual cause — check after `where`/`first` filtering","Decide a meaningful empty-case value in the template rather than relying on the function"],"tags":["hugo","templates","math","arguments","empty-collection"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}