HP Forums

Full Version: Why does -2^2 compute to -4 on my HP Prime?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
If I ask my prime to give me the answer to -2 squared it says the answer is -4 ?
Why is this happening?

procedure
======
press +/-
press 2
press x^2
press Enter
result shows as -4

Both Home & CAS modes provide same result
Why?
You may be interested in this thread.
Hth
Arno
Awesome! Thanks for the explanation

OK ..note to self..

(-2)^2=4 properly. Whew! Now I know Lol!
What happens if you press +/- *after* entering 2?
(02-08-2024 02:22 PM)KeithB Wrote: [ -> ]What happens if you press +/- *after* entering 2?

Same issue.

Apparently HP considers the +/- key as an operation rather than a declaration
I think this is nuts.
(02-08-2024 03:28 PM)KenL Wrote: [ -> ]Apparently HP considers the +/- key as an operation rather than a declaration
I think this is nuts.

HP does the correct way to parse a number. Negative sign is not part of number.

Do it the other way, we get into weird parsing situation. (note: this is a Prolog bug)
(12-23-2020 03:14 AM)robve Wrote: [ -> ]Parsers should not consider the - to be part of the number to avoid this flaw.

So it is ridiculously funny to see the following (note the spacing between - and 2):

?- X is -2^2.
X = 4.

?- X is - 2^2.
X = -4.
(02-08-2024 04:12 PM)Albert Chan Wrote: [ -> ]Negative sign is not part of number.

I had no idea.
Do I need to add parenthesis around my negative numbers from now on?

-2^2 = wrong result
(-2)^2 = always correct result
(02-08-2024 05:08 PM)KenL Wrote: [ -> ]I had no idea.
Do I need to add parenthesis around my negative numbers from now on?

-2^2 = wrong result
(-2)^2 = always correct result

With paper and pencil :
-2^2 = -4 is a correct answer from my point of view…
1) What is the result of the expression 0-2^2?
Answer: -4 (nobody doubts that).

Now we will remove the zero, because, as you know, it is completely useless there:
2) What is the result of the expression -2^2?
Expecting a positive result is somewhat contrary to point 1).
However, and I seem to be the one that always points this out:
Prime has *two* minus operators.
There is unary negation which should yield (-2)^2
and the minus operator which should yield -(2^2)

I don't have my Prime with me, so I can't tell which is being used in this case. If you look in the algebraic view, the unary minus is tinier and shifted up a bit, while the other is larger and justified to the middle of the number.
In general, this is a problem with our common notation. "-" has at least three different meanings in math:
  1. Subtraction, as in 4 - 1
  2. Unary negation, as in -X
  3. Part of a number, as in -273.3

None of this confusion would exist if we had separate symbols for all three operations. In the meantime, I think it's useful to recognize the cause.
(02-08-2024 03:28 PM)KenL Wrote: [ -> ]
(02-08-2024 02:22 PM)KeithB Wrote: [ -> ]What happens if you press +/- *after* entering 2?

Same issue.

Apparently HP considers the +/- key as an operation rather than a declaration
I think this is nuts.

Wolfram Alpha agrees with HP: https://www.wolframalpha.com/input?i=-2%5E2
(02-08-2024 05:59 PM)KeithB Wrote: [ -> ]However, and I seem to be the one that always points this out:
Prime has *two* minus operators.

Are they (− vs -) really the same thing?

If Yes, why 2 minus operators?
If No, what is the difference?
I must concede.

The graph of -x^2 is indeed upside down Lol!
(02-08-2024 09:12 PM)Albert Chan Wrote: [ -> ]
(02-08-2024 05:59 PM)KeithB Wrote: [ -> ]However, and I seem to be the one that always points this out:
Prime has *two* minus operators.

Are they (− vs -) really the same thing?

If Yes, why 2 minus operators?
If No, what is the difference?

No, I don't have my prime here, but this is in the catalog section of the manual:
[attachment=13257]

[attachment=13258]
And I seem to have been bit by it here:
https://www.hpmuseum.org/forum/thread-12460.html
The order of precedence used by algebraic scientific calculators has changed over the years. Early Texas instruments (TI) models like the SR-50 (Sum of Products logic) and TI-52/TI-30/TI-57/TI-58/TI-59, etc (AOS logic) would square the number in the display immediately after pressing the X^2 key (like RPN calculators). This would treat the sign of the number as an integral part of the number so -2^2 would equal 4. This is identical to the way HP RPN models calculated this type of problem.

For Texas Instruments, the change in logic occured around 1990 with the TI-81 (EOS logic). Theses and later models would treat the - as a negation operator with a lower precedence than the ^2 function so -2^2 would equal -4 on these models. This is described in more detail on the Datamath site:
http://www.datamath.org/Sci/WEDGE/SR-52_...ing_System:
The Prime is inconsistent with how it applies precedence for - and ^. Or maybe better said, when it imposes parentheses.

- 2 ^ 2 => -4
- 2 enter ^ 2 => 4
- 2 enter (copy from history) ^ 2 => -4

ANS seems to imply parentheses.

I've learned to be generous with the use of explicit parentheses when working on the Prime. It really isn't worse than other algebraic or textbook calculators, but I can do much more complex things with the Prime, so I get into subtler problems.

Having a truly usable RPN mode would be nice.
(02-08-2024 05:53 PM)chromos Wrote: [ -> ]1) What is the result of the expression 0-2^2?
Answer: -4 (nobody doubts that).

Now we will remove the zero, because, as you know, it is completely useless there ...

If we add MOD operator to the mix, zero make a difference.
Unary minus has higher precedence than binary minus, with MOD in-between.

>>> 0-10%7, -10%7
(-3, 4)

lua> 0-10%7, -10%7
-3      4

(02-09-2024 01:34 AM)john gustaf stebbins Wrote: [ -> ]I've learned to be generous with the use of explicit parentheses when working on the Prime.

That's a good advice! For clarity, -10%7 should really be written as (-10)%7

Practical C Programming preface Wrote:This book stresses simple, practical rules.
For example, there are 15 operator precedence rules in C. These can be simplified into two rules:

1. Multiply and divide come before add and subtract.
2. Put parentheses around everything else.
As the "Elements of Programming Style" puts it:
"Parenthesize to avoid ambiguity"

But I am afraid I was wrong - on teh internet!

powers have higher precedence than unary negation:
[attachment=13262]
Pages: 1 2
Reference URL's