BoundaryML/baml · error

modulo by zero in %= operator

Error message

modulo by zero in %= operator

What it means

THIR interpreter runtime error: the `%=` (modulo) operator was evaluated with a zero right-hand side. Like division, modulo by zero has no defined result, so evaluation of the compound assignment aborts with this bail at runtime.

Solutions

  1. Check the modulus is nonzero before applying %=.
  2. Return a default value when the modulus is 0.
  3. Fix the upstream computation producing the 0 modulus.

Example fix

// before
idx %= bucketSize
// after
if bucketSize != 0 { idx %= bucketSize } else { idx = 0 }
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try { applyAssignOp('ModAssign', lhs, rhs); } catch (e) { if (e.message.includes('modulo by zero in %=')) { /* use fallback index */ } else { throw e; } }

Prevention

When it happens

Trigger: Executing `x %= y` where x and y are Int and y equals 0.

Common situations: Modulo by a computed value that turned out to be 0 (e.g. bucket size, chunk length from empty config).

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/2c50f2d35a6b81d3. Report an issue: GitHub.

Appendix: source

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

                                        bail!("division by zero in /= operator");
                                    }
                                    BamlValueWithMeta::Float((a as f64) / b, meta)
                                }
                                (
                                    BamlValueWithMeta::Float(a, meta),
                                    BamlValueWithMeta::Int(b, _),
                                ) => {
                                    if b == 0 {
                                        bail!("division by zero in /= operator");
                                    }
                                    BamlValueWithMeta::Float(a / (b as f64), meta)
                                }
                                _ => bail!("unsupported types for /= operator"),
                            },
                            AssignOp::ModAssign => match (current_val.clone(), rhs_val.clone()) {
                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
                                    if b == 0 {
                                        bail!("modulo by zero in %= operator");
                                    }
                                    BamlValueWithMeta::Int(a % b, meta)
                                }
                                _ => bail!("unsupported types for %= operator"),
                            },
                            AssignOp::BitXorAssign => {
                                match (current_val.clone(), rhs_val.clone()) {
                                    (
                                        BamlValueWithMeta::Int(a, meta),
                                        BamlValueWithMeta::Int(b, _),
                                    ) => BamlValueWithMeta::Int(a ^ b, meta),
                                    _ => bail!("bitwise ^= requires integer operands"),
                                }
                            }
                            AssignOp::BitAndAssign => {
                                match (current_val.clone(), rhs_val.clone()) {
                                    (
                                        BamlValueWithMeta::Int(a, meta),

View on GitHub (pinned to bd85ce9dee)