Page 3 of 4

Re: A test program?

Posted: Mon Jun 25, 2018 12:58 pm
by BillO
Chromatix wrote:
Quote:
Can you predict what this 5 liner is going to output without RUNning it? Better yet, can you explain why this works?
It should print a good approximation to pi. It works because sin(x) has a gradient asymptotically approaching -1 as x -> pi, and a value crossing zero at exactly pi.
With the precision of most small BASIC interpreters, looping 5 times seems optimistic. That algorithm should converge pretty quickly

Re: A test program?

Posted: Mon Jun 25, 2018 3:10 pm
by BigEd
Thanks for the offerings Bruce - I've linked this thread over in the Stardot thread about type-ins for unattended Beebs.

Edit: ahem, thanks also to Mike and Bill for their offerings!

Re: A test program?

Posted: Mon Jun 25, 2018 8:23 pm
by Chromatix
BillO wrote:
With the precision of most small BASIC interpreters, looping 5 times seems optimistic. That algorithm should converge pretty quickly
Yes, on BBC BASIC IV (which uses a 5-byte format with a 32-bit mantissa), it has 3 decimals after one iteration, and at least 8 decimals after the second iteration. Modifying the program to test equality between iterations reveals that the result of the third iteration is identical to the second.

An equivalent program in C using "long double" format on an Intel-based Mac converges after 3 iterations, with 10 decimals being correct after the second.

Re: A test program?

Posted: Mon Jun 25, 2018 8:27 pm
by BigEd
It would be amusing to change the loop from
20 FOR I=1 TO 5
to
20 FOR I=1 TO 3.14

Re: A test program?

Posted: Tue Jun 26, 2018 12:53 am
by dclxvi
BillO wrote:
With the precision of most small BASIC interpreters, looping 5 times seems optimistic. That algorithm should converge pretty quickly
The purpose of looping a few extra times is to demonstrate that it's converging to pi, rather than being an exact mathematical expression like ATN(1)*4 that gets calculated to some precision.

The number of correct digits should roughly triple after each iteration. If the approximation P is pi+x where x (which can be positive or negative) is the error, then:

P + sin(P)
= pi+x + sin(pi+x)
= pi+x - sin(x), using the trig identity for sin(pi+x)
= pi+x - x + x^3 / 3! - x^5 / 5! + ..., using the power series for sin(x)
= pi + x^3 / 3! - x^5 / 5! + ...

