# RFC: Exponential operator

- Status: Implemented (PHP 5.6)
- Authors: Tjerk Meesters, datibbaw@php.net
- Date: 2013-11-23
- Version: 0.3
- Original: https://wiki.php.net/rfc/pow-operator
- Page: https://externals.io/rfc/pow-operator
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Should PHP get a power operator in 5.6? (closed)

Yes, right associative: 22 · Yes, non associative: 1 · No: 9

Voters: aharvey (No), ajf (Yes, right associative), auroraeosrose (Yes, right associative), bmajdak (No), bwoebi (Yes, non associative), chobieeee (Yes, right associative), cyberspice (Yes, right associative), datibbaw (Yes, right associative), daverandom (Yes, right associative), davey (Yes, right associative), derick (Yes, right associative), dm (Yes, right associative), googleguy (Yes, right associative), guilhermeblanco (No), kassner (Yes, right associative), krakjoe (Yes, right associative), kriscraig (No), levim (Yes, right associative), lstrojny (Yes, right associative), mbeccati (Yes, right associative), mfonda (No), nikic (No), peehaa (No), philstu (Yes, right associative), pollita (Yes, right associative), rasmus (Yes, right associative), rdlowrey (Yes, right associative), salathe (Yes, right associative), seld (Yes, right associative), stas (No), uw (No), willfitch (Yes, right associative)

## Discussions on the mailing list

- [rfc: pow-operator](https://externals.io/message/70325.md) — started on 2013-11-23, 33 messages
- [\[vote\] pow-operator](https://externals.io/message/70727.md) — started on 2013-12-18, 15 messages
- [\[vote\] power operator (again)](https://externals.io/message/70744.md) — started on 2013-12-19, 5 messages

---

## Introduction

This proposal is two-fold:

1. Introduce an exponential (right associative) operator `**`. 
    - Avoids a function call.
    - Support for [GMP overloading](https://externals.io/rfc/operator_overloading_gmp).
    - Easier to read and shorter to write.
    - Can be found in other languages.
2. Introduce an exponential assignment operator `**=`

## Proposal

A short working example:

```php
echo 2 ** 3; // 8
```

It supports [GMP overloading](https://externals.io/rfc/operator_overloading_gmp):

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

Example of exponent assignment:

```php
$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:

```php
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:

- 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 [f60b98c](https://github.com/datibbaw/php-src/commit/f60b98cf7a8371233d800a6faa286ddba4432d02).

22-Dec-2013:

- Moved RFC back to Voting status

## Discussion

> Should `-3 ** 2` 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 ** 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](http://en.wikipedia.org/wiki/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**

- <https://plus.google.com/u/0/104277466162910953762/posts/e3jCt51VfmD>
- <http://perldoc.perl.org/perlop.html>
- <http://www.tcl.tk/cgi-bin/tct/tip/274.html>
- <http://book.realworldhaskell.org/read/getting-started.html>
- <http://www.nku.edu/~foxr/CSC407/NOTES/ch7.ppt>‎ (second slide)

## 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](http://pecl.php.net/package/vld) or [phpdbg](https://github.com/krakjoe/phpdbg) would have to be updated, but I'm not aware of any core extensions that would otherwise be affected.

## Vote

Voting will be based on the following:

- 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.

---

**Vote: Should PHP get a power operator in 5.6?** — Yes, right associative: 22 · Yes, non associative: 1 · No: 9 (closed)

---

Voting ends on 5th of January 2014.

## Patches and Tests

Power operator PR: <https://github.com/php/php-src/pull/543>

PHP 5.6 Commits: [1](http://git.php.net/?p=php-src.git;a=commit;h=aff56f3c4539869910cf2778cf0ece2d8c2dd671) [2](http://git.php.net/?p=php-src.git;a=commit;h=363ff60475d93716722034b8f7a2486229bf4cfb)
