Post Reply 
fraction program hp 28s?
06-26-2024, 02:13 AM (This post was last modified: 06-26-2024 02:42 AM by Thomas Klemm.)
Post: #13
RE: fraction program hp 28s?
To analyse the algorithm I used the following program for the HP-42S:
Code:
00 { 4-Byte Prgm }
01 STO÷ ST Y
02 1/X
03 FP
04 END

Example

Using Free42:

1 ENTER 0.34

R/S

y: 2.941176470588235294117647058823529
x: 0.941176470588235294117647058823529

R/S

y: 3.125000000000000000000000000000001
x: 0.0625

R/S

y: 50.00000000000000000000000000000002
x: 0

But it's easier to follow if the numbers are written as fractions:

y: 1
x: 17/50

y: 50/17
x: 16/17

y: 50/17 * 17/16
y: 1/16

y: 50/17 * 17/16 * 16/1
x: 0/1

Now we can see that this is Euclid's algorithm to calculate the greatest common divisor in disguise.
In the Y-register the intermediate values cancel leaving us with 50/1.

We may also notice that the first two rounds are a somewhat convoluted way to calculate the fraction part of the number.
Instead we could start with:
Code:
1 OVER ABS FP

Here's the program for the HP-42S:
Code:
00 { 28-Byte Prgm }
01 1
02 RCL ST Y
03 ABS
04 FP
05▸LBL 00
06 STO÷ ST Y
07 1/X
08 FP
09 1ᴇ-4
10 X≥Y?
11 GTO 01
12 R↓
13 GTO 00
14▸LBL 01
15 R↓
16 R↓
17 ×
18 LASTX
19 END

Example

2.34

R/S

y: 117
x: 50

I didn't bother to round the result.

Also I noticed that for PI I get an Out of Range error message.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
fraction program hp 28s? - Rafa - 06-21-2024, 12:24 PM
RE: fraction program hp 28s? - C.Ret - 06-22-2024, 08:51 PM
RE: fraction program hp 28s? - Rafa - 06-23-2024, 06:08 PM
RE: fraction program hp 28s? - Peet - 06-22-2024, 10:44 PM
RE: fraction program hp 28s? - Rafa - 06-23-2024, 06:52 PM
RE: fraction program hp 28s? - Rafa - 06-23-2024, 06:43 PM
RE: fraction program hp 28s? - C.Ret - 06-24-2024, 07:39 PM
RE: fraction program hp 28s? - Rafa - 06-25-2024, 01:36 PM
RE: fraction program hp 28s? - C.Ret - 06-25-2024, 05:27 PM
RE: fraction program hp 28s? - Rafa - 06-25-2024, 07:46 PM
RE: fraction program hp 28s? - Thomas Klemm - 06-26-2024 02:13 AM



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