and the new error is dominated by the x^3 term. If you have D correct digits, then x (the error) is about 10^-D (or for that matter base^-D since it doesn't have to be base 10); after the next iteration, the error is on the order of x^3 = (10^-D)^3 = 10^-(3*D), meaning roughly 3*D digits are now correct.
BigEd wrote:
20 FOR I=1 TO 3.14
Or, for a little misdirection, FOR I=0 TO EXP(1). Eat your heart out, Euler!

Re: A test program?

Posted: Tue Jun 26, 2018 8:16 am
by BigEd
Oh yes, EXP(1), I like that!

Re: A test program?

Posted: Thu Jun 28, 2018 1:07 pm
by floobydust
First, thanks to all who have offered some interesting test programs, much appreciated! I also found the WIDTH setting and changed the default, so the first test program (from Mike) works as it should along with the Mandelbrot program (from Bill). I've been swamped on other fronts this past week, but spent some time working on the code yesterday and late last night. and still trying to resolve some odd issues.

1- Using Ctrl-C will generally exit the running program, but attempting to RUN it again results in a BRK condition that the BIOS/Monitor routines traps. I have to Cold start EhBasic to resolve it.
2- Using a Warm start will show the existing program via LIST, but again, it won't RUN without a Cold start and re-entering the program.

There's more than the above... but I also tried the programs from Bruce with odd results. The first one's results:

Code: Select all

Ready
10 P=3
20 FOR I=1 TO 5
30 PRINT P
40 P=P+SIN(P)
50 NEXT
RUN
 3
 3.14112
 3.14159
 3.14159
 3.14159

Ready
The second's results:

Code: Select all

Ready
100 DIM N(104)
110 FOR T=1 TO 104:N(T)=2:NEXT
120 FOR D=1 TO 31
130 Q=0
140 FOR T=104 TO 1 STEP -1
150 B=2*T-1
160 IF T=1 THEN B=10
170 R=T*Q+N(T)*10:Q=INT(R/B):N(T)=R-Q*B
180 NEXT
190 PRINT Q;
200 IF D=1 THEN PRINT ".";
210 NEXT
RUN
 3. 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9
Ready
The third program only printed blanks (spaces) on multiple lines, so not sure what it should be doing:

Code: Select all

Ready
10 P=ATN(1)*4
20 FOR Y = -10.5 TO 10.5
30 FOR X = -10.75 TO 10.75 STEP .5
40 A=ATN(Y/X)+(X<0)*SGN(Y)*P
50 C=(SQR(X*X+Y*Y)-A/2)/P
60 PRINT CHR$(32+3*(C-INT(C)<.4));
70 NEXT
80 PRINT
90 NEXT
RUN
                                 
                        
                      
                              
                         
                      
                              
                            
                       
                            
                          
                           
                         
                             
                          
                       
                               
                             
                      
                                
                             
                   

Ready
The fourth program runs endlessly (I eventually stop it) as:

Code: Select all

Ready
10 W=79
20 DIM A(W+1):A(W/2+1)=1
30 FOR I=0 TO 7:B(I)=ASC(MID$("00011110",8-I))-48:NEXT
40 P=A(W):A(W+1)=A(1):FOR I=1 TO W:N=A(I)
50 PRINT CHR$(32+3*N);
60 A(I)=B(4*P+2*N+A(I+1)):P=N:NEXT:PRINT
70 GOTO 40
RUN
                                       #                                       
                                      ###                                      
                                     ##  #                                     
                                    ## ####                                    
                                   ##  #   #                                   
                                  ## #### ###                                  
                                 ##  #    #  #                                 
                                ## ####  ######                                
                               ##  #   ###     #                               
                              ## #### ##  #   ###                              
                             ##  #    # #### ##  #                             
                            ## ####  ## #    # ####                            
                           ##  #   ###  ##  ## #   #                           
                          ## #### ##  ### ###  ## ###                          
                         ##  #    # ###   #  ###  #  #                         
                        ## ####  ## #  # #####  #######                        
                       ##  #   ###  #### #    ###      #                       
                      ## #### ##  ###    ##  ##  #    ###                      
                     ##  #    # ###  #  ## ### ####  ##  #                     
                    ## ####  ## #  ######  #   #   ### ####                    
                   ##  #   ###  ####     #### ### ##   #   #                   
                  ## #### ##  ###   #   ##    #   # # ### ###                  
                 ##  #    # ###  # ### ## #  ### ## # #   #  #                 
                ## ####  ## #  ### #   #  ####   #  # ## ######                
               ##  #   ###  ####   ## #####   # ##### #  #     #               
              ## #### ##  ###   # ##  #    # ## #     #####   ###              
             ##  #    # ###  # ## # ####  ## #  ##   ##    # ##  #             
            ## ####  ## #  ### #  # #   ###  #### # ## #  ## # ####            
           ##  #   ###  ####   #### ## ##  ###    # #  ####  # #   #           
          ## #### ##  ###   # ##    #  # ###  #  ## ####   ### ## ###          
         ##  #    # ###  # ## # #  ##### #  ######  #   # ##   #  #  #         
        ## ####  ## #  ### #  # ####     ####     #### ## # # #########        
       ##  #   ###  ####   #### #   #   ##   #   ##    #  # # #        #       
      ## #### ##  ###   # ##    ## ### ## # ### ## #  ##### # ##      ###      
     ##  #    # ###  # ## # #  ##  #   #  # #   #  ####     # # #    ##  #     
    ## ####  ## #  ### #  # #### #### ##### ## #####   #   ## # ##  ## ####    
   ##  #   ###  ####   #### #    #    #     #  #    # ### ##  # # ###  #   #   
  ## #### ##  ###   # ##    ##  ###  ###   ######  ## #   # ### # #  #### ###  
 ##  #    # ###  # ## # #  ## ###  ###  # ##     ###  ## ## #   # ####    #  # 
## ####  ## #  ### #  # ####  #  ###  ### # #   ##  ###  #  ## ## #   #  ######
   #   ###  ####   #### #   ######  ###   # ## ## ###  ######  #  ## #####     
  ### ##  ###   # ##    ## ##     ###  # ## #  #  #  ###     ######  #    #    
 ##   # ###  # ## # #  ##  # #   ##  ### #  ##########  #   ##     ####  ###   
## # ## #  ### #  # #### ### ## ## ###   ####         #### ## #   ##   ###  #  
#  # #  ####   #### #    #   #  #  #  # ##   #       ##    #  ## ## # ##  #####
 ### ####   # ##    ##  ### ########### # # ###     ## #  #####  #  # # ###    
##   #   # ## # #  ## ###   #           # # #  #   ##  ####    ###### # #  #   
# # ### ## #  # ####  #  # ###         ## # ##### ## ###   #  ##      # ##### #
  # #   #  #### #   ###### #  #       ##  # #     #  #  # ##### #    ## #     #
### ## #####    ## ##      #####     ## ### ##   ######## #     ##  ##  ##   ##
    #  #    #  ##  # #    ##    #   ##  #   # # ##        ##   ## ### ### # ## 
   ######  ##### ### ##  ## #  ### ## #### ## # # #      ## # ##  #   #   # # #
# ##     ###     #   # ###  ####   #  #    #  # # ##    ##  # # #### ### ## # #
  # #   ##  #   ### ## #  ###   # ######  ##### # # #  ## ### # #    #   #  # #
### ## ## #### ##   #  ####  # ## #     ###     # # ####  #   # ##  ### ##### #
    #  #  #    # # #####   ### #  ##   ##  #   ## # #   #### ## # ###   #     #
#  #########  ## # #    # ##   #### # ## #### ##  # ## ##    #  # #  # ###   ##
 ###        ###  # ##  ## # # ##    # #  #    # ### #  # #  ##### #### #  # ## 
##  #      ##  ### # ###  # # # #  ## #####  ## #   #### ####     #    #### # #
  ####    ## ###   # #  ### # # ####  #    ###  ## ##    #   #   ###  ##    # #
###   #  ##  #  # ## ####   # # #   ####  ##  ###  # #  ### ### ##  ### #  ## #
   # ##### ###### #  #   # ## # ## ##   ### ###  ### ####   #   # ###   ####  #
# ## #     #      ##### ## #  # #  # # ##   #  ###   #   # ### ## #  # ##   ###
  #  ##   ###    ##     #  #### #### # # # #####  # ### ## #   #  #### # # ##  
 ##### # ##  #  ## #   #####    #    # # # #    ### #   #  ## #####    # # # # 
##     # # ######  ## ##    #  ###  ## # # ##  ##   ## #####  #    #  ## # # ##
  #   ## # #     ###  # #  #####  ###  # # # ### # ##  #    ####  #####  # # # 
 ### ##  # ##   ##  ### ####    ###  ### # # #   # # ####  ##   ###    ### # ##
 #   # ### # # ## ###   #   #  ##  ###   # # ## ## # #   ### # ##  #  ##   # # 
### ## #   # # #  #  # ### ##### ###  # ## # #  #  # ## ##   # # ###### # ## ##
    #  ## ## # ####### #   #     #  ### #  # ####### #  # # ## # #      # #  # 
   #####  #  # #       ## ###   #####   #### #       #### # #  # ##    ## #####
# ##    ###### ##     ##  #  # ##    # ##    ##     ##    # #### # #  ##  #    
# # #  ##      # #   ##
Break in line 40
Ready
So, still a ways to go.... I've had to re-arrange Page zero quite a bit and moved the CHRGET/CHRGOT routine from Page Zero to ROM... I'm not using EhBasic to intercept the IRQ/NMI routines yet either. I'm hoping to get more free time to work on this in the near future... but retirement has gotten overly busy, who knew :roll:

Re: A test program?

Posted: Thu Jun 28, 2018 2:17 pm
by BillO
The third program will not work as written for EhBASIC.

Change:
60 PRINT CHR$(32+3*(C-INT(C)<.4));

to :
60 PRINT CHR$(32-3*(C-INT(C)<.4));


The issue is that in EhBASIC logical TRUE is -1, but in most Microsoft BASICs logical TRUE is 1.

Re: A test program?

Posted: Thu Jun 28, 2018 2:19 pm
by Chromatix
Quote:
3. 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9
You can improve the formatting of this by changing "PRINT Q;" to "PRINT ;Q;".

Also note that all variables involved (including the array) take on only integer values, so if trying this in a version of BASIC that recognises the difference, declaring them as integer variables results in a big speedup with no difference in output. In BBC BASIC that means appending % to each variable name, and using "R% DIV B%" instead of "INT(R%/B%)".
Quote:
The issue is that in EhBASIC logical TRUE is -1, but in most Microsoft BASICs logical TRUE is 1.
That's also true of BBC BASIC.

Re: A test program?

Posted: Thu Jun 28, 2018 2:24 pm
by JenniferDigital
Don't forget to use WIDTH 80,<whatever width you want between commas>

Re: A test program?

Posted: Thu Jun 28, 2018 3:07 pm
by BillO
Chromatix wrote:
Quote:
The issue is that in EhBASIC logical TRUE is -1, but in most Microsoft BASICs logical TRUE is 1.
That's also true of BBC BASIC.
As I'd expect. It's MS BASIC and it's derivatives that are wonky. -1 = TRUE is used in most languages, not only the conforming BASICs.

Re: A test program?

Posted: Thu Jun 28, 2018 3:18 pm
by barrym95838
The 1 for true thing seemed to be an Applesoft deal, because MS-BASIC on the TRS-80 and C=64 used -1. Other languages that used 1 for true, off the top of my head: K&R C, VTL-2, Fig-Forth.

Mike B.

Re: A test program?

Posted: Thu Jun 28, 2018 3:45 pm
by BillO
barrym95838 wrote:
The 1 for true thing seemed to be an Applesoft deal, because MS-BASIC on the TRS-80 and C=64 used -1. Other languages that used 1 for true, off the top of my head: K&R C, VTL-2, Fig-Forth.

Mike B.
You could be right Mike. I just tried it on OSI BASIC (an MS BASIC) and it's -1.

It does indeed seem to flip around from implementation to implementation in other languages too. I was always taught to test for FALSE=0 or TRUE <> 0 as it is the only fairly consistent arithmetic value assigned to a LOGICAL value.

Re: A test program?

Posted: Thu Jun 28, 2018 4:15 pm
by Klaus2m5
TRUE = -1 seems to be the better choice because you can use a logical NOT (one“s complement) to invert it to FALSE.

Re: A test program?

Posted: Thu Jun 28, 2018 4:32 pm
by Chromatix
Indeed. In C there are separate Boolean and bitwise inversion operators, just as there are separate Boolean and bitwise AND and OR operators. BASIC only has bitwise ones, which thus have to pull double duty.