Post Reply 
(HP-67/97) RATIONAL ARITHMETIC IN FLOATING-POINT by W. Kahan
05-02-2015, 05:39 PM (This post was last modified: 12-27-2023 12:55 AM by Thomas Klemm.)
Post: #1
(HP-67/97) RATIONAL ARITHMETIC IN FLOATING-POINT by W. Kahan
(02-04-2015 06:52 AM)Katie Wasserman Wrote:  A lot of this is fluff but not all, William Kahan has some papers on this site. For example this one on rational arithmetic in floating point. He gives programs for the 67/97 and the 71b and mentions the 15C and 41C.

Thanks for sharing the paper.

Cheers
Thomas

Usage:
Code:
        The stack holds four integers  X, Y, Z, T  construed as two rational numbers  Y/X  and  T/Z ,
        both presumed to be  in lowest terms (ilt).  If not,  pressing  [E]  will reduce  Y/X  to lowest
        terms while leaving  T/Z  unchanged.  The four rational operations are performed by pressing one of
        the keys  [A], [B], [C], [D]  to invoke reliable programs,  or  [a], [b], [c], [d]  to invoke obvious
        programs.  The reliable programs accept integers as large as  1,999,999,999  and deliver exactly
        correct results up to  8,000,000,000 . Specifically,  the programs ...

Add:         Press  [A]  or  [a]  to put  Y/X := (T/Z) + (Y/X)  ilt,  leaving  T/Z  unchanged.
Subtract:    Press  [B]  or  [b]  to put  Y/X := (T/Z) - (Y/X)  ilt,  leaving  T/Z  unchanged.
Multiply:    Press  [C]  or  [c]  to put  Y/X := (T/Z) * (Y/X)  ilt,  leaving  T/Z  unchanged.
Divide:      Press  [D]  or  [d]  to put  Y/X := (T/Z) / (Y/X)  ilt,  leaving  T/Z  unchanged.
Reduce:      Press  [E]  to put  Y/X := (Y/X)  ilt,  leaving  T/Z  unchanged.
GCD:         Press  [e]  to put  X := Greatest Common Divisor of  X and Y .
REM:         Press  [GSB] [8]  to put   X := Y - nX   and   n : = Integer nearest Y/X   into  reg. 8 .

        The programs use registers  0 to 8  and I,  and labels  2 to 8  too.

Example:

\(\frac{123,456,799}{123,456}-\frac{988,297,396}{988,291}=\frac{31}{189751872}\)

1234 56799.
[ENTER↑]
123456.
[ENTER↑]
9882 97396.
[ENTER↑]
988291.
\([\)B]
1897 51872.
[x<>y]
31.


