gohugoio/hugo · error

Log operator can't be used with non integer or float value

Error message

Log operator can't be used with non integer or float value

What it means

Thrown by math.Log (tpl/math/math.go:139) when its argument cannot be converted to float64. Log computes the natural logarithm and requires a numeric input. (Despite the message mentioning 'integer or float', the implementation simply tries cast.ToFloat64E.)

Solutions

  1. Pass a positive numeric value to math.Log.
  2. Add a numeric default and guard against <= 0 if Inf/NaN would break downstream rendering.
  3. Validate the source field is numeric and positive.
  4. Fix the source data.

Example fix

// before
{{ math.Log .Params.rate }}

// after
{{ $r := .Params.rate | default 1 }}
{{ if gt $r 0 }}{{ math.Log $r }}{{ end }}
Defensive patterns

Strategy: validation

Validate before calling

// Validate numeric AND positive input before math.Log (Log(0) gives -Inf):
{{ $r := .Param "rate" | default 1 }}
{{ if gt $r 0 }}{{ math.Log $r }}{{ end }}

Type guard

// Go caller:
func positiveNumber(v any) bool {
    f, err := cast.ToFloat64E(v)
    return err == nil && f > 0
}

Prevention

When it happens

Trigger: Calling {{ math.Log .Value }} with a non-numeric string, nil, bool, struct, or slice. Note also that Log(0) or negative inputs return -Inf/NaN without an error from this check, so domain issues are separate.

Common situations: Unset param (nil); text field mistaken for a number; passing 0 or negative numbers and then being surprised by Inf/NaN downstream.

Related errors


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

Appendix: source

Thrown at tpl/math/math.go:139

func (ns *Namespace) Div(inputs ...any) (any, error) {
	return ns.doArithmetic(inputs, '/')
}

// Floor returns the greatest integer value less than or equal to n.
func (ns *Namespace) Floor(n any) (float64, error) {
	xf, err := cast.ToFloat64E(n)
	if err != nil {
		return 0, errors.New("Floor operator can't be used with non-float value")
	}

	return math.Floor(xf), nil
}

// Log returns the natural logarithm of the number n.
func (ns *Namespace) Log(n any) (float64, error) {
	af, err := cast.ToFloat64E(n)
	if err != nil {
		return 0, errors.New("Log operator can't be used with non integer or float value")
	}

	return math.Log(af), nil
}

// Max returns the greater of all numbers in inputs. Any slices in inputs are flattened.
func (ns *Namespace) Max(inputs ...any) (maximum float64, err error) {
	return ns.applyOpToScalarsOrSlices("Max", math.Max, inputs...)
}

// MaxInt64 returns the maximum value for a signed 64-bit integer.
func (ns *Namespace) MaxInt64() int64 {
	return math.MaxInt64
}

// Min returns the smaller of all numbers in inputs. Any slices in inputs are flattened.
func (ns *Namespace) Min(inputs ...any) (minimum float64, err error) {
	return ns.applyOpToScalarsOrSlices("Min", math.Min, inputs...)

View on GitHub (pinned to 52c9bd7908)