Post Reply 
Why does -2^2 compute to -4 on my HP Prime?
02-09-2024, 01:30 PM
Post: #19
RE: Why does -2^2 compute to -4 on my HP Prime?
(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.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Why does -2^2 compute to -4 on my HP Prime? - Albert Chan - 02-09-2024 01:30 PM



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