Program:
Code:
001: 31 25 11 ; LBL A
002:       42 ;     CHS
003: 31 25 12 ; LBL B
004: 31 22 07 ;     GSB 7
005:    35 52 ;     X<>Y
006:    35 54 ;     R^
007:    33 04 ;     STO 4
008: 32 22 15 ;     GSB e
009:    31 51 ;     X=0?
010:       43 ;     EEX
011:    33 05 ;     STO 5
012: 33 81 00 ;     STO/ 0
013: 33 81 04 ;     STO/ 4
014:    34 01 ;     RCL 1
015:    35 52 ;     X<>Y
016: 31 22 08 ;     GSB 8
017:    34 04 ;     RCL 4
018:       71 ;     *
019:    33 06 ;     STO 6
020:    34 03 ;     RCL 3
021:    35 54 ;     R^
022: 31 22 08 ;     GSB 8
023:    34 00 ;     RCL 0
024:    33 07 ;     STO 7
025:       71 ;     *
026:    34 06 ;     RCL 6
027:       51 ;     -
028: 32 22 15 ;     GSB e
029: 33 81 05 ;     STO/ 5
030:    34 01 ;     RCL 1
031:    35 52 ;     X<>Y
032: 31 22 08 ;     GSB 8
033:    34 04 ;     RCL 4
034: 33 71 00 ;     STO* 0
035:       71 ;     *
036:    33 01 ;     STO 1
037:    34 08 ;     RCL 8
038:    33 06 ;     STO 6
039:    34 03 ;     RCL 3
040:    35 54 ;     R^
041: 31 22 08 ;     GSB 8
042:    34 07 ;     RCL 7
043:       71 ;     *
044:    34 01 ;     RCL 1
045:       51 ;     -
046:    35 52 ;     X<>Y
047:       81 ;     /
048:    33 01 ;     STO 1
049:    34 05 ;     RCL 5
050: 33 71 00 ;     STO* 0
051:    34 08 ;     RCL 8
052:    33 05 ;     STO 5
053: 31 25 05 ; LBL 5
054:    34 06 ;     RCL 6
055:    34 04 ;     RCL 4
056:       71 ;     *
057:       41 ;     ENTER
058:       41 ;     ENTER
059:    34 05 ;     RCL 5
060:    34 07 ;     RCL 7
061:       71 ;     *
062:       71 ;     *
063:       43 ;     EEX
064:       01 ;     1
065:       00 ;     0
066:    32 81 ;     X>Y?
067:    22 04 ;         GTO 4
068:    34 06 ;     RCL 6
069:    35 64 ;     ABS
070:    34 04 ;     RCL 4
071:    35 64 ;     ABS
072:    32 71 ;     X<=Y?
073:    22 03 ;         GTO 3
074:    35 82 ;     LASTX
075:    34 06 ;     RCL 6
076:    33 04 ;     STO 4
077:    35 52 ;     X<>Y
078:    33 06 ;     STO 6
079: 31 25 03 ; LBL 3
080:    34 05 ;     RCL 5
081:    35 64 ;     ABS
082:    34 07 ;     RCL 7
083:    35 64 ;     ABS
084:    32 71 ;     X<=Y?
085:    22 03 ;         GTO 3
086:    35 82 ;     LASTX
087:    34 05 ;     RCL 5
088:    33 07 ;     STO 7
089:    35 52 ;     X<>Y
090:    33 05 ;     STO 5
091: 31 25 03 ; LBL 3
092:    34 07 ;     RCL 7
093:    35 64 ;     ABS
094:    34 04 ;     RCL 4
095:    35 64 ;     ABS
096:    32 71 ;     X<=Y?
097:    22 03 ;         GTO 3
098:    34 06 ;     RCL 6
099:    34 07 ;     RCL 7
100: 31 22 08 ;     GSB 8
101:    33 06 ;     STO 6
102:    34 04 ;     RCL 4
103:    34 08 ;     RCL 8
104:       71 ;     *
105: 33 51 05 ;     STO- 5
106:    22 05 ;         GTO 5
107: 31 25 03 ; LBL 3
108:    34 05 ;     RCL 5
109:    34 04 ;     RCL 4
110: 31 22 08 ;     GSB 8
111:    33 05 ;     STO 5
112:    34 07 ;     RCL 7
113:    34 08 ;     RCL 8
114:       71 ;     *
115: 33 51 06 ;     STO- 6
116:    22 05 ;         GTO 5
117: 31 25 04 ; LBL 4
118:    35 82 ;     LASTX
119:    35 54 ;     R^
120:       51 ;     -
121: 33 61 01 ;     STO+ 1
122:    22 06 ;         GTO 6
123: 32 25 15 ; LBL e
124:       03 ;     3
125:       42 ;     CHS
126:    35 33 ;     STO I
127:    35 53 ;     RDN
128:    31 51 ;     X=0?
129:    22 02 ;         GTO 2
130: 31 22 08 ;     GSB 8
131:    22 24 ;         GTO (i)
132: 31 25 08 ; LBL 8
133:    33 08 ;     STO 8
134:    35 52 ;     X<>Y
135:       41 ;     ENTER
136:       41 ;     ENTER
137:    34 08 ;     RCL 8
138:       81 ;     /
139:    23 00 ;     DSP 0
140:    31 24 ;     RND
141:    33 08 ;     STO 8
142:    35 54 ;     R^
143:       71 ;     *
144:       51 ;     -
145:    35 22 ;         RTN
146: 31 25 02 ; LBL 2
147:    35 52 ;     X<>Y
148:    35 64 ;     ABS
149:    35 22 ;         RTN
150: 31 25 07 ; LBL 7
151:    33 00 ;     STO 0
152:    35 53 ;     RDN
153:    33 01 ;     STO 1
154:    35 53 ;     RDN
155:    33 02 ;     STO 2
156:    35 53 ;     RDN
157:    33 03 ;     STO 3
158:    35 22 ;         RTN
159: 31 25 15 ; LBL E
160: 31 22 07 ;     GSB 7
161:    35 53 ;     RDN
162: 32 22 15 ;     GSB e
163:    31 51 ;     X=0?
164:    22 06 ;         GTO 6
165: 33 81 00 ;     STO/ 0
166: 33 81 01 ;     STO/ 1
167: 31 25 06 ; LBL 6
168:    34 03 ;     RCL 3
169:    34 02 ;     RCL 2
170:    34 01 ;     RCL 1
171:    34 00 ;     RCL 0
172:    31 81 ;     X>0?
173:    35 22 ;         RTN
174:       42 ;     CHS
175:    35 52 ;     X<>Y
176:       42 ;     CHS
177:    35 52 ;     X<>Y
178:    35 22 ;         RTN
179: 31 25 14 ; LBL D
180:    35 52 ;     X<>Y
181: 31 25 13 ; LBL C
182: 31 22 07 ;     GSB 7
183:    33 04 ;     STO 4
184: 32 22 15 ;     GSB e
185:    31 61 ;     X#0?
186: 33 81 00 ;     STO/ 0
187:    31 61 ;     X#0?
188: 33 81 04 ;     STO/ 4
189:    34 01 ;     RCL 1
190:    34 02 ;     RCL 2
191: 32 22 15 ;     GSB e
192:    31 61 ;     X#0?
193: 33 81 01 ;     STO/ 1
194:    34 02 ;     RCL 2
195:    35 52 ;     X<>Y
196:    31 61 ;     X#0?
197:       81 ;     /
198:       00 ;     ST0* 0
199:    34 04 ;     RCL 4
200: 33 71 01 ;     STO* 1
201:    22 06 ;         GTO 6
202: 32 25 11 ; LBL a
203:       42 ;     CHS
204: 32 25 12 ; LBL b
205: 31 22 07 ;     GSB 7
206:       71 ;     *
207:    35 52 ;     X<>Y
208:    35 54 ;     R^
209: 33 71 00 ;     STO* 0
210:       71 ;     *
211:       51 ;     -
212:    33 01 ;     STO 1
213: 31 22 06 ;     GSB 6
214:    22 15 ;         GTO E
215: 32 25 14 ; LBL d
216:    35 52 ;     X<>Y
217: 32 25 13 ; LBL c
218: 31 22 07 ;     GSB 7
219: 33 71 01 ;     STO* 1
220:    35 54 ;     R^
221: 33 71 00 ;     STO* 0
222: 31 22 06 ;     GSB 6
223:    22 15 ;         GTO E

