[Tutor] a puzzle about -3**2 vs (-3)**2

Válas Péter turtle at 64.hu
Fri Jul 31 21:22:33 CEST 2015


2015-07-31 20:51 GMT+02:00 Alan Gauld <alan.gauld at btinternet.com>:

>
> (For example Forth was designed to fit
> into the very small amount of memory left over on an astronomical telescope
> control system, so is very, very terse, and uses many "illogical" code
> layouts.
> Everything was sacrificed to save space.)
>

Thank you for this interesting story, I never knew this. You mentioned such
interesting languages as Lisp and Forth. While you are right that operator
precedence may vary from language to language (just a few weeks ago PHP
schocked me with the different precedence of and/or and "C-style" &&/|| and
it took me quite a time to find the error), I think we may say that all the
mainstream languages of present times leave the precedence of ARITHMETICAL
operators intact among each other. And this follows the rules of elementary
math.

While thinking that minus is glued to the following number has its own
logic and is not hard to understand this logic, it's not the rule of
mathematics. That's why math has its worldwide accepted rules, and that's
why these are taught in schools whereever we go on the globe. These rules
are also based on logics, of course. And because prorgramming is a branch
of applied mathematics, it is really-really worth to keep the rules of math
-- I think where they are broken, it is an exception and not the normal
workflow.

While all we should read the famous manual concerning the precedence when
we learn a new language, we may trust that * will always preceed +, ** will
preceed any other arithmetical operator, and AND will preceed OR -- unless
we deal  with some special/obscure/historical language. Python is not an
exception.


More information about the Tutor mailing list