This is an old revision of the document!
PHP RFC: Raising zero to the power of negative number
- Version: 1.0
- Date: 2024-01-11
- Author: Jorg Sowa jorg.sowa@gmail.com
- Status: In discussion
- First Published at: http://wiki.php.net/rfc/raising_zero_to_power_of_negative_number
Introduction
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(pow(0, -1)); //float(INF) var_dump(1 / 0); //DivisionByZeroError: Division by zero
Proposal
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 notice, and for the major version, this operation will throw DivisionByZeroError.
//PHP 8.4 var_dump(0 ** -1); //Deprecated: Power of base 0 and the negative exponent is deprecated var_dump(pow(0, -1)); //Deprecated: Power of base 0 and the negative exponent is deprecated var_dump(1 / 0); //DivisionByZeroError: Division by zero
//PHP 9.0 var_dump(0 ** -1); //DivisionByZeroError: Division by zero var_dump(pow(0, -1)); //DivisionByZeroError: Division by zero var_dump(1 / 0); //DivisionByZeroError: Division by zero
Backward Incompatible Changes
This change will break all code including raising the zero number to a negative power.
Proposed PHP Version(s)
Deprecation Notice in the next minor PHP version 8.4.
Throwing DivisionByZeroError in the next major PHP version: 9.0.
Proposed Voting Choices
As per the voting RFC, a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.
Voting started 2024-x-x and closed 2024-x-x.
Implementation
https://github.com/jorgsowa/php-src/pull/11
The implementation is based on Ilija Tovilo's PoC: https://github.com/php/php-src/issues/8015#issuecomment-1193391843
References
Issue GH-8015: https://github.com/php/php-src/issues/8015