rfc:pow-operator

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
Next revisionBoth sides next revision
rfc:pow-operator [2013/12/19 01:56] datibbawrfc:pow-operator [2014/02/07 06:46] – Better (consistent) wording bwoebi
Line 1: Line 1:
  
 ====== PHP RFC: Power Operator ====== ====== PHP RFC: Power Operator ======
-  * Version: 0.1+  * Version: 0.3
   * Date: 2013-11-23   * Date: 2013-11-23
   * Author: Tjerk Meesters, datibbaw@php.net   * Author: Tjerk Meesters, datibbaw@php.net
-  * Status: Voting+  * Status: Implemented in PHP 5.6
   * First Published at: http://wiki.php.net/rfc/pow-operator   * First Published at: http://wiki.php.net/rfc/pow-operator
 +  * Revision (0.1 → 0.2): 2013-12-19
 +  * Revision (0.2 → 0.3): 2013-12-21
  
 ===== Introduction ===== ===== Introduction =====
  
-This proposal is three-fold:+This proposal is two-fold:
  
-  - Turn the existing [[http://php.net/pow|pow()]] function into a language construct.+  - Introduce an exponential (right associativeoperator ''<nowiki>**</nowiki>''.
     * Avoids a function call.     * Avoids a function call.
     * Support for [[rfc:operator_overloading_gmp|GMP overloading]].     * Support for [[rfc:operator_overloading_gmp|GMP overloading]].
-  - Introduce an exponential (right associative) operator ''<nowiki>**</nowiki>''. 
     * Easier to read and shorter to write.     * Easier to read and shorter to write.
     * Can be found in other languages.     * Can be found in other languages.
Line 20: Line 21:
  
 ===== Proposal ===== ===== Proposal =====
- 
-**pow() as language construct** 
- 
-No changes to existing code, safe for one: 
- 
-<code php> 
-var_dump(pow([], 2)); // int(0) instead of float(0) 
-</code> 
- 
-**Power operator** 
  
 A short working example: A short working example:
Line 64: Line 55:
  
 The operator precedence is: The operator precedence is:
-  * higher than the bitwise not (~), +  * higher than the bitwise not (~) and unary minus
-  * lower than array dereferencing+  * lower than array dereferencing.
-  * lower than unary minus.+
  
 Examples: Examples:
Line 72: Line 62:
 <code php> <code php>
 echo 2 ** 3 ** 2; // 512 (not 64) echo 2 ** 3 ** 2; // 512 (not 64)
-echo -3 ** 2; // 9 (not -9)+echo -3 ** 2; // -9 (not 9) 
 +echo 1 - 3 ** 2; // -8
 echo ~3 ** 2; // -10 (not 16) echo ~3 ** 2; // -10 (not 16)
 </code> </code>
 +
 +===== Changelog =====
 +
 +21-Dec-2013: 
 +  * Removed turning pow() into a language construct from the proposal, due to BC breaks.
 +  * Closed vote and moved RFC back to Discussion status.
 +  * Reverted commit [[https://github.com/datibbaw/php-src/commit/f60b98cf7a8371233d800a6faa286ddba4432d02|f60b98c]].
 +
 +22-Dec-2013:
 +  * Moved RFC back to Voting status
  
 ===== Discussion ===== ===== Discussion =====
 +
 +> Should ''-2 <nowiki>**</nowiki> 3'' evaluate to ''9'' instead of ''-9''?
 +
 +According to the following resources, the scale tips more towards having the exponent precede the unary minus:
 +
 +  * http://mathforum.org/library/drmath/view/53194.html
 +  * http://math.stackexchange.com/questions/491933/exponent-rules-with-negative-numbers
 +  * http://math.stackexchange.com/questions/68833/what-does-22-evaluate-to/68834#68834
 +
 +**Similar languages**
 +
 +  * Ada
 +  * D
 +  * F#
 +  * Fortran
 +  * Freemat
 +  * Haskell
 +  * Mathematica / Matlab / Scilab
 +  * Octave
 +  * Perl
 +  * Python
 +  * R
 +  * Ruby
 +  * Sage
 +  * VB / Basic
 +
 +**Dissimilar languages**
 +
 +  * Bash
 +  * Cobol
 +  * ColdFusion
 +  * Excel
 +  * Tcl
 +
 +----
  
 > Should ''2 <nowiki>**</nowiki> 3 <nowiki>**</nowiki> 2'' yield ''64'' (left associative), ''512'' (right associative) or throw an error (non associative)? > Should ''2 <nowiki>**</nowiki> 3 <nowiki>**</nowiki> 2'' yield ''64'' (left associative), ''512'' (right associative) or throw an error (non associative)?
Line 88: Line 124:
   * Octave   * Octave
   * Matlab   * Matlab
-  * D 
   * ColdFusion   * ColdFusion
  
 ** Languages with right associative exponential operator** ** Languages with right associative exponential operator**
  
 +  * D
   * Haskell   * Haskell
   * R   * R
Line 129: Line 165:
 External extensions such as [[http://pecl.php.net/package/vld|vld]] or [[https://github.com/krakjoe/phpdbg|phpdbg]] would have to be updated, but I'm not aware of any core extensions that would otherwise be affected. External extensions such as [[http://pecl.php.net/package/vld|vld]] or [[https://github.com/krakjoe/phpdbg|phpdbg]] would have to be updated, but I'm not aware of any core extensions that would otherwise be affected.
  
-===== Open Issues ===== +===== Vote =====
- +
-None. +
- +
- +
-===== Proposed Voting Choices =====+
  
 Voting will be based on the following: Voting will be based on the following:
  
-  * Turn ''pow()'' into a language construct, 
   * Add the power operators ''<nowiki>**</nowiki>'' and ''<nowiki>**</nowiki>='',   * Add the power operators ''<nowiki>**</nowiki>'' and ''<nowiki>**</nowiki>='',
   * Add ''ZEND_POW'' and ''ZEND_ASSIGN_POW'' opcodes.   * Add ''ZEND_POW'' and ''ZEND_ASSIGN_POW'' opcodes.
 +
 +Changes from 0.1:
 +
 +  * A option is added to vote for a non-associative ''<nowiki>**</nowiki>'' operator. See also: Discussion. This counts as an inclusion vote.
 +
 +A two third majority is required for acceptance.
  
 ---- ----
  
-<doodle title="Should PHP get a power operator in PHP 5.6?" auth="datibbaw" voteType="single" closed="false"> +<doodle title="Should PHP get a power operator in 5.6?" auth="datibbaw" voteType="single" closed="true"> 
-   * Yes+   * Yes, right associative 
 +   * Yes, non associative
    * No    * No
 </doodle> </doodle>
- 
-Voting ends on 31st of December. 
  
 ---- ----
 +
 +Voting ends on 5th of January 2014.
 +
 +
 +
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
 Power operator PR: https://github.com/php/php-src/pull/543 Power operator PR: https://github.com/php/php-src/pull/543
  
 +PHP 5.6 Commits: [[http://git.php.net/?p=php-src.git;a=commit;h=aff56f3c4539869910cf2778cf0ece2d8c2dd671|1]] [[http://git.php.net/?p=php-src.git;a=commit;h=363ff60475d93716722034b8f7a2486229bf4cfb|2]]
rfc/pow-operator.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1