{"record":{"id":"4719e14f17ce91a4","repo":"karatelabs/karate","slug":"division-by-zero","errorCode":null,"errorMessage":"Division by zero","messagePattern":"Division by zero","errorType":"exception","errorClass":"JsErrorException (rangeError)","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Terms.java","lineNumber":759,"sourceCode":"        if (context != null && context.isError()) return UNDEFINED;\n        if (isBigIntOp(lhs, rhs)) {\n            requireBothBigInt(lhs, rhs, \"*\");\n            return narrowBigInt(((BigInteger) lhs).multiply((BigInteger) rhs));\n        }\n        double result = lhs.doubleValue() * rhs.doubleValue();\n        return narrow(result);\n    }\n\n    static Object div(Object lhsObject, Object rhsObject, CoreContext context) {\n        Number lhs = toNumericOperand(lhsObject, context);\n        if (context != null && context.isError()) return UNDEFINED;\n        Number rhs = toNumericOperand(rhsObject, context);\n        if (context != null && context.isError()) return UNDEFINED;\n        if (isBigIntOp(lhs, rhs)) {\n            requireBothBigInt(lhs, rhs, \"/\");\n            BigInteger r = (BigInteger) rhs;\n            if (r.signum() == 0) {\n                throw JsErrorException.rangeError(\"Division by zero\");\n            }\n            return narrowBigInt(((BigInteger) lhs).divide(r));\n        }\n        // no special-casing for zero / Infinity operands: IEEE 754 double division is\n        // exactly what the spec's Number::divide mandates, and narrow() preserves -0.0\n        double result = lhs.doubleValue() / rhs.doubleValue();\n        return narrow(result);\n    }\n\n    static Object min(Object lhsObject, Object rhsObject, CoreContext context) {\n        Number lhs = toNumericOperand(lhsObject, context);\n        if (context != null && context.isError()) return UNDEFINED;\n        Number rhs = toNumericOperand(rhsObject, context);\n        if (context != null && context.isError()) return UNDEFINED;\n        if (isBigIntOp(lhs, rhs)) {\n            requireBothBigInt(lhs, rhs, \"-\");\n            return narrowBigInt(((BigInteger) lhs).subtract((BigInteger) rhs));\n        }","sourceCodeStart":741,"sourceCodeEnd":777,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Terms.java#L741-L777","documentation":"Karate's JS engine throws this RangeError when a BigInt division (the `/` operator with both operands as BigInt) has a zero divisor. IEEE 754 double division allows division by zero (yielding Infinity), but the BigInt spec mandates a RangeError. Terms.java implements the JS division operator and mirrors V8 behavior.","triggerScenarios":"Evaluating JS like `10n / 0n` or `x / y` where both operands are BigInt and y equals 0n via the embedded JS interpreter (karate-js Terms.div).","commonSituations":"JS expressions in Karate features or kjs scripts doing dynamic division where a computed BigInt divisor evaluates to zero (e.g. averaged counts, ratios with an empty denominator).","solutions":["Guard the divisor before dividing: if (y === 0n) return 0n; or return a sentinel instead of dividing","Cast to a normal number if BigInt precision is not required, so division by zero yields Infinity instead of throwing","Wrap the expression in try/catch and treat RangeError as an 'undefined result' case"],"exampleFix":"// before (JS in Karate)\nvar ratio = totaln / countn;\n// after\nvar ratio = countn === 0n ? 0n : totaln / countn;","handlingStrategy":"validation","validationCode":"if (typeof divisor !== 'bigint') throw new Error('divisor must be BigInt');\nif (divisor === 0n) throw new Error('divisor must be non-zero BigInt');","typeGuard":"function isNonZeroBigInt(v) { return typeof v === 'bigint' && v !== 0n; }","tryCatchPattern":"try { result = dividend / divisor; } catch (e) { if (String(e).includes('Division by zero')) result = 0n; else throw e; }","preventionTips":["Always validate BigInt divisors against 0n before dividing","Prefer Number arithmetic unless exact integer precision is required","Centralize division in a helper that guards the zero case"],"tags":["javascript","bigint","division"],"backgroundTag":"value-out-of-range","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}