{"record":{"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/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/math/math.go#L32-L68","documentation":"Thrown by math.Abs (tpl/math/math.go:50) when its single argument cannot be converted to a float64 via cast.ToFloat64E. math.Abs needs a numeric input; passing a non-numeric value (string that is not a number, bool, nil, struct, slice) makes the conversion fail. The message is Abs-specific, unlike the generic math functions.","triggerScenarios":"Calling {{ math.Abs .Param \"score\" }} where the param is a string like \"N/A\"; passing nil from an unset variable; passing a bool; passing a slice or struct.","commonSituations":"Front matter value is a quoted string that isn't numeric; a param is optional and unset (nil); type drift after a content migration.","solutions":["Ensure the argument is a number (int or float) before calling math.Abs.","Provide a numeric default: {{ math.Abs (.Param \"score\" | default 0) }}.","Cast with a check, or guard with {{ if where }} style conditionals on the value type.","Fix the source data so the front-matter field is numeric, not quoted."],"exampleFix":"// before\n{{ $abs := math.Abs .Params.score }}\n\n// after\n{{ $score := .Params.score | default 0 }}\n{{ $abs := math.Abs $score }}","handlingStrategy":"type-guard","validationCode":"// Ensure a numeric value before math.Abs:\n{{ $v := .Param \"score\" | default 0 }}\n{{ math.Abs $v }}","typeGuard":"// Go caller:\nfunc isNumeric(v any) bool {\n    switch reflect.ValueOf(v).Kind() {\n    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,\n        reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,\n        reflect.Float32, reflect.Float64:\n        return true\n    }\n    // cast also accepts numeric strings, so check that too:\n    if s, ok := v.(string); ok {\n        _, err := strconv.ParseFloat(s, 64)\n        return err == nil\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Pass numeric (int or float) values to math.Abs.","Add a numeric default for optional params.","Keep front-matter numeric fields unquoted."],"tags":["hugo","template","math","type-validation","abs"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}