{"id":"076f86e97021f4df","repo":"gohugoio/hugo","slug":"errmusttwonumberserror","errorCode":"errMustTwoNumbersError","errorMessage":"must provide at least two numbers","messagePattern":"must provide at least two numbers","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/math/math.go","lineNumber":30,"sourceCode":"// limitations under the License.\n\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)","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/math/math.go#L12-L48","documentation":"errMustTwoNumbersError is a sentinel in tpl/math/math.go returned by variadic math functions that need at least two operands to be meaningful (e.g. math.Max/math.Min via applyOpToScalarsOrSlices). Calling with zero or one scalar value gives nothing to compare, so Hugo fails fast instead of returning an arbitrary result.","triggerScenarios":"`{{ math.Max 5 }}` or `{{ math.Min }}` — calling a two-plus-operand math function with fewer than two numbers after flattening any slice arguments (a single one-element slice also triggers it).","commonSituations":"Piping a single value into max/min expecting a no-op (`{{ .Weight | math.Max }}`); passing a slice that turned out to hold only one element (or an empty slice plus one scalar); loops that build argument lists dynamically and end up too short.","solutions":["Pass at least two numbers: `{{ math.Max .Weight 10 }}`.","When comparing against a slice, ensure the flattened total is ≥2, or add a fixed bound: `{{ math.Max $values 0 }}`.","Guard dynamic inputs with a length check before calling."],"exampleFix":"<!-- before -->\n{{ math.Max .Params.weight }}\n<!-- after -->\n{{ math.Max (.Params.weight | default 0) 1 }}","handlingStrategy":"validation","validationCode":"{{ $nums := .Params.values }}\n{{ if ge (len $nums) 2 }}{{ math.Max (index $nums 0) (index $nums 1) }}{{ end }}","typeGuard":null,"tryCatchPattern":"{{ with try (math.Sub $a $b) }}{{ if .Err }}{{ warnf \"math op failed: %s\" .Err }}{{ else }}{{ .Value }}{{ end }}{{ end }}","preventionTips":["Binary math functions (add, sub, mul, div, max, min...) need at least two arguments — check `len` when spreading a slice into them","Never spread a user-supplied slice into a math call without a length check","Supply literal defaults when a second operand may be missing"],"tags":["hugo","templates","math","arguments","validation"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}