Pi Calc translation

Rosetta Code had a Basic256 program translated from a Pascal program to Calculate PI.
It was fairly easy to translate to SmallBASIC.

The translation was inspired by Anthony at The QB64 Edition wanting help to build a QB64 version for testing times.


' 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


Alas, testing for 100, somewhere about 75th decimal places (according to:
http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html
it shows wrong digits!

Somewhat better may be: this one (adapted from Rosetta code for BBB-Basic:

m=400:dim b(m)
for i=0 to m:b(i)=20:next
e=0:l=2
for c=m to 14 step -7
d=0:a=c*2-1
for p=c to 1 step -1
d=d*p+b(p)*100
b(p)=d%a:d\=a:a-=2
next
if d=99:e=e*100+d:l+=2
elif c=m:?(d\100)/10;:e=d%100
else:?right(string(l,"0")+str(e+d\100),l);:e=d%100:l=2:fi
next

Cheers!

PS. curious enough, for 200, the discrepancies begin about 150th...

The BBB-Basic? not BBC? code is briefer, and more accurate? you say.

I knew I should have checked the output digits more carefully and will, now! Thanks for heads up.

Frankly, I don't see a need for more than, what? 25 digits, unless making a Random Number Generator?

hmm... I think that's it?

Counting decimal as a place in string mismatches begin at 78th place:

My code and reference source for check:

' Pi check.bas SmallBASIC 0.12.9 (B+=MGA) 2017-05-20

'reference file, I copy/pasted 10976 digits from
' http://www.piday.org/million/
tload "piday-org-million.txt", piRef, 1
'? len(piRef)
'? right(piRef, 4) '5123 is what my file says too
tload "piCalc.txt", piCheck, 1
N = 100 + 1
? "Digit place of mismatch between reference string and calc string."
for
i = 1 to N
print i, mid(piRef, i, 1), mid(piCheck, i, 1);
if mid(piRef, i, 1) <> mid(piCheck, i, 1) then ? " mismatch" else ?
next
?:? "Done comparing PI strings."
' ha there is a problem testing 16 + 1 digits too on 15th that disappears in 100 + 1 test
pause


Thanks again jsalai!

BBC-Basic. of course (a typo)...
In illo tempore, for numerical calculations I used UBASIC by Yuji Kida. It gave up to 2600 digits of PI immediately.

Now, making complex calculations with help of my 10 years young granddaughter, I hardly need moe than 3.

Cheers

Aha! for 1000 digits, errors DO NOT ARRIVE until first 755 digits compared correctly!!!

So, the algorithm's first errors occur approx 75% of number of digits being calculated.
(75% Just a shot in dark guess based on too small a sample for predicting trends, never-the-less I do predict trend!)


' Pi check.bas SmallBASIC 0.12.9 (B+=MGA) 2017-05-20

'reference file, I copy/pasted 10976 digits from
' http://www.piday.org/million/
tload "piday-org-million.txt", piRef, 1
'? len(piRef)
'? right(piRef, 4) '5123 is what my file says too
tload "piCalc.txt", piCheck, 1
N = 1000 + 1
? "Digit place of mismatch between reference string and calc string."
for
i = 1 to N
'print i, mid(piRef, i, 1), mid(piCheck, i, 1);
if mid(piRef, i, 1) <> mid(piCheck, i, 1) then
? "Mismatch at place ";i
c++
if c > 20 then ? "More than 20 errors, done!" : pause : end
end if
next
?:? "Done comparing PI strings."
' no problem with 16 digits
pause