gastownhall/beads · error
division by zero
Error message
division by zero
What it means
parseMulDiv performs '/' only after checking the right operand is non-zero; a zero divisor raises this explicit error instead of returning IEEE +Inf. The library fails fast so users get a clear message rather than silently infinite/NaN results.
Source
Thrown at internal/formula/range.go:283
}
for {
switch p.current().typ {
case tokMul:
p.advance()
right, err := p.parsePow()
if err != nil {
return 0, err
}
left *= right
case tokDiv:
p.advance()
right, err := p.parsePow()
if err != nil {
return 0, err
}
if right == 0 {
return 0, fmt.Errorf("division by zero")
}
left /= right
default:
return left, nil
}
}
}
// parsePow handles ^ (power, highest binary precedence, right-associative)
func (p *exprParser) parsePow() (float64, error) {
base, err := p.parseUnary()
if err != nil {
return 0, err
}
if p.current().typ == tokPow {
p.advance()
exp, err := p.parsePow() // Right-associativeView on GitHub (pinned to 71377f2769)
Solutions
- Guard the divisor: use a non-zero constant or check the substituted variable value before evaluation
- Rewrite as multiplication by a safe value or clamp the divisor (e.g. 'x/max(y,1)')
- Log/inspect the variable values feeding the expression
Example fix
// before
range 1..count/0 { ... }
// after
range 1..count/workers { ... } // ensure workers != 0 Defensive patterns
Strategy: validation
Validate before calling
// before evaluating, ensure no variable that appears as a divisor is zero
for name, v := range vars {
if v == 0 { return fmt.Errorf("variable %s is zero; used as divisor", name) }
} Try / catch
val, err := EvaluateExpr(expr, vars)
if err != nil && strings.Contains(err.Error(), "division by zero") {
// substitute safe defaults for zero variables and retry
} Prevention
- Clamp divisor variables to a minimum of 1 before evaluation
- Validate config values that feed divisions at load time
- Prefer 'x/max(y,1)' patterns in user-authored expressions
When it happens
Trigger: EvaluateExpr with a division whose right side evaluates to 0, e.g. '10/0' or '8/(2-2)' in a range endpoint like 'range 1..10/0'.
Common situations: Variable placeholders substituted with 0 (empty config values), computed denominators that hit zero at runtime, off-by-one arithmetic like 'n-n'.
Related errors
- unexpected token after expression
- expected closing parenthesis
- unexpected token in expression
- parsing formula: %w
- resolving formula: %w
AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30).
Data as JSON: /api/errors/fed6f948e8314cce.
Report an issue: GitHub.