HP Forums
HP-42S - How do I round a number? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: HP-42S - How do I round a number? (/thread-16511.html)



HP-42S - How do I round a number? - DM48 - 03-21-2021 08:15 PM

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


RE: HP-42S - How do I round a number? - Didier Lachieze - 03-21-2021 08:27 PM

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.


RE: HP-42S - How do I round a number? - DM48 - 03-21-2021 08:30 PM

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.


RE: HP-42S - How do I round a number? - Guenter Schink - 03-21-2021 08:33 PM

Type in
Code:
6
1/x
SHOW
RND
SHOW
You'll get the idea


RE: HP-42S - How do I round a number? - Werner - 03-21-2021 08:59 PM

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


RE: HP-42S - How do I round a number? - DM48 - 03-21-2021 09:17 PM

Thank you everyone.

Werner, Perfect solution. Thank you!!


RE: HP-42S - How do I round a number? - Dave Britten - 03-21-2021 10:51 PM

(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.


RE: HP-42S - How do I round a number? - Thomas Okken - 03-21-2021 10:59 PM

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.


RE: HP-42S - How do I round a number? - Werner - 03-22-2021 04:19 AM

(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


RE: HP-42S - How do I round a number? - Werner - 03-22-2021 05:03 AM

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


RE: HP-42S - How do I round a number? - Dave Britten - 03-22-2021 11:39 AM

(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.


RE: HP-42S - How do I round a number? - Thomas Okken - 03-22-2021 12:19 PM

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...)


RE: HP-42S - How do I round a number? - Albert Chan - 03-22-2021 03:05 PM

(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.


RE: HP-42S - How do I round a number? - Werner - 03-22-2021 03:39 PM

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


RE: HP-42S - How do I round a number? - Ren - 03-23-2021 07:57 PM

Now I see why I didn't get a RaNDom number when I pressed RND!
B^)


RE: HP-42S - How do I round a number? - vassilisprevelakis - 03-25-2021 05:39 PM

(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


RE: HP-42S - How do I round a number? - DM48 - 03-25-2021 05:42 PM

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.


RE: HP-42S - How do I round a number? - kostrse - 05-30-2022 08:41 PM

(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?


RE: HP-42S - How do I round a number? - Joe Horn - 05-30-2022 09:24 PM

(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.


RE: HP-42S - How do I round a number? - kostrse - 05-30-2022 10:31 PM

(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.