{"id":"5730d52b0b352e8e","repo":"gohugoio/hugo","slug":"pow-operator-can-t-be-used-with-non-float-value","errorCode":null,"errorMessage":"Pow operator can't be used with non-float value","messagePattern":"Pow operator can't be used with non-float value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/math/math.go","lineNumber":202,"sourceCode":"}\n\n// Mul multiplies the multivalued numbers n1 and n2 or more values.\nfunc (ns *Namespace) Mul(inputs ...any) (any, error) {\n\treturn ns.doArithmetic(inputs, '*')\n}\n\n// Pi returns the mathematical constant pi.\nfunc (ns *Namespace) Pi() float64 {\n\treturn math.Pi\n}\n\n// Pow returns n1 raised to the power of n2.\nfunc (ns *Namespace) Pow(n1, n2 any) (float64, error) {\n\taf, erra := cast.ToFloat64E(n1)\n\tbf, errb := cast.ToFloat64E(n2)\n\n\tif erra != nil || errb != nil {\n\t\treturn 0, errors.New(\"Pow operator can't be used with non-float value\")\n\t}\n\n\treturn math.Pow(af, bf), nil\n}\n\n// Product returns the product of all numbers in inputs. Any slices in inputs are flattened.\nfunc (ns *Namespace) Product(inputs ...any) (product float64, err error) {\n\tfn := func(x, y float64) float64 {\n\t\treturn x * y\n\t}\n\treturn ns.applyOpToScalarsOrSlices(\"Product\", fn, inputs...)\n}\n\n// Rand returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0).\nfunc (ns *Namespace) Rand() float64 {\n\treturn rand.Float64()\n}\n","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/math/math.go#L184-L220","documentation":"Hugo's `math.Pow` casts both arguments to float64 with `cast.ToFloat64E` before calling Go's `math.Pow`. If either argument can't be converted to a float — a non-numeric string, nil, or a composite type — Hugo returns this error rather than producing a bogus result.","triggerScenarios":"`{{ math.Pow A B }}` with a non-numeric argument: `{{ math.Pow \"two\" 3 }}`, `{{ math.Pow .Params.base 2 }}` when the param is missing (nil), or piping a non-numeric value in via `|`.","commonSituations":"Exponent or base pulled from front matter/data files as a quoted string that isn't a clean number (e.g. \"2x\"); missing `.Params` keys resolving to nil; shortcode `.Get` values that contain stray whitespace or units.","solutions":["Cast arguments explicitly: `{{ math.Pow (float $a) (float $b) }}`.","Default missing params: `{{ math.Pow (default 2.0 .Params.base) 3 }}`.","Clean the data source so the value is a plain number (unquoted in YAML/TOML/JSON)."],"exampleFix":"<!-- before -->\n{{ math.Pow .Params.base 2 }}\n<!-- after -->\n{{ math.Pow (float (default 1 .Params.base)) 2 }}","handlingStrategy":"type-guard","validationCode":"{{ $base := float $x }}{{ $exp := float $y }}{{ math.Pow $base $exp }}","typeGuard":"func isNumeric(v any) bool {\n    switch v.(type) {\n    case int, int64, float32, float64, uint, uint64:\n        return true\n    }\n    return false\n}","tryCatchPattern":"if _, err := ns.Pow(x, y); err != nil {\n    // input not convertible to float — reject upstream data\n}","preventionTips":["Wrap both arguments with `float` before `math.Pow`","Don't feed strings or nil .Params values into math functions; default them first with `default 0.0`","Add a data-validation partial that coerces numeric params once, then reuse the coerced values"],"tags":["hugo","go-templates","math","type-conversion"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}