10 ' PDQ2: Advanced PDQ; tolerance is specified as A/B, 20 ' allowing the ENTIRE algorithm to be in the integer domain. 30 randomize:for J=1 to isqrt(irnd):J=rnd:next 35 print chr(25); point 40 input "Decimal Number, Tolerance as A,B";J,A1,B1:print chr(2); 50 if or{A1<>round(A1),B1<>round(B1),A1<0} then print "Bad inputs":end 60 if or{A1=0,B1=0} then B1=1:A1=0 70 if B1<0 then B1=10^(-B1) 80 A=A1:B=B1:reduce A,B 90 if J>0 then 130 ' negative input gets rounded to F digits 100 F=12:J=-J:P=int(log(J)/log(10))+1 110 J*=10^(F-P):J=round(J):J/=10^(F-P) 120 print "Input rounded to";J 130 if type(J)=2 then N=num(J):D=den(J):goto 150 135 D=round(1/#eps) 140 N=round(J*D) 150 reduce D,N:D0=D:N0=N 160 Pn=0:Pd=1:Cn=1:Cd=0:Y=1:color 7 170 repeat 180 Q=N\D:R=res 190 print using(14),Q;:color 14 200 X=Q*Cn+Pn:Y=Q*Cd+Pd 210 Pn=Cn:Cn=X:Pd=Cd:Cd=Y 220 Err=abs(B*(X*D0-Y*N0)) 230 print Cn//Cd;:color 7:gosub 430 240 N=D:D=R 250 until Err<=Y*D0*A:color 7 260 if Q=1 then 460 270 Lo=0:Hi=Q 280 repeat Mid=(Lo+Hi)\2 290 X=Cn-Pn*Mid:Y=Cd-Pd*Mid 300 if abs(B*(X*D0-Y*N0))<=Y*D0*A then Lo=Mid else Hi=Mid 310 print using(14),Q-Mid; 320 print X//Y; 330 Err=abs(B*(X*D0-Y*N0)) 340 gosub 430 350 until Hi-Lo<=1 360 Mid=Lo 370 X=Cn-Pn*Mid:Y=Cd-Pd*Mid 380 if Mid then color 12 else 460 390 print using(14),Q-Mid;:color 14 400 print X//Y;:color 12 410 Err=abs(B*(X*D0-Y*N0)) 420 gosub 430:goto 460 430 if X/Y=J then color 9 440 if Err<=Y*D0*A then print " !" else print " -" 450 return 460 color 7 470 print " A/B =";A/B 480 print " Err =";J-X/Y 490 print "B*Err/A =";(J*B-X*B/Y)/max(1,A)