Raising a number to the power of a negative number is equivalent to taking the reciprocal of the number raised to the positive opposite of the power.
If we raise the number zero to the power of a negative number we end up with the one divided by zero, which gives an undefined result. Currently in PHP, such operation results in INF
, while explicit division by zero gives a DivisionByZeroError
.
var_dump(0 ** -1); //float(INF) var_dump(0 ** -1.1); //float(INF) var_dump(pow(0, -1)); //float(INF) var_dump(pow(0, -1.1)); //float(INF) var_dump(1 / 0); //DivisionByZeroError: Division by zero
The RFC proposes to change the behavior of this operation to match the division by zero operation. In the next minor version raising a number to the power of a negative number will throw a Deprecation, and in the major version, this operation will throw DivisionByZeroError.
//PHP 8.4 var_dump(0 ** -1); //Deprecated: Zero raised to a negative power is deprecated var_dump(0 ** -1.1); //Deprecated: Zero raised to a negative power is deprecated var_dump(pow(0, -1)); //Deprecated: Zero raised to a negative power is deprecated var_dump(pow(0, -1.1)); //Deprecated: Zero raised to a negative power is deprecated var_dump(1 / 0); //DivisionByZeroError: Division by zero
//PHP 9.0 var_dump(0 ** -1); //DivisionByZeroError: Zero cannot be raised to a negative power var_dump(0 ** -1.1); //DivisionByZeroError: Zero cannot be raised to a negative power var_dump(pow(0, -1)); //DivisionByZeroError: Zero cannot be raised to a negative power var_dump(pow(0, -1.1)); //DivisionByZeroError: Zero cannot be raised to a negative power var_dump(1 / 0); //DivisionByZeroError: Division by zero
To provide a way to compute the numbers according to the IEEE 754 rules, the RFC proposes adding the function fpow
similarly to the function fdiv
that exists for the division operation, and fmod
for the module operation for floating numbers.
//PHP 8.4 var_dump(fpow(0, -1)); //float(INF) var_dump(fpow(0, -1.1)); //float(INF)
This change will break code including raising the zero number to a negative power. Such cases may keep their current behavior by using the new fpow
function.
Deprecation Notice in the next minor PHP version 8.4.
Function fpow
in the next minor PHP version 8.4.
Throwing DivisionByZeroError
in the next major PHP version: 9.0.
As per the voting RFC, a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.
Voting started on 2024-04-05 and will end on 2024-04-20 00:00 GMT.
Commit: https://github.com/php/php-src/commit/23afe57f01e0915eef246eba60e60fda74fd2dcf
PR: https://github.com/php/php-src/pull/13128
The implementation is based on Ilija Tovilo's PoC: https://github.com/php/php-src/issues/8015#issuecomment-1193391843
Issue GH-8015: https://github.com/php/php-src/issues/8015
RFC Division by zero: https://wiki.php.net/rfc/engine_warnings