Req #71467 [Wfx->Nab]: -2 **2 = -4

[email protected]
Newsgroups php.standards
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=71467&edit=1

 ID:                 71467
 Updated by:         [email protected]
 Reported by:        public at grik dot net
 Summary:            -2 **2 = -4
-Status:             Wont fix
+Status:             Not a bug
 Type:               Feature/Change Request
 Package:            PHP Language Specification
 Operating System:   unrelated
 PHP Version:        7.0.3RC1
 Block user comment: N
 Private report:     N

 New Comment:

Hello,

According to the page http://php.net/manual/en/language.operators.precedence.php

The ** operator has higher precedence than the - operator.

The code you posted is equivalent to:

echo -(2 ** 2);

You can get the result you probably desired by doing:

echo (-2) ** 2;


Parentheses are your friends.


Previous Comments:
------------------------------------------------------------------------
[2016-01-27 23:38:11] [email protected]

** has higher precedence than -
http://php.net/manual/en/language.operators.precedence.php

-2 ** 2 => -(2 ** 2)

The programming world is split on whether power/exponentiation ranks higher or lower than negation - PHP chose the higher side, which also includes Python and Perl.

------------------------------------------------------------------------
[2016-01-27 23:27:18] public at grik dot net

Description:
------------
In 5.6 the operator ** appeared, but its behavior is very unexpected.

echo -2 ** 2; gives -4


Test script:
---------------
echo -2 ** 2;

Expected result:
----------------
4

Actual result:
--------------
-4


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=71467&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.