gohugoio/hugo · error

Pow operator can't be used with non-float value

Error message

Pow operator can't be used with non-float value

What it means

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.

Source

Thrown at tpl/math/math.go:202

}

// Mul multiplies the multivalued numbers n1 and n2 or more values.
func (ns *Namespace) Mul(inputs ...any) (any, error) {
	return ns.doArithmetic(inputs, '*')
}

// Pi returns the mathematical constant pi.
func (ns *Namespace) Pi() float64 {
	return math.Pi
}

// Pow returns n1 raised to the power of n2.
func (ns *Namespace) Pow(n1, n2 any) (float64, error) {
	af, erra := cast.ToFloat64E(n1)
	bf, errb := cast.ToFloat64E(n2)

	if erra != nil || errb != nil {
		return 0, errors.New("Pow operator can't be used with non-float value")
	}

	return math.Pow(af, bf), nil
}

// Product returns the product of all numbers in inputs. Any slices in inputs are flattened.
func (ns *Namespace) Product(inputs ...any) (product float64, err error) {
	fn := func(x, y float64) float64 {
		return x * y
	}
	return ns.applyOpToScalarsOrSlices("Product", fn, inputs...)
}

// Rand returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0).
func (ns *Namespace) Rand() float64 {
	return rand.Float64()
}

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Cast arguments explicitly: `{{ math.Pow (float $a) (float $b) }}`.
  2. Default missing params: `{{ math.Pow (default 2.0 .Params.base) 3 }}`.
  3. Clean the data source so the value is a plain number (unquoted in YAML/TOML/JSON).

Example fix

<!-- before -->
{{ math.Pow .Params.base 2 }}
<!-- after -->
{{ math.Pow (float (default 1 .Params.base)) 2 }}
Defensive patterns

Strategy: type-guard

Validate before calling

{{ $base := float $x }}{{ $exp := float $y }}{{ math.Pow $base $exp }}

Type guard

func isNumeric(v any) bool {
    switch v.(type) {
    case int, int64, float32, float64, uint, uint64:
        return true
    }
    return false
}

Try / catch

if _, err := ns.Pow(x, y); err != nil {
    // input not convertible to float — reject upstream data
}

Prevention

When it happens

Trigger: `{{ 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 `|`.

Common situations: 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.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/5730d52b0b352e8e.json. Report an issue: GitHub ↗.