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

math.Log computes the natural logarithm after casting the argument to float64; if the cast fails, this error is returned. It flags a type error in the input — note that a valid numeric input that is negative or zero will not raise this error but will return NaN/-Inf per Go's math.Log semantics.

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 8a468df065)

Solutions

  1. Pass a numeric argument: `{{ math.Log 10 }}`.
  2. Convert or default first: `{{ math.Log (float (.Params.count | default 1)) }}`.
  3. Separately guard against zero/negative inputs if NaN or -Inf in output would break downstream formatting.

Example fix

<!-- before -->
{{ math.Log .Params.count }}
<!-- after -->
{{ math.Log (float (.Params.count | default 1)) }}
Defensive patterns

Strategy: type-guard

Validate before calling

{{ $v := .Params.count }}
{{ $f := float $v }}
{{ if gt $f 0 }}{{ math.Log $f }}{{ end }}

Type guard

{{ with try (float $v) }}{{ if not .Err }}{{ math.Log .Value }}{{ end }}{{ end }}

Try / catch

{{ with try (math.Log $v) }}{{ if .Err }}{{ warnf "Log needs a number: %s" .Err }}{{ else }}{{ .Value }}{{ end }}{{ end }}

Prevention

When it happens

Trigger: `{{ math.Log "ten" }}` or `{{ math.Log .Params.count }}` where the param is nil, a non-numeric string, or a collection — anything cast.ToFloat64E rejects.

Common situations: Tag-cloud or weight-scaling templates (`math.Log .Count`-style) applied to values sourced from data files or params that are strings or missing; passing the result of a lookup that returned nil.

Related errors


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