rfc:pow-operator

This is an old revision of the document!


PHP RFC: Power Operator

  • Version: 0.2
  • Date: 2013-11-23
  • Author: Tjerk Meesters, datibbaw@php.net
  • Status: Under Discussion
  • Revision (0.1 → 0.2): 2013-12-19

Introduction

This proposal is two-fold:

  1. Introduce an exponential (right associative) operator **.
    • Avoids a function call.
    • Support for GMP overloading.
    • Easier to read and shorter to write.
    • Can be found in other languages.
  2. Introduce an exponential assignment operator **=

Proposal

A short working example:

echo 2 ** 3; // 8

It supports GMP overloading:

$base = gmp_init(2);
$exponent = 3;
var_dump($base ** $exponent);
 
// output
object(GMP)#3 (1) {
  ["num"]=>
  string(1) "8"
}

Example of exponent assignment:

$x = 2;
$x **= 3;
echo $x; // 8

Important

The proposed associativity is right, just like how power towers work.

The operator precedence is:

  • higher than the bitwise not (~) and unary minus,
  • lower than array dereferencing.

Examples:

echo 2 ** 3 ** 2; // 512 (not 64)
echo -3 ** 2; // -9 (not 9)
echo 1 - 3 ** 2; // -8
echo ~3 ** 2; // -10 (not 16)

Changelog

21-Dec-2013: Reverted the change that turns pow() into a language construct due to BC breaks. 21-Dec-2013: Closed vote and moved RFC back to Discussion status

Discussion

Should -2 ** 3 evaluate to 9 instead of -9?

According to the following resources, the scale tips more towards having the exponent precede the unary minus:

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 ** 3 ** 2 yield 64 (left associative), 512 (right associative) or throw an error (non associative)?

The exponent operator evaluation order should be based on Tetration and therefore be right associative.

Languages with left associative exponential operator

  • VB (not by choice imho)
  • Basic
  • Octave
  • Matlab
  • ColdFusion

Languages with right associative exponential operator

  • D
  • Haskell
  • R
  • F#
  • Ruby
  • Perl
  • Python
  • Mathematica
  • Freemat
  • Scilab
  • Tcl (changed from left associative!)
  • Cobol
  • Fortran
  • Sage
  • Bash

Languages with non associative exponential operator

  • Ada

Sources

Proposed PHP Version(s)

PHP 5.6

Impact to Existing Extensions

The opcode ZEND_POW <165> and ZEND_ASSIGN_POW <166> is added.

External extensions such as vld or phpdbg would have to be updated, but I'm not aware of any core extensions that would otherwise be affected.

Open Issues

An issue was brought up that due to pow() itself becoming a language construct, it would break existing code that uses “public function pow()”.

Vote

Voting will be based on the following:

  • Turn pow() into a language construct,
  • Add the power operators ** and **=,
  • Add ZEND_POW and ZEND_ASSIGN_POW opcodes.

Changes from 0.1:

  • A option is added to vote for a non-associative ** operator. See also: Discussion. This counts as an inclusion vote.

A two third majority is required for acceptance.


Should PHP get a power operator in 5.6?
Real name Yes, right associative Yes, non associative No
aharvey (aharvey)   
ajf (ajf)   
auroraeosrose (auroraeosrose)   
bmajdak (bmajdak)   
bwoebi (bwoebi)   
chobieeee (chobieeee)   
cyberspice (cyberspice)   
datibbaw (datibbaw)   
daverandom (daverandom)   
davey (davey)   
derick (derick)   
dm (dm)   
googleguy (googleguy)   
guilhermeblanco (guilhermeblanco)   
kassner (kassner)   
krakjoe (krakjoe)   
kriscraig (kriscraig)   
levim (levim)   
lstrojny (lstrojny)   
mbeccati (mbeccati)   
mfonda (mfonda)   
nikic (nikic)   
peehaa (peehaa)   
philstu (philstu)   
pollita (pollita)   
rasmus (rasmus)   
rdlowrey (rdlowrey)   
salathe (salathe)   
seld (seld)   
stas (stas)   
uw (uw)   
willfitch (willfitch)   
Final result: 22 1 9
This poll has been closed.

Voting ends on 5th of January 2014.

Patches and Tests

rfc/pow-operator.1387604755.txt.gz · Last modified: 2017/09/22 13:28 (external edit)