Re: Re: power operator (again)
| From: | Rowan Collins | Date: | Thu, 19 Dec 2013 22:42:43 +0000 |
| Subject: | Re: Re: power operator (again) | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-70772@lists.php.net to get a copy of this message | ||
On 19/12/2013 22:16, Daniel Macedo wrote:
I understood Rasmus's point to be rather simpler than that: PHP code will rarely contain the actual hard-coded sum "-3 ** 2", it will much more likely look something like this: $var = some_function(); $square_of_var = $var ** 2; Now if some_function() returns -3 (the integer), $square_of_var will end up with a value of 9. No need to worry about associativity, no type casting, just application of maths, and the result is as expected. Now, you could imagine a more complex example like this: $cube_of_inverted_var = -$var ** 3; But that's hard enough to read that I'd probably introduce brackets anyway, if not avoid the unary operator altogether: $cube_of_inverted_var = (-1 * $var) ** 3; In my head, the reason "-3 ** 2" looks like (-3) ** 2 is that I tend to think of "-3" as a single literal value, not the application of the unary operator "-" to the literal value "3". Once it becomes "-$var ** 2", that impression evaporates, and I would, personally, avoid assuming precedence one way or another, and add some brackets. Regards, -- Rowan Collins [IMSoP]No, the average PHP developer is going to do $var ** 2 and it willbehave exactly as they expect with negative values. Yes, in the very I didn't quite understand if you're making the case for assuming a type casting of $var and keep with the expected math or if operator precedence would mean the result could be negative in some cases.