On 04/11/2022 21:08, J.G.Harston wrote (cross-posted from Discussion Group):
In C bitwise operators are higher than comparison. In Python,
comparison operators are higher than bitwise. Which is "correct"?
Both and neither. The only correct answer is: read the documentation.
Yes, quite right: when it comes to bitwise operators and comparison operators there is no agreed standard. But that's not what we are talking about, which is unary minus and exponentiation. In that case there is an acknowledged 'correct' priority, exponentiation is higher than unary minus. It's because BBC BASIC is clearly wrong that I raised it, if it was just an arbitrary choice it would be of little interest.
BBC BASIC has an unusual feature that may have contributed to the anomaly: it's permissible to omit the parentheses from many of the built-in functions. For example it's legitimate to write
SINRAD30 rather than
SIN(RAD(30)) which would be mandated in most BASICs. This has a particular consequence which is highly relevant: the function's parameter is a numeric
factor rather than an
expression.
Specifically, the expression evaluator is not called in order to get the parameter value:
RAD30+20 does not mean
RAD(30+20) it means
RAD(30)+20. But we want unary minus to be accepted despite it being a factor, so that
SINRAD-30 works; a direct consequence of this is that unary plus and minus become the highest priority operators!
I wonder which came first: did the desire to be able to omit parentheses lead to unary minus incorrectly having a higher priority than exponentiation, or did the incorrect priority come first and a realisation that this allowed the parentheses to be omitted come later. We will probably never know.