Cards for Jacques Laporte's HP-67 Emulator:
Code:
 0 0 0 0 0 0 0
 3 0 0 0 2 2 2
 9 4 4 7 3 0 b
 7 f b 1 c f a
 7 1 8 4 8 0 9
 5 1 d 5 4 a e
 4 7 7 3 9 6 3
 9 7 4 b 8 3 0
 8 5 a e 3 8 3
 9 9 7 7 0 b 8
 9 1 3 9 e 0 7
 4 b 8 3 0 7 1
 3 9 7 7 b 8 4
 7 7 3 9 6 7 8
 e 0 7 5 9 1 1
 e 3 0 3 8 7 1
 1 b 3 9 7 4 7
 6 f 5 9 5 7 8
 1 1 1 d 3 9 3
 9 7 7 7 5 1 b
 2 6 7 4 2 6 7
 6 d 4 5 2 1 0
 9 6 3 0 9 4 7
 6 4 3 d 3 5 7
 d 3 5 7 2 6 7
 7 2 6 7 5 f 3
 7 7 f 3 9 5 3
 0 9 7 7 5 4 3
 7 7 7 6 d 3 5
 7 2 6 7 4 2 6
 d 5 a 5 3 9 7
 8 7 4 9 6 b 8
 7 8 7 7 9 5 b
 8 7 4 7 5 f 3
 2 5 2 1 f 0 c
