rfc:raising_zero_to_power_of_negative_number

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
rfc:raising_zero_to_power_of_negative_number [2024/01/12 23:23] jorg_sowarfc:raising_zero_to_power_of_negative_number [2024/04/21 00:06] – Changed status to accepted jorg_sowa
Line 3: Line 3:
   * Date: 2024-01-11   * Date: 2024-01-11
   * Author: Jorg Sowa <jorg.sowa@gmail.com>   * Author: Jorg Sowa <jorg.sowa@gmail.com>
-  * Status: In discussion+  * Status: Accepted
   * First Published at: http://wiki.php.net/rfc/raising_zero_to_power_of_negative_number   * First Published at: http://wiki.php.net/rfc/raising_zero_to_power_of_negative_number
  
Line 10: Line 10:
 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. 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.+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 <php>INF</php>, while explicit division by zero gives a <php>DivisionByZeroError</php>.
  
 <code php> <code php>
 var_dump(0 ** -1); //float(INF) 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)); //float(INF)
 +var_dump(pow(0, -1.1)); //float(INF)
  
 var_dump(1 / 0); //DivisionByZeroError: Division by zero var_dump(1 / 0); //DivisionByZeroError: Division by zero
Line 27: Line 29:
 //PHP 8.4 //PHP 8.4
 var_dump(0 ** -1); //Deprecated: Zero raised to a negative power is deprecated 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)); //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 var_dump(1 / 0); //DivisionByZeroError: Division by zero
Line 37: Line 41:
 //PHP 9.0 //PHP 9.0
 var_dump(0 ** -1); //DivisionByZeroError: Zero cannot be raised to a negative power 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)); //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 var_dump(1 / 0); //DivisionByZeroError: Division by zero
 </code> </code>
 +
 +To provide a way to compute the numbers according to the IEEE 754 rules, the RFC proposes adding the function <php>fpow</php> similarly to the function <php>fdiv</php> that exists for the division operation, and <php>fmod</php> for the module operation for floating numbers.
 +
 +<code php>
 +//PHP 8.4
 +var_dump(fpow(0, -1)); //float(INF)
 +var_dump(fpow(0, -1.1)); //float(INF)
 +</code>
 +
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
-This change will break all code including raising the zero number to a negative power.+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 <php>fpow</php> function.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
 Deprecation Notice in the next minor PHP version 8.4. Deprecation Notice in the next minor PHP version 8.4.
  
-Throwing DivisionByZeroError in the next major PHP version: 9.0.+Function <php>fpow</php> in the next minor PHP version 8.4. 
 + 
 +Throwing <php>DivisionByZeroError</php> in the next major PHP version: 9.0.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
Line 54: Line 71:
 As per the voting RFC, a yes/no vote with a 2/3 majority is needed for this proposal to be accepted. 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-and closed 2024-x-x+Voting started on 2024-04-05 and will end on 2024-04-20 00:00 GMT. 
 + 
 +<doodle title="RFC: Raising zero to the power of negative number" auth="jorg_sowa" voteType="single" closed="false" closeon="2024-04-20T00:00:00Z"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Implementation ===== ===== Implementation =====
-https://github.com/jorgsowa/php-src/pull/11+https://github.com/php/php-src/pull/13128 (without fpow yet)
  
 The implementation is based on Ilija Tovilo's PoC: The implementation is based on Ilija Tovilo's PoC:
Line 64: Line 86:
 ===== References ===== ===== References =====
 Issue GH-8015: https://github.com/php/php-src/issues/8015 Issue GH-8015: https://github.com/php/php-src/issues/8015
 +RFC Division by zero: https://wiki.php.net/rfc/engine_warnings
  
rfc/raising_zero_to_power_of_negative_number.txt · Last modified: 2024/05/23 15:29 by jorg_sowa