Post Reply 
Why does -2^2 compute to -4 on my HP Prime?
02-09-2024, 04:18 PM
Post: #21
RE: Why does -2^2 compute to -4 on my HP Prime?
(02-09-2024 03:23 PM)KeithB Wrote:  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:

Two items in that list of precedence don't properly describe the current behaviour of the HP Prime. I argued against those two during early development (and filed multiple bug reports etc.); it seems that a correct description of implemented precedence didn't quite make it out to the documentation writers in time.

(I did review the documentation involving the apps I implemented, but not the documentation in general. There is a third item in that list which I think should be a bit different, but... priorities... then & now. Oh, and none of these three I'm thinking of are negation — and there certainly are arguments towards different approaches that could be made there.)
Find all posts by this user
Quote this message in a reply
02-09-2024, 04:58 PM
Post: #22
RE: Why does -2^2 compute to -4 on my HP Prime?
I guess what confuses me is that - and I am talking unary negation here - that these should be equivalent:

-2^2
and

x:= -2
x^2

But what I am seeing is that they aren't.
Find all posts by this user
Quote this message in a reply
02-09-2024, 05:06 PM
Post: #23
RE: Why does -2^2 compute to -4 on my HP Prime?
The HP71 has pretty much the same precedence (including the unary minus! I have no idea how to enter that from the keyboard.)

When I do the A = -2: A^2 in calc mode, when I enter A it immediately puts parens around it. It does *not* put parens if I put A = 2!
Find all posts by this user
Quote this message in a reply
02-10-2024, 10:15 AM (This post was last modified: 02-10-2024 10:16 AM by ijabbott.)
Post: #24
RE: Why does -2^2 compute to -4 on my HP Prime?
(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?

To confound (human) operators. Smile

— Ian Abbott
Find all posts by this user
Quote this message in a reply
02-11-2024, 11:17 PM
Post: #25
RE: Why does -2^2 compute to -4 on my HP Prime?
(02-09-2024 01:30 PM)Albert Chan Wrote:  
If we add MOD operator to the mix, zero make a difference.
Unary minus has higher precedence than binary minus, with MOD in-between.

It’s good for me to have read back a bit further in time. The “third item in that list” I was alluding to was MOD — how I personally view MOD depends on whether math mode is on in my brain. If it is, I like to see MOD as a declaration that we are doing modular arithmetic, so it would have much lower precedence — below equality tests, even (“(mod x)” would appear at the end of each line of mathematics). When I’m in programming mode, I can certainly appreciate % having the same precedence as /. Perhaps a hybrid approach would be to treat MOD as a declaration of arithmetic type, similar to how units are handled on the HP Prime. (Once applied, the modulus would carry through to results.)
Find all posts by this user
Quote this message in a reply
02-12-2024, 01:57 PM (This post was last modified: 02-12-2024 02:22 PM by Joe Horn.)
Post: #26
RE: Why does -2^2 compute to -4 on my HP Prime?
(02-11-2024 11:17 PM)jte Wrote:  Perhaps a hybrid approach would be to treat MOD as a declaration of arithmetic type, similar to how units are handled on the HP Prime. (Once applied, the modulus would carry through to results.)

Prime already does that, using %% syntax in CAS view. Example in CAS: (11111%%777)^22222 --> 100%%777. This is Prime's way of saying "11111^22222 ≡ 100 (mod 777)" as can be verified by performing powmod(11111,22222,777). Once %% notation is included in a calculation, the modulus carries through the calculation.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
03-16-2024, 11:57 PM
Post: #27
RE: Why does -2^2 compute to -4 on my HP Prime?
(02-12-2024 01:57 PM)Joe Horn Wrote:  
(02-11-2024 11:17 PM)jte Wrote:  Perhaps a hybrid approach would be to treat MOD as a declaration of arithmetic type, similar to how units are handled on the HP Prime. (Once applied, the modulus would carry through to results.)

Prime already does that, using %% syntax in CAS view. Example in CAS: (11111%%777)^22222 --> 100%%777. This is Prime's way of saying "11111^22222 ≡ 100 (mod 777)" as can be verified by performing powmod(11111,22222,777). Once %% notation is included in a calculation, the modulus carries through the calculation.

Yes, although I was thinking (at least initially) of something a little different once you get into the details — my inclination would be to preserve the commutativity of + and x, for example. (Rather than prefer a modulus on the left over one on the right, combine them in a symmetric manner — for example, by taking the GCD [when left and right both have a modulus] and perhaps annotating discarded factors.) I'd also be inclined to preserve the modulus when a zero is produced (if we were to use %%% for this alternative operation, I mean that (1%%%2) + 1 would evaluate to 0%%%2, unlike how (1%%2) + 1 evaluates to 0 [not 0%%2]).
Find all posts by this user
Quote this message in a reply
03-17-2024, 08:33 AM
Post: #28
RE: Why does -2^2 compute to -4 on my HP Prime?
(03-16-2024 11:57 PM)jte Wrote:  I'd also be inclined to preserve the modulus when a zero is produced (if we were to use %%% for this alternative operation, I mean that (1%%%2) + 1 would evaluate to 0%%%2, unlike how (1%%2) + 1 evaluates to 0 [not 0%%2]).

0%%2 is returned, as desired, when the CAS setting for Simplify is set to None or Minimum (default). 0 is returned only when the Simplify setting is Maximum.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)