Code:
 0 0 0 0 0 0 0
 4 0 0 0 2 2 2
 3 8 4 7 4 3 f
 4 d 5 a 6 3 9
 3 1 9 f 1 c 1
 3 e e d 6 c 1
 3 0 9 8 f 8 d
 f b 8 d 2 5 4
 9 8 2 e 6 0 1
 e 7 8 1 b 1 b
 2 6 3 0 f 2 0
 e 3 8 3 9 4 7
 9 2 3 1 9 1 3
 1 9 0 f 7 0 e
 a e 3 1 b 7 f
 e 0 e 9 3 3 1
 7 2 7 3 f 6 8
 1 8 0 d 6 5 4
 1 c 3 0 1 c 0
 e 5 5 7 0 7 1
 9 4 b 7 f c 3
 0 f d 0 e 3 0
 7 2 7 1 8 4 5
 3 8 0 5 3 a e
 1 e 5 3 3 0 7
 2 8 1 5 3 a e
 e b 1 c e a d
 6 e 1 7 4 e 0
 3 8 3 9 e 0 4
 7 3 0 3 9 b 7
 b 7 f c 3 0 e
 d d e b 6 9 1
 0 0 0 0 d e b
 6 e 0 4 7 e 1
 4 f e 4 2 8 c
Find all posts by this user
Quote this message in a reply
06-13-2015, 01:58 AM
Post: #2
RE: (HP-67/97) RATIONAL ARITHMETIC IN FLOATING-POINT by W. Kahan
Thanks for posting this. Early on I entered the program, but apparently missed one step, which threw me off. I let it sit for a long time. Tonight I went through step by step and found my error. Now it works just fine.


Regards,
Bob
Find all posts by this user
Quote this message in a reply
06-24-2016, 02:37 PM
Post: #3
RE: (HP-67/97) RATIONAL ARITHMETIC IN FLOATING-POINT by W. Kahan
(06-13-2015 01:58 AM)bshoring Wrote:  Thanks for posting this. Early on I entered the program, but apparently missed one step, which threw me off. I let it sit for a long time. Tonight I went through step by step and found my error. Now it works just fine.

Bob, if you're using RPN-67, simply paste the entire code into the listing area of an empty card. No typing or editing required.
Find all posts by this user
Quote this message in a reply
06-24-2016, 03:19 PM (This post was last modified: 06-24-2016 03:20 PM by Willy R. Kunz.)
Post: #4
RE: (HP-67/97) RATIONAL ARITHMETIC IN FLOATING-POINT by W. Kahan
(02-04-2015 06:52 AM)Katie Wasserman Wrote:  A lot of this is fluff but not all, William Kahan has some papers on this site. For example this one on rational arithmetic in floating point. He gives programs for the 67/97 and the 71b and mentions the 15C and 41C.

Thanks for sharing the paper.

Cheers
Thomas

Usage:
Code:
        The stack holds four integers  X, Y, Z, T  construed as two rational numbers  Y/X  and  T/Z ,
        both presumed to be  in lowest terms (ilt).  If not,  pressing  [E]  will reduce  Y/X  to lowest
        terms while leaving  T/Z  unchanged.  The four rational operations are performed by pressing one of
        the keys  [A], [B], [C], [D]  to invoke reliable programs,  or  [a], [b], [c], [d]  to invoke obvious
        programs.  The reliable programs accept integers as large as  1,999,999,999  and deliver exactly
        correct results up to  8,000,000,000 . Specifically,  the programs ...

Add:         Press  [A]  or  [a]  to put  Y/X := (T/Z) + (Y/X)  ilt,  leaving  T/Z  unchanged.
Subtract:    Press  [B]  or  [b]  to put  Y/X := (T/Z) - (Y/X)  ilt,  leaving  T/Z  unchanged.
Multiply:    Press  [C]  or  [c]  to put  Y/X := (T/Z) * (Y/X)  ilt,  leaving  T/Z  unchanged.
Divide:      Press  [D]  or  [d]  to put  Y/X := (T/Z) / (Y/X)  ilt,  leaving  T/Z  unchanged.
Reduce:      Press  [E]  to put  Y/X := (Y/X)  ilt,  leaving  T/Z  unchanged.
GCD:         Press  [e]  to put  X := Greatest Common Divisor of  X and Y .
REM:         Press  [GSB] [8]  to put   X := Y - nX   and   n : = Integer nearest Y/X   into  reg. 8 .

        The programs use registers  0 to 8  and I,  and labels  2 to 8  too.

