Chronology Current Month Current Thread Current Date
[Year List] [Month List (current year)] [Date Index] [Thread Index] [Thread Prev] [Thread Next] [Date Prev] [Date Next]

Re: [Phys-L] math notation puzzles



In the context of:
-3​^​2
and
-3**2

On 2/6/19 8:58 AM, Jeffrey Schnick wrote:

For the first two, my best bet would be -9. [...] a writer of an interpreter
or compiler can do it any way they want; there are probably some for
which the result is 9. There might be some that introduce a rounding
error. My TI83+ calculator gives -9. Microsoft Excel gives 9. I'm
not sure at all of my answer. When in doubt, I use parentheses, or,
in some cases, I experiment to find out how the device/language I am
working with handles the expression.

That's the right answer. The answer is either 9 or -9, depending.
Microsoft Visual Basic does it one way, while Microsoft VBscript
does it the other way.

Exponentiation generally takes precedence over negation.

I wouldn't have said that. As a matter of opinion, I agree that
exponentiation *should* take precedence, but I see no evidence
that it "generally" does. My answer to the "how do I know"
question is:
https://rosettacode.org/wiki/Operator_precedence

Historically I think exponentiation had precedence until early
versions of the BASIC language came along. They did it the other
way -- perhaps because it makes the parser simpler -- and the
diseases spread from there.

One reason for asking the question is to give students practice
in dealing with ill-posed questions. Most of the problems that
land on my desk are ill-posed. (If they had been well posed,
somebody would have solved them already.) Typical end-of-chapter
exercises are well-posed, which gives a highly misleading impression.

Another reason is to emphasize that these exponential expressions
are bug bait. They are not portable. You can't send such an
expression to somebody and assume he can evaluate it the way you
want. So it pays to stick in the parentheses, unless you are
absolutely sure nobody is going to port the expression from one
environment to another.

Another trick is to write 0-3**2, which works because AFAIK
every computer language worthy of the name gives exponentiation
precedence over the binary "-" subtraction operator.

Javascript recently added an exponentiation operator to the
language specification. To their credit, they made it illegal
to combine unary negation with exponentiation. The parser
makes a special check, just to catch this problem. So you have
to write 0-3**2, or use parentheses. This guarantees that no
javascript users will get fooled.