HP-42S - How do I round a number?
03-21-2021, 08:15 PM
Post: #1
 DM48 Member Posts: 89 Joined: May 2020
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.
03-21-2021, 08:27 PM
Post: #2
 Didier Lachieze Senior Member Posts: 1,428 Joined: Dec 2013
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.
03-21-2021, 08:30 PM
Post: #3
 DM48 Member Posts: 89 Joined: May 2020
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.
03-21-2021, 08:33 PM
Post: #4
 Guenter Schink Senior Member Posts: 462 Joined: Dec 2013
RE: HP-42S - How do I round a number?
Type in
Code:
6 1/x SHOW RND SHOW
You'll get the idea
03-21-2021, 08:59 PM
Post: #5
 Werner Senior Member Posts: 687 Joined: Dec 2013
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 -

Cheers, Werner
03-21-2021, 09:17 PM (This post was last modified: 03-21-2021 09:18 PM by DM48.)
Post: #6
 DM48 Member Posts: 89 Joined: May 2020
RE: HP-42S - How do I round a number?
Thank you everyone.

Werner, Perfect solution. Thank you!!

HP48GX user learning the HP42s and DM42.
03-21-2021, 10:51 PM
Post: #7
 Dave Britten Senior Member Posts: 2,064 Joined: Dec 2013
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 -

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.
03-21-2021, 10:59 PM
Post: #8
 Thomas Okken Senior Member Posts: 1,726 Joined: Feb 2014
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.
03-22-2021, 04:19 AM
Post: #9
 Werner Senior Member Posts: 687 Joined: Dec 2013
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
03-22-2021, 05:03 AM (This post was last modified: 03-22-2021 07:47 AM by Werner.)
Post: #10
 Werner Senior Member Posts: 687 Joined: Dec 2013
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
03-22-2021, 11:39 AM
Post: #11
 Dave Britten Senior Member Posts: 2,064 Joined: Dec 2013
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. You can, of course, adjust the magnitude of the number before and after rounding, though that adds additional steps/bytes.
03-22-2021, 12:19 PM (This post was last modified: 03-22-2021 12:22 PM by Thomas Okken.)
Post: #12
 Thomas Okken Senior Member Posts: 1,726 Joined: Feb 2014
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...)
03-22-2021, 03:05 PM
Post: #13
 Albert Chan Senior Member Posts: 1,791 Joined: Jul 2018
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.
03-22-2021, 03:39 PM (This post was last modified: 03-22-2021 03:39 PM by Werner.)
Post: #14
 Werner Senior Member Posts: 687 Joined: Dec 2013
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
03-23-2021, 07:57 PM
Post: #15
 Ren Member Posts: 178 Joined: Mar 2016
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
03-25-2021, 05:39 PM
Post: #16
 vassilisprevelakis Member Posts: 70 Joined: Mar 2014
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
03-25-2021, 05:42 PM
Post: #17
 DM48 Member Posts: 89 Joined: May 2020
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.
 « Next Oldest | Next Newest »

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