08-16-2022, 12:30 PM
A really nice and quick one is this one:
Code: (Select All)
$Console:Only
dig& = Val(Command$)
If dig& <= 0 Then Input "how many digits? ", dig&
Print calcPI$(dig&)
Function calcPI$ (dig&)
arrSize& = (dig& + 1) Or 1: If arrSize& > 200 Then ten% = 10 Else ten% = 100: arrSize& = (arrSize& + 1) / 2
Dim POWER(arrSize&), TERM(arrSize&), RESULT(arrSize&)
For pass% = 1 To 2
For L& = 0 To arrSize&
POWER(L&) = 0: TERM(L&) = 0: If pass% = 1 Then RESULT(L&) = 0
Next L&
POWER(0) = 16 / pass% ^ 2
mode% = 0: If pass% = 1 Then divisor% = 5 Else divisor% = 239
GoSub divide
xponent% = 1: sign% = 3 - 2 * pass%
Do
For L& = 0 To arrSize&: TERM(L&) = POWER(L&): Next L& '
mode% = 1: divisor% = xponent%: GoSub divide
mode% = (sign% < 0) + Abs(sign% > 0)
carry% = 0
For l1& = arrSize& To 0 Step -1
sum% = RESULT(l1&) + TERM(l1&) * mode% + carry% * mode%: carry% = 0
If (mode% = 1) * (sum% < ten%) + (mode% = -1) * (sum% >= 0) Then
RESULT(l1&) = sum%
Else
RESULT(l1&) = sum% + mode% * -ten%: carry% = 1
End If
'Locate 20, (l1& + 1) * 2: Print _Trim$(Str$(RESULT(l1&)));: _Delay .001
Next l1&
xponent% = xponent% + 2: sign% = -sign%
mode% = 0
If pass% = 1 Then divisor% = 25 Else divisor% = 239
GoSub divide
If pass% = 2 Then GoSub divide
Loop While zero% <> 0
Next pass%
result$ = _Trim$(Str$(RESULT(0))) + "."
For L& = 1 To arrSize&
result$ = result$ + _Trim$(Str$(RESULT(L&)))
Next L&
calcPI$ = result$: Exit Function
divide:
digit% = 0: zero% = 0
For l1& = 0 To arrSize&
digit% = digit% + TERM(l1&) * mode% + POWER(l1&) - POWER(l1&) * mode%
quotient% = Int(digit% / divisor%)
residue% = digit% Mod divisor%
zero% = zero% Or (quotient% + residue%)
If mode% Then TERM(l1&) = quotient% Else POWER(l1&) = quotient%
digit% = residue% * ten%
Next l1&
mode% = 0
Return
End Function
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience