6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu Nov 21, 2024 1:02 pm

All times are UTC




Post new topic Reply to topic  [ 53 posts ]  Go to page Previous  1, 2, 3, 4  Next
Author Message
 Post subject: Re: A test program?
PostPosted: Mon Jun 25, 2018 12:58 pm 
Offline
User avatar

Joined: Fri Dec 12, 2008 10:40 pm
Posts: 1007
Location: Canada
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

_________________
Bill


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Mon Jun 25, 2018 3:10 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Mon Jun 25, 2018 8:23 pm 
Offline

Joined: Mon May 21, 2018 8:09 pm
Posts: 1462
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Mon Jun 25, 2018 8:27 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
It would be amusing to change the loop from
20 FOR I=1 TO 5
to
20 FOR I=1 TO 3.14


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Tue Jun 26, 2018 12:53 am 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Tue Jun 26, 2018 8:16 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
Oh yes, EXP(1), I like that!


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Thu Jun 28, 2018 1:07 pm 
Offline
User avatar

Joined: Tue Mar 05, 2013 4:31 am
Posts: 1385
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:
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:
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:
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:
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:

_________________
Regards, KM
https://github.com/floobydust


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Thu Jun 28, 2018 2:17 pm 
Offline
User avatar

Joined: Fri Dec 12, 2008 10:40 pm
Posts: 1007
Location: Canada
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.

_________________
Bill


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Thu Jun 28, 2018 2:19 pm 
Offline

Joined: Mon May 21, 2018 8:09 pm
Posts: 1462
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Thu Jun 28, 2018 2:24 pm 
Offline

Joined: Mon May 25, 2015 1:12 pm
Posts: 92
Don't forget to use WIDTH 80,<whatever width you want between commas>


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Thu Jun 28, 2018 3:07 pm 
Offline
User avatar

Joined: Fri Dec 12, 2008 10:40 pm
Posts: 1007
Location: Canada
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.

_________________
Bill


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Thu Jun 28, 2018 3:18 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Thu Jun 28, 2018 3:45 pm 
Offline
User avatar

Joined: Fri Dec 12, 2008 10:40 pm
Posts: 1007
Location: Canada
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.

_________________
Bill


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Thu Jun 28, 2018 4:15 pm 
Offline

Joined: Sat Jul 28, 2012 11:41 am
Posts: 442
Location: Wiesbaden, Germany
TRUE = -1 seems to be the better choice because you can use a logical NOT (one´s complement) to invert it to FALSE.

_________________
6502 sources on GitHub: https://github.com/Klaus2m5


Top
 Profile  
Reply with quote  
 Post subject: Re: A test program?
PostPosted: Thu Jun 28, 2018 4:32 pm 
Offline

Joined: Mon May 21, 2018 8:09 pm
Posts: 1462
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 53 posts ]  Go to page Previous  1, 2, 3, 4  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: