RosettaCodeData/Task/Pi/M2000-Interpreter/pi.m2000

96 lines
3.4 KiB
Plaintext

Module Checkpi {
Module FindPi(Digits){
Digits++
n=Int(3.32*Digits)
PlusOne=Lambda N=0% -> {
=N
N++
}
PlusTwo=Lambda N=1% -> {
=N
N+=2
}
Dim A(n)<<PlusOne(), B(n)<<PlusTwo()
Dim Ten(n), CarrierOver(n), Sum(n),Remainder(n)=2
OutPutDigits=Digits
Predigits=Stack
CallBack=lambda fl=true, Chars=0 (x)->{
Print x;
Chars++
If fl then Print "." : Print " "; : fl=false : Chars=0 : exit
If Chars=50 then {
Print
Print " ";
Chars=0
Refresh
} else.if (Chars mod 5)=0 then {
Print " ";
Refresh
}
\\ explicitly refresh output layer, using Fast ! mode of speed
}
Print "Pi=";
While Digits {
NextDigit(&CallBack, &Digits)
}
print
Refresh
Sub NextDigit(&f, &D)
CarrierOver=0
For k=n-1 to 1 {
Ten(k)=Remainder(k)*10%
CarrierOver(k)=CarrierOver
Sum(k)=Ten(k)+CarrierOver(k)
q=Sum(k) div B(k)
Remainder(k)=Sum(k)-B(k)*q
CarrierOver=A(k)*q
}
Ten(0)=Remainder(0)*10%
CarrierOver(0)=CarrierOver
Sum(0)=Ten(0)+CarrierOver(0)
q=Sum(0) div 10%
Remainder(0)=Sum(0)-10%*q
if q<>9 and q<>10 then {
Stack Predigits {
While not empty {
Call f(Number)
if D>0 then D--
If D=0 then flush ' empty stack
}
Push q
}
} else.if q=9 Then {
Stack Predigits { Data q }
} else {
Stack Predigits {
While not empty {
Call f((Number+1) mod 10)
if D>0 then D--
If D=0 then flush ' empty stack
}
Push 0
}
}
End Sub
}
\\ reduce time to share with OS
\\ Need explicitly use of refresh output layer (M2000 console)
\\ Slow for a screen refresh per statement and give more time to OS
Rem Set Slow
\\ Fast is normal screen refresh, per Refresh time, and give standard time to OS
Rem Set Fast
\\ Fast ! use Refresh for screen refresh, and give less time o OS than standard
\\ Esc key work when Refresh executed (and OS get little time)
Set Fast !
FindPi 4
FindPi 28
Print Pi ' pi in M2000 is Decimal type with 29 digits (1 plus 28 after dot, is same as FindPi 28)
Refresh
FindPi 50
}
Flush ' empty stack of values
CheckPi
List ' no variables exist
Modules ? ' current module exist
Stack ' Stack of values ' has to be empty, we didn't use current stack for values.