HP Forums

Full Version: HP-42S - How do I round a number?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
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.
Type in
Code:
6
1/x
SHOW
RND
SHOW
You'll get the idea
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
Thank you everyone.

Werner, Perfect solution. Thank you!!
(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.
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-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
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 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.
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 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.
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
Now I see why I didn't get a RaNDom number when I pressed RND!
B^)
(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
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.
(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

How do you enter the instruction code 2E9 directly in Free42?
(05-30-2022 08:41 PM)kostrse Wrote: [ -> ]
(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

How do you enter the instruction code 2E9 directly in Free42?

It's not an instruction code, it's just a number. Press 2 E 9, where E is the E key between +/- and backspace.

But as Werner wrote, in Free42 you should use 2E31 instead.
(05-30-2022 09:24 PM)Joe Horn Wrote: [ -> ]It's not an instruction code, it's just a number. Press 2 E 9, where E is the E key between +/- and backspace.

But as Werner wrote, in Free42 you should use 2E31 instead.

Oh, thank you! I thought that it's some undocumented processor instruction which needs to be entered directly.
Reference URL's