In the section of the program where you are asked to enter numbers yourself, we have actually included an ABS function to help your computer out of its predicament! Computers are happiest in Binary arithmetic but would probably settle for Hexadecimal as a compromise - but if we humans insist that micros communicate with us in the Decimal system then we have only ourselves to blame if the computer seems to get it wrong occasionally.
1 REM *** BBC VERSION *** 2 REM *** ROOTS *** 10 FORX=224TO230:VDU23,X:FORY=0TO7:READA:V DUA:NEXT, 20 MODE6:PRINTTAB(16,0)"ROOTS."'TAB(16)" ======" 30 VDU28,1,24,38,2,19,1,6;0; 40 PRINTTAB(0,23)"This program calculate s roots using both the internal '^' functio n in the computer ROM and the NEWTON/RAPHSON reiterative approximation." 50 PRINT'"When is a 5 not a 5 ??" 60 PRINT'"The program will demonstrate o ne of the problems faced by your computer when it tries to convert the result ofits bi nary system calculations into decimal for d isplay on the screen." 70 PRINT'"Although we would need to empl oy machine code methods to actually see the BIT difference between two seemin gly identical numbers,we can" 80 PRINT"show you the 'impossible' as yo ur computer fails to recognise the number for whic it is searching!" 90 PRINT'" Press SPACE.";:REP EATUNTILINKEY(-99) 100 PRINT'''''"Any root may be calculated by reiterative approximation using" 110 PRINT" A = 1"CHR$227" N + G*( R-1)"CHR$229'" R"CHR$228"G^(R-1) "CHR$230 120 PRINT"Where N=original number"'" R=root required"'" G=any number(constan t)"'" A=approximation to R"CHR$224" N"SP C(17)"produced by the formula." 130 PRINT'"If A<>G then we let G=A and re peatedlyapply the formula,making G equal to the preceding value of A until A=G. Atthis p oint A= "CHR$224" N" 140 PRINT''''" Press SPACE.";:R EPEATUNTILINKEY(-99) 150 PRINT'''''" A = 1"CHR$227" N + G*(R-1)"CHR$229'" R"CHR$228"G^(R-1) "CHR$230 160 PRINT'"Example to find "CHR$225;CHR$2 24"16" 170 PRINT'"Let G=3 (A reasonable guess s ince"'CHR$225;CHR$224"16=4 as you know!)" 180 G=3:N=16 190 GOSUB210 200 GOTO220 210 A=(N/G+G)/2:RETURN 220 PRINT"G=";G" gives A=";A 230 PRINT'"We now let G=";A" and try"'"th e formula again."' 240 G=A:GOSUB210 250 PRINT"G=";G" gives A=";A 260 IFA=G GOTO280 270 GOTO240 280 PRINT'"Therefore "CHR$225;CHR$224"16= ";A 290 PRINT"This computer says that "CHR$22 5;CHR$224"16=";SQR(16) 300 PRINT'" Press SPACE.";:REPE ATUNTILINKEY(-99) 310 PRINT''''''"Example to find "CHR$225; CHR$224"25" 320 PRINT'"Let G=3 again ("CHR$225;CHR$22 4"25=5 as we know!)"' 330 G=3:N=25:GOSUB210 340 PRINT"G=";G" gives A=";A 350 PRINT'"We now let G=";A" and try"'"th e formula again."' 360 G=A:GOSUB210 370 PRINT"G=";G" gives A=";A 380 IFA=G GOTO400 390 GOTO360 400 PRINT'"Therefore "CHR$224"25 = ";A 410 PRINT'"But the computer failed to sto p on thefirst G=5 gives A=5 because G does no tequal exactly 5!!" 420 PRINTTAB(0,24)'"Press R to repeat exa mples or SPACE";:*FX15 430 X$=GET$:IFX$="R" GOTO150 ELSE IFX$<>" " GOTO420 440 CLS 450 INPUTTAB(0,5)"Enter your number (N) " N$:IFN$="Q" END ELSEIFVAL(N$)<=0 GOTO450 460 N=VALN$:IFN>1E7 GOTO450 470 INPUTTAB(0,8)"Enter number of desired root"R$:IFVAL(R$)<=0 GOTO470 480 R=VALR$:IFR<2 OR R>99 GOTO470 490 G=2*(N^(1/R)):X=1 500 CLS:PRINTTAB(0,4)"Number ";N'"Root "; R'"Let G=";G' 510 A=(N/G^(R-1)+G*(R-1))/R 520 PRINT"<";X"> G=";G" gives A=";A 530 IFABS(A-G)<G/1E9 GOTO550 540 X=X+1:G=A:GOTO510 550 PRINT'''"The ";R" root of ";N" is ";A 560 PRINT'"The computer function says tha t the"';R" root of ";N" is ";N^(1/R) 570 PRINT'" Press SPACE":REPEATU NTILINKEY(-99) 580 CLS:PRINTTAB(0,4)"Enter 'Q' to quit o r" 590 GOTO450 600 DATA31,16,16,16,144,80,32,0 610 DATA112,144,32,64,240,0,0,0,240,16,11 2,16,240,0,0,0 620 DATA2,4,8,8,16,16,32,32,32,16,16,8,8, 4,2,0,64,32,16,16,8,8,4,4,4,8,8,16,16,32,64,0