mikefarah/yq · error

cannot modulo by 0

Error message

cannot modulo by 0

What it means

moduloScalars verified both operands parse as int64 and then checks the divisor before computing lhsNum % rhsNum — a rhs of integer zero would panic the runtime (integer division by zero), so the guard converts it into an explicit error. This is the modulo equivalent of a divide-by-zero check.

Source

Thrown at pkg/yqlib/operator_modulo.go:57

		// custom tag - we have to have a guess
		lhsTag = lhs.guessTagFromCustomType()
		lhsIsCustom = true
	}

	if lhsTag == "!!int" && rhsTag == "!!int" {
		target.Kind = ScalarNode
		target.Style = lhs.Style

		format, lhsNum, err := parseInt64(lhs.Value)
		if err != nil {
			return err
		}
		_, rhsNum, err := parseInt64(rhs.Value)
		if err != nil {
			return err
		}
		if rhsNum == 0 {
			return fmt.Errorf("cannot modulo by 0")
		}
		remainder := lhsNum % rhsNum

		target.Tag = lhs.Tag
		target.Value = fmt.Sprintf(format, remainder)
	} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
		target.Kind = ScalarNode
		target.Style = lhs.Style

		lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
		if err != nil {
			return err
		}
		rhsNum, err := strconv.ParseFloat(rhs.Value, 64)
		if err != nil {
			return err
		}
		remainder := math.Mod(lhsNum, rhsNum)

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Filter zero divisors: select(.mod != 0) before applying %
  2. Substitute a non-zero default: .n % (.mod // 1)
  3. Fix the data so the modulus operand is never 0
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pkg/yqlib/operator_modulo.go:57 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05). Data as JSON: /api/errors/e51445e445e5286a. Report an issue: GitHub.