ADR 0020: int ** int is an int; a negative exponent is rejected¶
Status: Final
Type: Feature
Created: 2026-07-29
Amends: 0005 (its decision that pow(x, y) is x ** y meets this rule)
Amended-By: 0075 (the int that int ** int gives grows past 64 bits)
Related-ADRs: 0002, whose debug/release overflow latitude does not reach here. Provenance: arbitrations A1, A9, A11, A50, A56, A189; upstream question U32.
Context¶
2 ** -1 returned 0 in both build modes, on all three backends, with no diagnostic. CPython returns 0.5.
The difficulty is the result type, which CPython works out from the exponent's sign at run time: long_pow delegates to float_pow when the exponent is negative, so 2 ** 3 is the int 8 and 2 ** -1 is the float 0.5. A compiler that gives every expression one type cannot reproduce that. Every way of not reproducing it is a choice about which half to lose:
- always an
int:2 ** -1has no answer; - always Float64:
2 ** 3prints8.0where CPython prints8, a divergence that hits the common case; - Float64 when the exponent is a statically known negative,
intotherwise: CPython-exact for a literal, and undecidable for a variable.
Decision¶
int ** int is an int, always¶
Each expression has one result type, a function of the operand types and of nothing else, the same shape as int / int → Float64, which this compiler already has. The exponent's sign changes what the operation does, never what it is. The int is CPython's where it grows (0075): 2 ** 70 is the exact value in an executable. Where int is 64 bits, a power the compiler folds past them is a compile error (A193). One computed at run time overflows as 0002 says.
A negative exponent fails, as early as its sign is knowable¶
| the exponent | what happens |
|---|---|
a statically known negative (a literal, a signed literal, a module constant, or any expression constant folding resolves to a negative int) |
PP100 at compile time, naming CPython's answer and the fix |
| not statically known, and of signed integer type | a guard, in both build modes, aborting through __pp_pow_neg_fail with the source line |
unsigned or Bool |
no check: there is no negative value to have |
The diagnostic reads:
**with the negative exponent -1 has no integer value: CPython promotes to float here (2 ** -1is0.5), so the result type would depend on the exponent's sign, which a compiled program fixes at compile time. Writefloat(base) ** expfor the float
and the runtime abort says the same thing with a line number. Both name one fix and no other, because float(base) ** exp already lowers to libm's pow and already gives 0.5. The message does not offer 1 / base ** 1, which looks like the other obvious rewrite and is not equivalent for a zero base: inf here, ZeroDivisionError in CPython, because int / int is a float division. A diagnostic must not recommend its way into a second divergence.
Both build modes: outside the spec §4.1.1 latitude¶
Spec §4.1.1 lets a release build wrap on integer overflow. This compiler takes that latitude for the 64-bit int (0002): debug traps, release wraps. That latitude does not reach here, though the two look similar. An overflowing a * b has a value in release: the wrapped one, which the target's hardware computed and which the spec permits. 2 ** -1 has no int value at all: the 0 it used to return was fabricated. So the check is not an overflow check, it is not an overflow-trap opcode, and --no-overflow-checks does not disable it. A program either computes exactly what CPython computes or stops; there is no silently-wrong-value mode for int ** int.
The run-time stop is a named divergence¶
Where CPython computes 2 ** -n as a float, the compiled program stops, in both modes, and says why. A stop where CPython computes is distance (A191). This one is kept by decision: if the program produced the float, the expression would have two result types, which the decision above refuses. The compile-time refusal covers every exponent whose sign folds, so the stop is reached only by an exponent computed at run time.
pow(x, y) and x ** y stay one program¶
0005 lowers pow(x, y) by rewriting it to the ** node before lowering, precisely so the two spellings cannot drift. This rule is the first that could have drifted, because it inspects the source exponent after the rewrite; pow(2, -1) and 2 ** -1 produce the same diagnostic, byte for byte, and a test holds the messages identical.
Alternatives considered¶
Promote a literal negative exponent to Float64. 2 ** -1 can be resolved at compile time, and giving it Float64 would be exactly what CPython computes. Refused for three reasons, in order of weight:
- It makes one operator mean two things, keyed on syntax.
2 ** -1would be aFloat64value andk = -1; 2 ** kan abort: same computation, same operands, two outcomes, and the difference is whether the author happened to spell a literal. A rule that is inconsistent as well as strict is worse than one that is merely strict, because nothing learned from the first refusal predicts the second. 0 ** -1would still need a runtime guard. CPython raisesZeroDivisionError: 0.0 cannot be raised to a negative power; C'spow(0.0, -1.0)is+inf. So the promoted path is not "compute it and stop worrying": it is a floatpowplus a zero-base check plus a second failure mode, with a base that is rarely statically known. The promotion buys one expression and pays for a second guard.- It introduces the implicit int→float promotion POST otherwise refuses, and refuses, on the grounds that promotion changes printed output (
min(1, 2.5)stays unlowered for exactly this reason). Deciding this case by promotion would pre-empt that standing refusal in the opposite direction, from a smaller case.
Consequences¶
2 ** -1stops returning0: it is a compile error.2.0 ** -1is0.5and always was.--no-overflow-checksdoes not turn the guard off, and should not: it exists for code that wants modular arithmetic. No arithmetic wants a fabricated zero.- The float
**cases follow 0002's rule for a result with no wrapped value:0.0 ** -1.0raises CPython'sZeroDivisionError, and(-8.0) ** 0.5, acomplexin CPython, stops in both modes, the same result-type problem one type wider. - The general question stays open upstream (U32):
**is the second operator whose CPython result type is a function of a value. There is no principle yet beyond "pick the type the types give you and refuse the rest". The third instance is the one that should produce the principle.