{"id":"4cd8d3b4c28d4f3d","repo":"gohugoio/hugo","slug":"sqrt-operator-can-t-be-used-with-non-integer-or-fl","errorCode":null,"errorMessage":"Sqrt operator can't be used with non integer or float value","messagePattern":"Sqrt operator can't be used with non integer or float value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/math/math.go","lineNumber":244,"sourceCode":"\t}\n\n\treturn _round(xf), nil\n}\n\n// Sin returns the sine of the radian argument n.\nfunc (ns *Namespace) Sin(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.Sin(af), nil\n}\n\n// Sqrt returns the square root of the number n.\nfunc (ns *Namespace) Sqrt(n any) (float64, error) {\n\taf, err := cast.ToFloat64E(n)\n\tif err != nil {\n\t\treturn 0, errors.New(\"Sqrt operator can't be used with non integer or float value\")\n\t}\n\n\treturn math.Sqrt(af), nil\n}\n\n// Sub subtracts multivalued.\nfunc (ns *Namespace) Sub(inputs ...any) (any, error) {\n\treturn ns.doArithmetic(inputs, '-')\n}\n\n// Sum returns the sum of all numbers in inputs. Any slices in inputs are flattened.\nfunc (ns *Namespace) Sum(inputs ...any) (sum float64, err error) {\n\tfn := func(x, y float64) float64 {\n\t\treturn x + y\n\t}\n\treturn ns.applyOpToScalarsOrSlices(\"Sum\", fn, inputs...)\n}\n","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/math/math.go#L226-L262","documentation":"Hugo's `math.Sqrt` casts its argument to float64 with `cast.ToFloat64E` before calling Go's `math.Sqrt`. If the value isn't an integer, float, or numeric string, the cast fails and Hugo returns this error. Note that negative inputs are NOT caught here — they return NaN, per Go semantics — only non-numeric types trigger this error.","triggerScenarios":"`{{ math.Sqrt X }}` with a non-numeric X: `{{ math.Sqrt \"sixteen\" }}`, `{{ math.Sqrt .Params.area }}` when the param is absent (nil), or passing a slice/map/dict by mistake.","commonSituations":"Values from data files or front matter quoted as strings with non-numeric content; nil from missing params; accidentally passing a whole object (e.g. `.Params` instead of `.Params.area`) due to a typo.","solutions":["Verify the argument resolves to a number; cast with `{{ math.Sqrt (float $v) }}` for string-typed numeric values.","Guard nil params: `{{ with .Params.area }}{{ math.Sqrt . }}{{ end }}`.","If you later see NaN in output, also check the value isn't negative — that case passes this check silently."],"exampleFix":"<!-- before -->\n{{ math.Sqrt .Params.area }}\n<!-- after -->\n{{ with .Params.area }}{{ math.Sqrt (float .) }}{{ end }}","handlingStrategy":"validation","validationCode":"{{ $x := float $v }}{{ if ge $x 0 }}{{ math.Sqrt $x }}{{ end }}","typeGuard":"func sqrtSafe(v any) (float64, error) {\n    f, err := cast.ToFloat64E(v)\n    if err != nil || f < 0 {\n        return 0, fmt.Errorf(\"invalid sqrt input %v\", v)\n    }\n    return math.Sqrt(f), nil\n}","tryCatchPattern":"if _, err := ns.Sqrt(x); err != nil {\n    // non-numeric input — fix the data source\n}","preventionTips":["Convert with `float` (or `int`) before `math.Sqrt`","Also check for negative inputs — conversion succeeds but the result is NaN","Validate numeric fields in data files with a schema check before the build"],"tags":["hugo","go-templates","math","type-conversion"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}