BoundaryML/baml · error
division by zero at
Error message
division by zero at {:?} What it means
The `/` division operator checks the divisor and bails when it is zero — for both integer division (b == 0) and float division (b == 0.0). The interpreter converts the error to a runtime failure at the expression's source position rather than producing infinity or NaN.
Solutions
- Check the divisor for zero before dividing and return a sentinel/default value for the zero case
- Guarantee denominators come from validated, nonzero sources
- For averages over possibly-empty lists, special-case the empty case
- Catch the runtime error around evaluation and handle it
Example fix
// before
let avg = total / count; // count can be 0
// after
let avg = if (count == 0) { 0.0 } else { total / count }; Defensive patterns
Strategy: validation
Validate before calling
if (divisor === 0 || divisor === 0.0) { throw new Error('guard: divisor is zero, skip division'); } Type guard
function isNonZeroNumber(v) { return typeof v === 'number' && v !== 0; } Try / catch
try { result = a / b; } catch (e) { if (String(e).includes('division by zero')) { result = defaultValue; } else { throw e; } } Prevention
- Always check divisors for zero before dividing
- Special-case empty collections before computing averages
- Validate user/LLM-supplied numeric inputs
When it happens
Trigger: Evaluating `a / b` where b is the integer 0 or float 0.0, including when b is a computed value or variable that happens to be zero (e.g. an empty-sum denominator, a count of zero items).
Common situations: Computing averages/ratios over empty collections (count = 0); user-supplied or LLM-supplied divisors that can be zero; percentages with total = 0.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- division by zero in /= operator
- modulo by zero in %= operator
- expected value, found function
- for loop requires iterable (list)
- instanceof requires a class instance on the left side at
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/a8572a960fb24660.
Report an issue: GitHub.
Appendix: source
Thrown at engine/baml-compiler/src/thir/interpret.rs:2401
BinaryOperator::Mul => match (left_val.clone(), right_val.clone()) {
(BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
BamlValueWithMeta::Int(a * b, meta.clone())
}
(BamlValueWithMeta::Float(a, _), BamlValueWithMeta::Float(b, _)) => {
BamlValueWithMeta::Float(a * b, meta.clone())
}
(BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Float(b, _)) => {
BamlValueWithMeta::Float((a as f64) * b, meta.clone())
}
(BamlValueWithMeta::Float(a, _), BamlValueWithMeta::Int(b, _)) => {
BamlValueWithMeta::Float(a * (b as f64), meta.clone())
}
_ => bail!("unsupported types for * operator at {:?}", meta.0),
},
BinaryOperator::Div => match (left_val.clone(), right_val.clone()) {
(BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
if b == 0 {
bail!("division by zero at {:?}", meta.0);
}
BamlValueWithMeta::Float((a as f64) / (b as f64), meta.clone())
}
(BamlValueWithMeta::Float(a, _), BamlValueWithMeta::Float(b, _)) => {
if b == 0.0 {
bail!("division by zero at {:?}", meta.0);
}
BamlValueWithMeta::Float(a / b, meta.clone())
}
(BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Float(b, _)) => {
if b == 0.0 {
bail!("division by zero at {:?}", meta.0);
}
BamlValueWithMeta::Float((a as f64) / b, meta.clone())
}
(BamlValueWithMeta::Float(a, _), BamlValueWithMeta::Int(b, _)) => {
if b == 0 {
bail!("division by zero at {:?}", meta.0);View on GitHub (pinned to bd85ce9dee)