Index wersja polskawersja polska

Elektronika MK-85 - algorithms

Function RAN# (ROM subroutine 2FC6)

The calculator actually generates pseudo-random integer numbers in range 0-32767. The result is later multiplied by 0.30517578125E-4 (=1/32768) to return a real number in range 0-0.9999...

The BASIC program below calculates pseudo-random 15-bit integer numbers using the same algorithm as function RAN#. For comparison it also prints values returned by actual RAN# function.

10 S=32768*RAN#
20 S=12869*S+6925
30 S=S-32768*INT(S/32768)
40 PRINT S;32768*RAN#
50 GOTO 20

Square root (ROM subroutine 300E)

The BASIC program shown below illustrates the algorithm used by the MK-85. It calculates square roots of numbers in range 1 to 99.9999... with a precision of R digits.

The original procedure uses only simple and effective math operations: shift, add/subtract, compare and table lookup. The BASIC version thereof had to simulate shifts of BCD numbers with multiplications by powers of 10.

The detailed algorithm description of this and the following functions can be found in the article J.E. Meggitt, Pseudo Division and Pseudo Multiplication processes, IBM Journal, Res & Dev, April 1962.

5 R=7
10 INPUT "Radicand=",A
20 IF A<1 THEN 40
30 IF A<100 THEN 50
40 PRINT "Invalid!":GOTO 10
50 B=1:Q=0:J=0

200 IF J>=R THEN 600
210 IF A<B THEN 400

300 A=A-B
310 GOSUB 800
320 B=B+2*D
330 Q=Q+1
340 GOTO 200

400 A=10*A
410 B=B-D
420 Q=10*Q
430 J=J+1
440 GOSUB 800
450 B=B+D
460 GOTO 200

600 PRINT "SQR=";Q/10^R
610 GOTO 10

800 D=10^-J
810 RETURN

Function EXP (ROM subroutine 30A2)

The following BASIC program calculates EXP(x) for numbers in range 0 to LN(10) with a precision of R digits. It uses the same algorithm as the original code in the MK-85 ROM.

10 R=7:IF R>6; DEFM(R-1)
20 FOR J=0 TO R-1
30 T(J)=LN(1+10^-J)*10^J
40 NEXT J

100 INPUT "Argument=",A
110 IF A<0 THEN 130
120 IF A<LN(10) THEN 140
130 PRINT "Invalid!":GOTO 100
140 Q=0:J=0
150 GOTO 500

200 IF J>=R THEN 600
210 IF A<B THEN 400

300 A=A-B
310 Q=Q+1
320 GOTO 200

400 A=A*10
410 Q=Q*10
420 J=J+1

500 B=T(J)
510 GOTO 200

600 A=0:B=10^R

700 IF FRAC(Q/10)<>0 THEN 800
710 IF Q<>0 THEN 900
720 GOTO 1000

800 M=INT(B/10^J)
810 A=A+B
820 B=B+M
830 Q=Q-1
840 GOTO 700

900 A=INT(A/10)
910 Q=INT(Q/10)
920 J=J-1
930 GOTO 700

1000 PRINT "EXP=";1+A/10^(R+J)
1010 GOTO 100

Natural logarithm (ROM subroutine 31AC)

The following BASIC program calculates LN(x) for numbers in range 1 to 10 with a precision of R digits. It uses the same algorithm as the original code in the MK-85 ROM.

10 R=7:IF R>6; DEFM(R-1)
20 FOR J=0 TO R-1
30 T(J)=LN(1+10^-J)*10^J
40 NEXT J

100 INPUT "Argument=",A
110 IF A<1 THEN 130
120 IF A<10 THEN 140
130 PRINT "Invalid!":GOTO 100
140 Q=0:B=1:J=0
150 A=A-1

200 IF J>=R THEN 500
210 IF A<B THEN 400

300 M=B*10^-J
310 A=A-B
320 B=B+M
330 Q=Q+1
340 GOTO 200

400 A=A*10
410 Q=Q*10
420 J=J+1
430 GOTO 200

500 A=0
510 GOTO 900

600 IF 10*FRAC(Q/10)<>0 THEN 700
610 IF Q<>0 THEN 800
620 GOTO 1000

700 A=A+B
710 Q=Q-1
720 GOTO 600

800 A=INT(A/10)
810 Q=INT(Q/10)
820 J=J-1

900 B=T(J)
910 GOTO 600

1000 PRINT "LN=";A*10^-J
1010 GOTO 100

Arcus tangent (ROM subroutine 33E4)

The following BASIC program calculates ATN(x) for arguments in range 0 to 1 with a precision of R digits. It uses the same algorithm as the original code in the MK-85 ROM.

10 R=7:IF R>6; DEFM(R-1)
20 FOR J=0 TO R-1
30 T(J)=ATN(10^-J)*10^J
40 NEXT J

100 INPUT "Argument=",A
110 IF A<0 THEN 130
120 IF A<1 THEN 140
130 PRINT "Invalid!":GOTO 100
140 Q=0:B=1:J=0

200 IF J>=R THEN 500
210 IF A<B THEN 400

300 M=A*10^(-2*J)
310 A=A-B
320 B=B+M
330 Q=Q+1
340 GOTO 200

400 A=A*10
410 Q=Q*10
420 J=J+1
430 GOTO 200

500 A=0
510 GOTO 900

600 IF 10*FRAC(Q/10)<>0 THEN 700
610 IF Q<>0 THEN 800
620 GOTO 1000

700 A=A+B
710 Q=Q-1
720 GOTO 600

800 A=INT(A/10)
810 Q=INT(Q/10)
820 J=J-1

900 B=T(J)
910 GOTO 600

1000 PRINT "ATN=";A*10^-J
1010 GOTO 100

Tangent (ROM subroutine 362C)

The following BASIC program calculates TAN(x) for angles in range 0 to PI/2 radians with a precision of R digits. It uses the same algorithm as the original code in the MK-85 ROM.

10 MODE 5:R=7:IF R>6; DEFM(R-1)
20 FOR J=0 TO R-1
30 T(J)=ATN(10^-J)*10^J
40 NEXT J

100 INPUT "Argument=",A
110 IF A<0 THEN 130
120 IF A<PI/2 THEN 140
130 PRINT "Invalid!":GOTO 100
140 Q=0:J=0
150 GOTO 500

200 IF J>=R THEN 600
210 IF A<B THEN 400

300 A=A-B
310 Q=Q+1
320 GOTO 200

400 A=A*10
410 Q=Q*10
420 J=J+1

500 B=T(J)
510 GOTO 200

600 A=0:B=10^R

700 IF 10*FRAC(Q/10)<>0 THEN 800
710 IF Q<>0 THEN 900
720 GOTO 1000

800 M=INT(A*10^(-2*J))
810 A=A+B
820 B=B-M
830 Q=Q-1
840 GOTO 700

900 A=INT(A/10)
910 Q=INT(Q/10)
920 J=J-1
930 GOTO 700

1000 PRINT "TAN=";A/B/10^J
1010 GOTO 100