Post Reply 
HP-42S - How do I round a number?
03-21-2021, 08:15 PM
Post: #1
HP-42S - How do I round a number?
I see the RND command, but I am not understanding how it works because it appears to do nothing.

I want to round an answer in a program to two decimals places. How is this accomplished?

TIA

HP48GX user learning the HP42s and DM42.
Find all posts by this user
Quote this message in a reply
03-21-2021, 08:27 PM
Post: #2
RE: HP-42S - How do I round a number?
From the manual :
Quote:RND: Rounds the number in the X-register using the current display format.

So to round to two decimal places do: FIX 02 RND.
Find all posts by this user
Quote this message in a reply
03-21-2021, 08:30 PM
Post: #3
RE: HP-42S - How do I round a number?
Thank you! Coming from the 48G, I can see a lot of the 42 influence on the 48G. Both are great calculators.

I am really starting to love the 42.

HP48GX user learning the HP42s and DM42.
Find all posts by this user
Quote this message in a reply
03-21-2021, 08:33 PM
Post: #4
RE: HP-42S - How do I round a number?
Type in
Code:
6
1/x
SHOW
RND
SHOW
You'll get the idea
Find all posts by this user
Quote this message in a reply
03-21-2021, 08:59 PM
Post: #5
RE: HP-42S - How do I round a number?
If you do not want to change the display settings, do (on a 42S)
Code:
2E9
+
LASTX
-
For Free42 and DM42, use 2E31

Cheers, Werner
Find all posts by this user
Quote this message in a reply
03-21-2021, 09:17 PM (This post was last modified: 03-21-2021 09:18 PM by DM48.)
Post: #6
RE: HP-42S - How do I round a number?
Thank you everyone.

Werner, Perfect solution. Thank you!!

HP48GX user learning the HP42s and DM42.
Find all posts by this user
Quote this message in a reply
03-21-2021, 10:51 PM
Post: #7
RE: HP-42S - How do I round a number?
(03-21-2021 08:59 PM)Werner Wrote:  If you do not want to change the display settings, do (on a 42S)
Code:
2E9
+
LASTX
-
For Free42 and DM42, use 2E31

Cheers, Werner

This is what I typically use - it's 3 bytes shorter:

ENTER
FP
+
IP

Can't remember where I found that algorithm. It might have been 65 Notes.
Visit this user's website Find all posts by this user
Quote this message in a reply
03-21-2021, 10:59 PM
Post: #8
RE: HP-42S - How do I round a number?
Note that RND rounds the number in X to what is displayed, in whatever display mode happens to be in effect, and that makes it rather flexible. Used with FIX, you can use it to round to a certain number of digits behind the decimal, so FIX 02 RND can be used to round an amount of money to dollars and cents, while SCI 03 RND will round the number to three significant digits, regardless of its magnitude, which is useful in scientific and engineering applications.
Visit this user's website Find all posts by this user
Quote this message in a reply
03-22-2021, 04:19 AM
Post: #9
RE: HP-42S - How do I round a number?
(03-21-2021 10:51 PM)Dave Britten Wrote:  ENTER
FP
+
IP

Yes, but that does a FIX 0 RND.
Moreover, it doesn’t work for
9 1/X LASTX * (0.999..)
returning 2 instead of 1.

Cheers, Werner
Find all posts by this user
Quote this message in a reply
03-22-2021, 05:03 AM (This post was last modified: 03-22-2021 07:47 AM by Werner.)
Post: #10
RE: HP-42S - How do I round a number?
In the same vein, SCI 03 rounding: (which, incidentally, also fails for 0.9999.. ;-)
Code:
1E8
RCLx ST Y
+
LASTX
-
Free42, use 1E30

Cheers, Werner
Find all posts by this user
Quote this message in a reply
03-22-2021, 11:39 AM
Post: #11
RE: HP-42S - How do I round a number?
(03-22-2021 04:19 AM)Werner Wrote:  
(03-21-2021 10:51 PM)Dave Britten Wrote:  ENTER
FP
+
IP

Yes, but that does a FIX 0 RND.
Moreover, it doesn’t work for
9 1/X LASTX * (0.999..)
returning 2 instead of 1.

Cheers, Werner

Oh right, missed the point about rounding to two decimal places. Smile You can, of course, adjust the magnitude of the number before and after rounding, though that adds additional steps/bytes.
Visit this user's website Find all posts by this user
Quote this message in a reply
03-22-2021, 12:19 PM (This post was last modified: 03-22-2021 12:22 PM by Thomas Okken.)
Post: #12
RE: HP-42S - How do I round a number?
Since the point of the exercise is to perform rounding without changing the display mode, maybe using RCLFLAG and STOFLAG is more convenient:

RCLFLAG X<>Y FIX 02 RND X<>Y 36.41 STOFLAG R↓ R↓

(Update: Except that's not possible on the real HP-42S...)
Visit this user's website Find all posts by this user
Quote this message in a reply
03-22-2021, 03:05 PM
Post: #13
RE: HP-42S - How do I round a number?
(03-22-2021 05:03 AM)Werner Wrote:  In the same vein, SCI 03 rounding: (which, incidentally, also fails for 0.9999.. ;-)
Code:
1E8
RCLx ST Y
+
LASTX
-
Free42, use 1E30

A more robust way is with Veltkamp’s splitting, algorithm 3
On various ways to split a floating-point number

C = 1E30 + 1      // Free42-Decimal, SCI 03 (*)
Y = C*X
Z = X-Y+Y          // Z has 34-30 = 4 sig. digits

(*) not quite: SCI-03 does half-way away-from-zero, Z does banker's rounding.
Find all posts by this user
Quote this message in a reply
03-22-2021, 03:39 PM (This post was last modified: 03-22-2021 03:39 PM by Werner.)
Post: #14
RE: HP-42S - How do I round a number?
Yes, that's where I got it from (well from TJ Dekker) but I thought the shortcut was ok. I should have known better, again :-)
so, only 1 instruction more, and this time correct for 0.9999... :-)

Code:
 1E8
 RCLx ST Y
 RCL+ ST Y
 STO- ST Y
 +

Cheers, Werner
Find all posts by this user
Quote this message in a reply
03-23-2021, 07:57 PM
Post: #15
RE: HP-42S - How do I round a number?
Now I see why I didn't get a RaNDom number when I pressed RND!
B^)

10B, 10BII, 12C, 14B, 15C, 16C, 17B, 18C, 19BII, 20b, 22, 29C, 35, 38G, 39G, 41CV, 48G, 97
Find all posts by this user
Quote this message in a reply
03-25-2021, 05:39 PM
Post: #16
RE: HP-42S - How do I round a number?
(03-21-2021 08:27 PM)Didier Lachieze Wrote:  From the manual :
Quote:RND: Rounds the number in the X-register using the current display format.

So to round to two decimal places do: FIX 02 RND.

yes this is how the HP-41 RND function works as well.

**vp

http://www.series80.org
Find all posts by this user
Quote this message in a reply
03-25-2021, 05:42 PM
Post: #17
RE: HP-42S - How do I round a number?
On the 48G, you just do

2 RND

and it rounds the number to two decimal places on the first level of the stack (x register for 41/42 users).

With the 42, I have to do FIX 02 RND ALL do I don't effect all of the numbers on the stack.

HP48GX user learning the HP42s and DM42.
Find all posts by this user
Quote this message in a reply
Post Reply 




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