{"record":{"id":"9f37ce0e7e885117","repo":"BoundaryML/baml","slug":"division-by-zero-in-operator","errorCode":null,"errorMessage":"division by zero in /= operator","messagePattern":"division by zero in /= operator","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":924,"sourceCode":"                                }\n                                (\n                                    BamlValueWithMeta::Float(a, meta),\n                                    BamlValueWithMeta::Float(b, _),\n                                ) => BamlValueWithMeta::Float(a * b, meta),\n                                (\n                                    BamlValueWithMeta::Int(a, meta),\n                                    BamlValueWithMeta::Float(b, _),\n                                ) => BamlValueWithMeta::Float((a as f64) * b, meta),\n                                (\n                                    BamlValueWithMeta::Float(a, meta),\n                                    BamlValueWithMeta::Int(b, _),\n                                ) => BamlValueWithMeta::Float(a * (b as f64), meta),\n                                _ => bail!(\"unsupported types for *= operator\"),\n                            },\n                            AssignOp::DivAssign => match (current_val.clone(), rhs_val.clone()) {\n                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {\n                                    if b == 0 {\n                                        bail!(\"division by zero in /= operator\");\n                                    }\n                                    BamlValueWithMeta::Float((a as f64) / (b as f64), meta)\n                                }\n                                (\n                                    BamlValueWithMeta::Float(a, meta),\n                                    BamlValueWithMeta::Float(b, _),\n                                ) => {\n                                    if b == 0.0 {\n                                        bail!(\"division by zero in /= operator\");\n                                    }\n                                    BamlValueWithMeta::Float(a / b, meta)\n                                }\n                                (\n                                    BamlValueWithMeta::Int(a, meta),\n                                    BamlValueWithMeta::Float(b, _),\n                                ) => {\n                                    if b == 0.0 {\n                                        bail!(\"division by zero in /= operator\");","sourceCodeStart":906,"sourceCodeEnd":942,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L906-L942","documentation":"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.","triggerScenarios":"Executing `x /= 0` or `x /= y` where y evaluates to Int 0 at runtime.","commonSituations":"Dividing by a computed count that turned out to be 0 (e.g. empty collection size, average with no samples).","solutions":["Check the divisor is nonzero before applying /=.","Return a default value (e.g. 0.0 or null) when the divisor is 0.","Trace why the divisor evaluated to 0 (often an empty list or unset variable)."],"exampleFix":"// before\navg /= n\n// after\nif n != 0 { avg /= n } else { avg = 0.0 }","handlingStrategy":"validation","validationCode":"if (rhs === 0) throw new Error('divisor is zero');","typeGuard":"function isNonZeroInt(v) { return typeof v === 'number' && Number.isInteger(v) && v !== 0; }","tryCatchPattern":"try { applyAssignOp('DivAssign', lhs, rhs); } catch (e) { if (e.message.includes('division by zero in /=')) { /* use fallback value */ } else { throw e; } }","preventionTips":["Always check the divisor != 0 before /=.","Handle empty-collection counts before dividing.","Provide explicit fallback values for zero divisors."],"tags":["runtime","arithmetic","division-by-zero","interpreter"],"backgroundTag":"invalid-argument-value","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}