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

From: Date: Wed, 27 Jan 2016 23:40:05 +0000
Subject: Req #71467 [Wfx->Nab]: -2 **2 = -4
References: 1  Groups: php.standards 
Request: Send a blank email to standards-+get-1017@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71467&edit=1 ID: 71467 Updated by: danack@php.net 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] requinix@php.net ** 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

« previous php.standards (#1017) next »