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

Thrown by the math.Pow template function when either of its two arguments cannot be converted to a float64 via cast.ToFloat64E. Pow delegates to math.Pow(af, bf), so both operands must be numeric (int, float, or a numeric string). Any non-numeric value (nil, bool, struct, slice, non-numeric string) triggers this error.

Solutions

  1. Ensure both arguments are numeric: use {{ math.Pow (float $base) (float $exp) }} or cast in front matter as numbers.
  2. Default nil/empty values before calling: {{ $base | default 0 }} then pass to math.Pow.
  3. If the value comes from .Params, declare it as a number in front matter (e.g. weight: 2 not weight: "2").

Example fix

// before
{{ math.Pow $base $exp }}
// after
{{ math.Pow (float $base) (float $exp) }}
Defensive patterns

Strategy: validation

Validate before calling

// In template: coerce before calling
{{ math.Pow (float $base) (float $exp) }}

// In Go: check numeric kind before invoking the namespace
func isNumeric(v any) bool {
    switch reflect.ValueOf(v).Kind() {
    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
         reflect.Float32, reflect.Float64, reflect.Uint, reflect.Uint8, reflect.Uint16,
         reflect.Uint32, reflect.Uint64:
        return true
    }
    return false
}

Prevention

When it happens

Trigger: Calling {{ math.Pow "abc" 2 }}, {{ math.Pow $page 2 }}, or {{ math.Pow nil 3 }} in a Hugo template. Also triggered by passing a .Params value that resolved to a non-numeric type (e.g. a map or slice).

Common situations: Front matter values typed as strings that look numeric but are not coerced; passing a page object or .Scratch Get result that is nil; migrating templates where a value used to be an int but became a string after a Hugo version change.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/5730d52b0b352e8e. Report an issue: GitHub.

Appendix: 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 52c9bd7908)