BoundaryML/baml · error

division by zero in /= operator

Error message

division by zero in /= operator

What it means

The /= operator explicitly guards against integer division by zero: when the right-hand operand is Int 0, handle_statement bails instead of producing an infinity/NaN result. BAML's interpreter converts /= results to float but rejects a zero divisor outright.

Solutions

  1. Check the divisor is nonzero before applying /=.
  2. Return a default value (e.g. 0.0 or null) when the divisor is 0.
  3. Trace why the divisor evaluated to 0 (often an empty list or unset variable).

Example fix

// before
avg /= n
// after
if n != 0 { avg /= n } else { avg = 0.0 }
Defensive patterns

Strategy: validation

Validate before calling

if (rhs === 0) throw new Error('divisor is zero');

Type guard

function isNonZeroInt(v) { return typeof v === 'number' && Number.isInteger(v) && v !== 0; }

Try / catch

try { applyAssignOp('DivAssign', lhs, rhs); } catch (e) { if (e.message.includes('division by zero in /=')) { /* use fallback value */ } else { throw e; } }

Prevention

When it happens

Trigger: Executing `x /= 0` or `x /= y` where y evaluates to Int 0 at runtime.

Common situations: Dividing by a computed count that turned out to be 0 (e.g. empty collection size, average with no samples).

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


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/9f37ce0e7e885117. Report an issue: GitHub.

Appendix: source

Thrown at engine/baml-compiler/src/thir/interpret.rs:924

                                }
                                (
                                    BamlValueWithMeta::Float(a, meta),
                                    BamlValueWithMeta::Float(b, _),
                                ) => BamlValueWithMeta::Float(a * b, meta),
                                (
                                    BamlValueWithMeta::Int(a, meta),
                                    BamlValueWithMeta::Float(b, _),
                                ) => BamlValueWithMeta::Float((a as f64) * b, meta),
                                (
                                    BamlValueWithMeta::Float(a, meta),
                                    BamlValueWithMeta::Int(b, _),
                                ) => BamlValueWithMeta::Float(a * (b as f64), meta),
                                _ => bail!("unsupported types for *= operator"),
                            },
                            AssignOp::DivAssign => match (current_val.clone(), rhs_val.clone()) {
                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
                                    if b == 0 {
                                        bail!("division by zero in /= operator");
                                    }
                                    BamlValueWithMeta::Float((a as f64) / (b as f64), meta)
                                }
                                (
                                    BamlValueWithMeta::Float(a, meta),
                                    BamlValueWithMeta::Float(b, _),
                                ) => {
                                    if b == 0.0 {
                                        bail!("division by zero in /= operator");
                                    }
                                    BamlValueWithMeta::Float(a / b, meta)
                                }
                                (
                                    BamlValueWithMeta::Int(a, meta),
                                    BamlValueWithMeta::Float(b, _),
                                ) => {
                                    if b == 0.0 {
                                        bail!("division by zero in /= operator");

View on GitHub (pinned to bd85ce9dee)