{"record":{"id":"238f4b7206757de6","repo":"PHPOffice/PhpSpreadsheet","slug":"div-0","errorCode":"#DIV/0!","errorMessage":"#DIV/0!","messagePattern":"#DIV/0!","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Calculation\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Calculation/MathTrig/Helpers.php","lineNumber":89,"sourceCode":"    public static function validatePositive(float|int $number, ?string $except = null): void\n    {\n        if ($number > 0) {\n            return;\n        }\n\n        throw new Exception($except ?? ExcelError::NAN());\n    }\n\n    /**\n     * Confirm number != 0.\n     */\n    public static function validateNotZero(float|int $number): void\n    {\n        if ($number) {\n            return;\n        }\n\n        throw new Exception(ExcelError::DIV0());\n    }\n\n    public static function returnSign(float $number): int\n    {\n        return $number ? (($number > 0) ? 1 : -1) : 0;\n    }\n\n    public static function getEven(float $number): float\n    {\n        $significance = 2 * self::returnSign($number);\n\n        return $significance ? (ceil($number / $significance) * $significance) : 0;\n    }\n\n    /**\n     * Return NAN or value depending on argument.\n     */\n    public static function numberOrNan(float $result): float|string","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Calculation/MathTrig/Helpers.php#L71-L107","documentation":"MathTrig\\Helpers::validateNotZero() rejects a zero divisor/denominator with ExcelError::DIV0() ('#DIV/0!'). Its call sites are MathTrig\\Operations::mod() (MOD) and MathTrig\\Operations::quotient() (QUOTIENT), replicating Excel's behaviour for =MOD(x,0) and =QUOTIENT(x,0). The engine converts the thrown Calculation\\Exception into the cell value '#DIV/0!'; direct PHP calls see the raw exception.","triggerScenarios":"=MOD(10,0) or =QUOTIENT(5,0); MOD/QUOTIENT where the divisor cell is empty, evaluates to 0, or a referenced chain yields 0 (e.g. =MOD(A1,B1-B2) with B1=B2); calling Operations::mod(10, 0) directly.","commonSituations":"Divisor columns containing blanks or zero-valued rows in imported CSV/Xlsx data; downstream formulas where a subtraction used as the divisor collapses to zero; data-entry spreadsheets where the divisor is only filled in later; migrating formulas that silently relied on PHP's ownDivision semantics.","solutions":["Guard the divisor in the formula: =IF(B1<>0, MOD(A1,B1), 0) or wrap with =IFERROR(MOD(A1,B1),\"\")","Fix the source data so the divisor cell is non-zero (or explicitly 0-handled)","Pre-check values in PHP before writing the formula if you generate formulas programmatically","When calling Operations::mod()/quotient() directly, test $divisor != 0 first or catch Calculation\\Exception"],"exampleFix":"// before\n$sheet->getCell('C1')->setValue('=MOD(A1,B1)'); // B1 = 0 -> #DIV/0!\n\n// after\n$sheet->getCell('C1')->setValue('=IF(B1=0, 0, MOD(A1,B1))');","handlingStrategy":"validation","validationCode":"if (!is_numeric($divisor) || (float) $divisor === 0.0) {\n    return 0; // or skip / report\n}\n$result = \\PhpOffice\\PhpSpreadsheet\\Calculation\\MathTrig\\Operations::quotient($numerator, $divisor);","typeGuard":"function isNonZeroNumber(mixed $d): bool\n{\n    return (is_int($d) || is_float($d)) && $d !== 0;\n}","tryCatchPattern":"try {\n    $r = Operations::mod($dividend, $divisor);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Calculation\\Exception $e) {\n    if ($e->getMessage() === '#DIV/0!') { $r = 0; } else { throw $e; }\n}","preventionTips":["Treat blank/zero divisor cells as data defects: validate source columns before formula generation","Prefer =IF(B1=0, fallback, MOD(A1,B1)) in generated formulas","For dynamic divisors (B1-B2 patterns), add a zero check or IFERROR wrapper","In PHP callers, short-circuit on zero before invoking mod/quotient"],"tags":["phpspreadsheet","excel-formula","math","division-by-zero","div0-error"],"backgroundTag":"division-by-zero","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}