{"record":{"id":"de17fd0ef8785b4f","repo":"qax-os/excelize","slug":"div-0","errorCode":"#DIV/0!","errorMessage":"#DIV/0!","messagePattern":"#DIV/0!","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"calc.go","lineNumber":1407,"sourceCode":"\tif rOpdVal.Type != ArgNumber {\n\t\treturn errors.New(rOpdVal.String)\n\t}\n\topdStack.Push(newNumberFormulaArg(lOpdVal.Number * rOpdVal.Number))\n\treturn nil\n}\n\n// calcDiv evaluate division arithmetic operations.\nfunc calcDiv(rOpd, lOpd formulaArg, opdStack *Stack) error {\n\tlOpdVal := lOpd.ToNumber()\n\tif lOpdVal.Type != ArgNumber {\n\t\treturn errors.New(lOpdVal.String)\n\t}\n\trOpdVal := rOpd.ToNumber()\n\tif rOpdVal.Type != ArgNumber {\n\t\treturn errors.New(rOpdVal.String)\n\t}\n\tif rOpdVal.Number == 0 {\n\t\treturn errors.New(formulaErrorDIV)\n\t}\n\topdStack.Push(newNumberFormulaArg(lOpdVal.Number / rOpdVal.Number))\n\treturn nil\n}\n\n// calculate evaluate basic arithmetic operations.\nfunc calculate(opdStack *Stack, opt efp.Token) error {\n\tif opt.TValue == \"-\" && opt.TType == efp.TokenTypeOperatorPrefix {\n\t\tif opdStack.Len() < 1 {\n\t\t\treturn ErrInvalidFormula\n\t\t}\n\t\topd := opdStack.Pop().(formulaArg)\n\t\topdStack.Push(newNumberFormulaArg(0 - opd.ToNumber().Number))\n\t}\n\tif opt.TValue == \"-\" && opt.TType == efp.TokenTypeOperatorInfix {\n\t\tif opdStack.Len() < 2 {\n\t\t\treturn ErrInvalidFormula\n\t\t}","sourceCodeStart":1389,"sourceCodeEnd":1425,"githubUrl":"https://github.com/qax-os/excelize/blob/f2483381fbfbe432a6baa98d48a1c277183f3285/calc.go#L1389-L1425","documentation":"calcDiv explicitly checks for division by zero: if the right operand converts to the number 0, it returns errors.New(formulaErrorDIV), producing the #DIV/0! error, exactly as Excel does. The library does not substitute infinity or a default; the formula evaluation aborts with this error value.","triggerScenarios":"Any formula like =A1/B1 where B1 evaluates to 0; =SUM(A1:A5)/COUNT(B1:B5) where the COUNT is 0 (empty range); AVERAGE over no cells used as a denominator.","commonSituations":"Empty templates where denominator cells are filled in later; aggregations over filtered/empty datasets yielding 0; lookup keys missing so the denominator resolves to 0 or an empty cell (empty coerces to 0).","solutions":["Guard the denominator: =IF(B1=0,\"\",A1/B1) or =IFERROR(A1/B1,0)","Pre-check in code that the divisor cell is non-zero before running Calculate","Fill in the denominator data before evaluating dependent formulas","Catch the error from Calculate and treat #DIV/0! as an expected business condition"],"exampleFix":"// before: =A1/B1 (B1 is 0) -> #DIV/0!\n// after: =IFERROR(A1/B1,0)","handlingStrategy":"try-catch","validationCode":"v, _ := f.GetCellValue(\"Sheet1\", \"B1\")\nif n, err := strconv.ParseFloat(v, 64); err == nil && n == 0 {\n    return fmt.Errorf(\"divisor B1 is zero\")\n}","typeGuard":null,"tryCatchPattern":"if _, err := f.Calculate(); err != nil {\n    if strings.Contains(err.Error(), \"#DIV/0!\") {\n        return 0 // business-defined fallback\n    }\n    return err\n}","preventionTips":["Always wrap divisions in IFERROR or IF(den=0,...) in formulas","Fill denominator cells before recalculating dependent formulas","Watch for empty cells: they coerce to 0 as divisors"],"tags":["excel","formula","division-by-zero"],"backgroundTag":"division-by-zero","analyzedSha":"f2483381fbfbe432a6baa98d48a1c277183f3285","analyzedAt":"2026-09-02T01:26:19.299Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}