rfc:raising_zero_to_power_of_negative_number

PHP RFC: Raising zero to the 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(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

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, 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)

Backward Incompatible Changes

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.

Proposed PHP Version(s)

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.

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 on 2024-04-05 and will end on 2024-04-20 00:00 GMT.

RFC: Raising zero to the power of negative number
Real name Yes No
alcaeus (alcaeus)  
asgrim (asgrim)  
beberlei (beberlei)  
bwoebi (bwoebi)  
crell (crell)  
dams (dams)  
derick (derick)  
ericmann (ericmann)  
gasolwu (gasolwu)  
girgias (girgias)  
heiglandreas (heiglandreas)  
kalle (kalle)  
kguest (kguest)  
mbeccati (mbeccati)  
mcmic (mcmic)  
nicolasgrekas (nicolasgrekas)  
petk (petk)  
ramsey (ramsey)  
reywob (reywob)  
saki (saki)  
santiagolizardo (santiagolizardo)  
sergey (sergey)  
svpernova09 (svpernova09)  
theodorejb (theodorejb)  
timwolla (timwolla)  
trowski (trowski)  
weierophinney (weierophinney)  
Final result: 27 0
This poll has been closed.

Implementation

References

rfc/raising_zero_to_power_of_negative_number.txt · Last modified: 2024/08/11 21:01 by jorg_sowa