qax-os/excelize · error

#DIV/0!

#DIV/0!

Error message

#DIV/0!

What it means

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.

Source

Thrown at calc.go:1407

	if rOpdVal.Type != ArgNumber {
		return errors.New(rOpdVal.String)
	}
	opdStack.Push(newNumberFormulaArg(lOpdVal.Number * rOpdVal.Number))
	return nil
}

// calcDiv evaluate division arithmetic operations.
func calcDiv(rOpd, lOpd formulaArg, opdStack *Stack) error {
	lOpdVal := lOpd.ToNumber()
	if lOpdVal.Type != ArgNumber {
		return errors.New(lOpdVal.String)
	}
	rOpdVal := rOpd.ToNumber()
	if rOpdVal.Type != ArgNumber {
		return errors.New(rOpdVal.String)
	}
	if rOpdVal.Number == 0 {
		return errors.New(formulaErrorDIV)
	}
	opdStack.Push(newNumberFormulaArg(lOpdVal.Number / rOpdVal.Number))
	return nil
}

// calculate evaluate basic arithmetic operations.
func calculate(opdStack *Stack, opt efp.Token) error {
	if opt.TValue == "-" && opt.TType == efp.TokenTypeOperatorPrefix {
		if opdStack.Len() < 1 {
			return ErrInvalidFormula
		}
		opd := opdStack.Pop().(formulaArg)
		opdStack.Push(newNumberFormulaArg(0 - opd.ToNumber().Number))
	}
	if opt.TValue == "-" && opt.TType == efp.TokenTypeOperatorInfix {
		if opdStack.Len() < 2 {
			return ErrInvalidFormula
		}

View on GitHub (pinned to f2483381fb)

Solutions

  1. Guard the denominator: =IF(B1=0,"",A1/B1) or =IFERROR(A1/B1,0)
  2. Pre-check in code that the divisor cell is non-zero before running Calculate
  3. Fill in the denominator data before evaluating dependent formulas
  4. Catch the error from Calculate and treat #DIV/0! as an expected business condition

Example fix

// before: =A1/B1 (B1 is 0) -> #DIV/0!
// after: =IFERROR(A1/B1,0)
Defensive patterns

Strategy: try-catch

Validate before calling

v, _ := f.GetCellValue("Sheet1", "B1")
if n, err := strconv.ParseFloat(v, 64); err == nil && n == 0 {
    return fmt.Errorf("divisor B1 is zero")
}

Try / catch

if _, err := f.Calculate(); err != nil {
    if strings.Contains(err.Error(), "#DIV/0!") {
        return 0 // business-defined fallback
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of qax-os/excelize@f2483381fb (2026-09-02). Data as JSON: /api/errors/de17fd0ef8785b4f. Report an issue: GitHub.