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: 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.) |
|||
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. |
|||
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! |
|||
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: To confound (human) operators. — Ian Abbott |
|||
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: ⋮ 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.) |
|||
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- |
|||
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.) 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]). |
|||
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- |
|||
05-21-2024, 08:52 PM
Post: #29
|
|||
|
|||
RE: Why does -2^2 compute to -4 on my HP Prime?
(03-17-2024 08:33 AM)Joe Horn Wrote:(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]). Ah! Great point. This it seems is another point of departure — I wouldn’t simplify 0%%%2 to 0 any more than I would simplify 0_(m) to 0. |
|||
07-18-2024, 05:18 PM
Post: #30
|
|||
|
|||
RE: Why does -2^2 compute to -4 on my HP Prime?
Hi
Not only the Prime, also the 50g. Since I use RPN, I hadn't noticed that detail on the 50g. regards (02-08-2024 04:56 AM)KenL Wrote: If I ask my prime to give me the answer to -2 squared it says the answer is -4 ? |
|||
07-21-2024, 08:25 PM
Post: #31
|
|||
|
|||
RE: Why does -2^2 compute to -4 on my HP Prime?
The relative precedence of the exponentiation operator and unary negation (and unary plus) operator does vary amongst programming languages, so there is no consensus in programming languages for the result of -2^2 (or -2 ** 2) should be.
For example, Fortran, ALGOL 60, and JavaScript give unary operators higher precedence than the exponentiation operator, whereas Python and Julia give the exponentiation operator higher precedence than the unary operators. There are also differences in associativity of the exponentiation operator. In most languages that have one (and have a concept of operator precedence), the exponentiation operator associates right-to-left, so a^b^c means a^(b^c). One exception is ALGOL 60 where the exponentiation operator associates left-to-right like the multiplicative and additive operators, so a^b^c means (a^b)^c. — Ian Abbott |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)