Example:

\(\frac{123,456,799}{123,456}-\frac{988,297,396}{988,291}=\frac{31}{189751872}\)

1234 56799.
[ENTER↑]
123456.
[ENTER↑]
9882 97396.
[ENTER↑]
988291.
\([\)B]
1897 51872.
[x<>y]
31.


Program:
Code:
LBL A
    CHS
LBL B
    GSB 7
    X<>Y
    R^
    STO 4
    GSB e
    X=0?
    EEX
    STO 5
    STO/ 0
    STO/ 4
    RCL 1
    X<>Y
    GSB 8
    RCL 4
    *
    STO 6
    RCL 3
    R^
    GSB 8
    RCL 0
    STO 7
    *
    RCL 6
    -
    GSB e
    STO/ 5
    RCL 1
    X<>Y
    GSB 8
    RCL 4
    STO* 0
    *
    STO 1
    RCL 8
    STO 6
    RCL 3
    R^
    GSB 8
    RCL 7
    *
    RCL 1
    -
    X<>Y
    /
    STO 1
    RCL 5
    STO* 0
    RCL 8
    STO 5
LBL 5
    RCL 6
    RCL 4
    *
    ENTER
    ENTER
    RCL 5
    RCL 7
    *
    *
    EEX
    1
    0
    X>Y?
        GTO 4
    RCL 6
    ABS
    RCL 4
    ABS
    X<=Y?
        GTO 3
    LASTX
    RCL 6
    STO 4
    X<>Y
    STO 6
LBL 3
    RCL 5
    ABS
    RCL 7
    ABS
    X<=Y?
        GTO 3
    LASTX
    RCL 5
    STO 7
    X<>Y
    STO 5
LBL 3
    RCL 7
    ABS
    RCL 4
    ABS
    X<=Y?
        GTO 3
    RCL 6
    RCL 7
    GSB 8
    STO 6
    RCL 4
    RCL 8
    *
    STO- 5
        GTO 5
LBL 3
    RCL 5
    RCL 4
    GSB 8
    STO 5
    RCL 7
    RCL 8
    *
    STO- 6
        GTO 5
LBL 4
    LASTX
    R^
    -
    STO+ 1
        GTO 6
LBL e
    3
    CHS
    STO I
    RDN
    X=0?
        GTO 2
    GSB 8
        GTO (i)
LBL 8
    STO 8
    X<>Y
    ENTER
    ENTER
    RCL 8
    /
    DSP 0
    RND
    STO 8
    R^
    *
    -
        RTN
LBL 2
    X<>Y
    ABS
        RTN
LBL 7
    STO 0
    RDN
    STO 1
    RDN
    STO 2
    RDN
    STO 3
        RTN
LBL E
    GSB 7
    RDN
    GSB e
    X=0?
        GTO 6
    STO/ 0
    STO/ 1
LBL 6
    RCL 3
    RCL 2
    RCL 1
    RCL 0
    X>0?
        RTN
    CHS
    X<>Y
    CHS
    X<>Y
        RTN
LBL D
    X<>Y
LBL C
    GSB 7
    STO 4
    GSB e
    X#0?
    STO/ 0
    X#0?
    STO/ 4
    RCL 1
    RCL 2
    GSB e
    X#0?
    STO/ 1
    RCL 2
    X<>Y
    X#0?
    /
    ST0* 0
    RCL 4
    STO* 1
        GTO 6
LBL a
    CHS
LBL b
    GSB 7
    *
    X<>Y
    R^
    STO* 0
    *
    -
    STO 1
    GSB 6
        GT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 9 e 0 4
 7 3 0 3 9 b 7
 b 7 f c 3 0 e
 d d e b 6 9 1
 0 0 0 0 d e b
 6 e 0 4 7 e 1
 4 f e 4 2 8 c
[/quote]

Users of RPN-67/97 may import the attached program card (via iTunes file sharing).

.zip  RatArithmFP.rpn67.zip (Size: 1.74 KB / Downloads: 18)
Find all posts by this user
Quote this message in a reply
Post Reply 




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