{"record":{"id":"d04d2b4f4add411f","repo":"karatelabs/karate","slug":"exponent-must-be-non-negative","errorCode":null,"errorMessage":"Exponent must be non-negative","messagePattern":"Exponent must be non-negative","errorType":"exception","errorClass":"JsErrorException (rangeError)","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Terms.java","lineNumber":809,"sourceCode":"                throw JsErrorException.rangeError(\"Division by zero\");\n            }\n            // Java BigInteger.remainder matches JS BigInt % semantics (sign follows dividend)\n            return narrowBigInt(((BigInteger) lhs).remainder(r));\n        }\n        double result = lhs.doubleValue() % rhs.doubleValue();\n        return narrow(result);\n    }\n\n    static Object exp(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(\"Exponent must be non-negative\");\n            }\n            return narrowBigInt(((BigInteger) lhs).pow(r.intValueExact()));\n        }\n        double result = Math.pow(lhs.doubleValue(), rhs.doubleValue());\n        return narrow(result);\n    }\n\n    static Object add(Object lhs, Object rhs, CoreContext context) {\n        // Spec evaluation of binary +: ToPrimitive both operands first (default hint),\n        // then string-or-number dispatch on the *primitives*. ObjectLike on either side\n        // is the rare case — primitives short-circuit through the existing fast path.\n        if (lhs instanceof ObjectLike) {\n            lhs = toPrimitive(lhs, \"default\", context);\n            if (context != null && context.isError()) return UNDEFINED;\n        }\n        if (rhs instanceof ObjectLike) {\n            rhs = toPrimitive(rhs, \"default\", context);\n            if (context != null && context.isError()) return UNDEFINED;","sourceCodeStart":791,"sourceCodeEnd":827,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Terms.java#L791-L827","documentation":"BigInt exponentiation (`**`) with a negative exponent is undefined per the JS spec (a negative BigInt power is not an integer), so Terms.java throws a RangeError instead of calling BigInteger.pow. This matches V8's 'Exponent must be non-negative'.","triggerScenarios":"Evaluating `2n ** -3n` or `base ** exp` where both are BigInt and exp < 0n in karate-js.","commonSituations":"Generic math helper functions written for numbers reused with BigInt operands; computing reciprocal/powers like 10n ** -n when porting numeric code.","solutions":["Ensure the exponent is non-negative before the operation","Use Number arithmetic for negative exponents: `2 ** -3` gives 0.25","Compute the positive power and divide: 1n / (2n ** 3n) if an exact BigInt reciprocal fraction is acceptable"],"exampleFix":"// before\nvar p = baseN ** expN;\n// after\nvar p = expN < 0n ? (1n / (baseN ** -expN)) : (baseN ** expN);","handlingStrategy":"validation","validationCode":"if (exp < 0n) throw new Error('BigInt exponent must be non-negative: ' + exp);","typeGuard":"function canBigIntPow(b, e) { return typeof b === 'bigint' && typeof e === 'bigint' && e >= 0n; }","tryCatchPattern":"try { p = base ** exp; } catch (e) { if (String(e).includes('Exponent must be non-negative')) p = Number(base) ** Number(exp); else throw e; }","preventionTips":["Check exponent sign before BigInt **","Use Number math for fractional or negative exponents","Wrap math helpers with BigInt/Number assertions at their boundaries"],"tags":["javascript","bigint","exponentiation"],"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"}