{"id":"8a28528130c055f0","repo":"gohugoio/hugo","slug":"the-math-abs-function-requires-a-numeric-argument","errorCode":null,"errorMessage":"the math.Abs function requires a numeric argument","messagePattern":"the math\\.Abs function requires a numeric argument","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/math/math.go","lineNumber":50,"sourceCode":")\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 {\n\t\treturn 0, errors.New(\"the math.Abs function requires a numeric argument\")\n\t}\n\n\treturn math.Abs(af), nil\n}\n\n// Acos returns the arccosine, in radians, of n.\nfunc (ns *Namespace) Acos(n any) (float64, error) {\n\taf, err := cast.ToFloat64E(n)\n\tif err != nil {\n\t\treturn 0, errors.New(\"requires a numeric argument\")\n\t}\n\treturn math.Acos(af), nil\n}\n\n// Add adds the multivalued addends n1 and n2 or more values.\nfunc (ns *Namespace) Add(inputs ...any) (any, error) {\n\treturn ns.doArithmetic(inputs, '+')\n}","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/math/math.go#L32-L68","documentation":"math.Abs in Hugo casts its argument to float64 with cast.ToFloat64E; if the cast fails (the value isn't a number or a numeric string), this error is returned. It signals a type problem in the template input, not a math problem.","triggerScenarios":"`{{ math.Abs \"abc\" }}`, `{{ math.Abs .Params.offset }}` where the param is a non-numeric string, a map, a slice, or nil — anything spf13/cast cannot convert to float64.","commonSituations":"Front-matter values quoted as non-numeric strings (e.g. \"−3\" with a unicode minus, \"3px\"); passing a whole object instead of its numeric field; nil params on pages missing the key.","solutions":["Pass a numeric value or numeric string: `{{ math.Abs -3.5 }}`.","Sanitize/convert first: `{{ math.Abs (float .Params.offset) }}` after stripping units, or use `default 0` for missing params.","Fix the front matter so the value is a plain number, not a decorated string."],"exampleFix":"<!-- before -->\n{{ math.Abs .Params.offset }}  {{/* offset: \"-3px\" */}}\n<!-- after -->\n{{ math.Abs (float (strings.TrimSuffix \"px\" .Params.offset)) }}","handlingStrategy":"type-guard","validationCode":"{{ $v := .Params.delta }}\n{{ if reflect.IsSlice $v }}{{ warnf \"expected number\" }}{{ else }}{{ math.Abs (float $v) }}{{ end }}","typeGuard":"{{ with try (float $v) }}{{ if not .Err }}{{ math.Abs .Value }}{{ end }}{{ end }}","tryCatchPattern":"{{ with try (math.Abs .Params.delta) }}{{ if .Err }}{{ warnf \"math.Abs: %s\" .Err }}{{ else }}{{ .Value }}{{ end }}{{ end }}","preventionTips":["Coerce untyped front-matter values with `float` or `int` before math.Abs","Remember YAML quotes make strings: `delta: \"-3\"` is a string until cast","Don't pass nil/missing params — guard with `with` first"],"tags":["hugo","templates","math","type-conversion"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}