(old) Pi Calculation program
#1
Here is something I did with / for Anthony but never got around to posting the SB code here:
Code:
' pi calc.bas SmallBASIC 0.12.9 (B+=MGA) 2017-05-18
' I will attempt to translate 256 version to SmallBASIC
' all comments mine except with symbols noted below
' I added a title and a ruler to test digits length

'===========================================================
' Source:
' Rosetta Code - Pi
' BASIC256 sample
' noted at RC: Translation of Pascal,
' Originally published by Stanley Rabinowitz

' translator to 256 not mentioned note symbols:
'     # 256 comment
' { original comment from Pascal }
'==========================================================
' 256 to SmallBASIC notes
' len is not a 256 keyword???
' dim array with () but refer to values of array with [] ?
' I am hoping % stands for MOD and is same as SB
' I am hoping \ stands for integer division same as SB
' True = 1 False = 0 same as SB but made all caps here
' for some reason the decimal is not printing (fixed)

n = 16                       ' first test
n = 100                      ' with max ruler
n = 10000                    ' bigger array

? "Pi to ";n;" digits is: "   ' title

' Honestly,  I have no idea what this is about, just a copy translate

len_ = 10 * n \ 4   ' len is SB keyword using len_ (learned tip at FB)

needDecimal = TRUE  ' variable camel, make constants all caps

dim a(len_)         ' OK this version uses an array to store values
                   'so can't go on and on and on like the bunny
       
nines = 0
predigit = 0        ' {First predigit is a 0}

for j = 1 to len_
  a(j-1) = 2        '    # {Start with 2s}
next j

for j = 1 to n      
  q = 0
  for i = len_ to 1 step -1
     '#  {Work backwards}
     x = 10 * a(i - 1) + q * i
     a(i - 1) = x % (2 * i - 1)
     q = x \ (2 * i - 1)
  next i
  a(0) = q % 10
  q = q \ 10
  if q = 9 then
     nines = nines + 1
  else
     if q = 10 then
        d = predigit + 1 : gosub outputd
        if nines > 0 then
           for k = 1 to nines
              d =  0 : gosub outputd
           next k
        end if
        predigit = 0
        nines = 0
     else
        d = predigit : gosub outputd
        predigit = q
        if nines <> 0 then
           for k = 1 to nines
              d =  9 : gosub outputd
           next k
           nines = 0
        end if
     end if
  end if
next j
print predigit       ' gets last digit output

if n <= 100 then
 for i = 1 to n       ' oh let's make a ruler to test how many digits!
   if i = 1 then ? "1."; else ? i mod 10;
 next
 ?
end if
pause
end

label outputd
if needDecimal then
 if d = 0 then return   ' oh, this really is needed
 print d;".";           ' print d + "." wasn't working
 needDecimal = FALSE
else
  print d;
end if
return


Damn this forum editor is something else! Yeah, I know, could be the user too! 
OK try a paste in from the code pasted into SB editor, there that's better!

Well let's check if a copy / paste of a copy / paste from:
https://smallbasic.sourceforge.io/?q=node/1719

stills works?  yes the link works where it is explained that only the first 75% of the digits can be trusted.
From tests, of shorter version 16, 100, 1000... digits 75% are correct.

So for this 10000 digit run trust only first 7,500. I also have code at link for checking your digits with a million digit one found on Internet (see details at SB link).

Also a snapshot of 100 digit run that has a "digit ruler" underneath it to locate digits, around the 75th things should go bad.


Attached Files Thumbnail(s)

B += x
Reply