Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program to calculate pi
#9
I KNEW I had an old Pi around here somewhere.  I could smell it.
This was originally written for the TRS-80.  I recently stumbled over it on one of my old disk images.

No extended-precision tricks here.  This program just closes in on Pi to the limits of double-precision accuracy, which was slow enough on 8-bit machines.


This first version uses Basic's built-in SQR function:
Code: (Select All)
1 'Originally written for TRS-80 Model III Basic; spaces added for later Basics.
10 DefDbl A-Z
20 PI = 2
30 S = Sqr(S + 2): P = PI: PI = 2 * P / S
35 Print P; Tab(32); PI
40 If P <> PI Then 30
50 Print "PI ="; PI
Okay, THAT version of the program is good enough for all of the PC Basics that I've tried it on (QB64PE & FB under Windoze, and QBasic, GWBasic, & Turbo Basic under DOS).


Buuut: the TRS-80's SQR only calculated to single-precision accuracy, so here's a workaround for that:
Code: (Select All)
1 'Originally written for TRS-80 Model III Basic; spaces added for later Basics.
10 DefDbl A-Z
20 PI = 2: E = .000000000000001
30 N = R + 2: GoSub 100: PP = PI: PI = PI * 2 / R
35 Print PP; Tab(32); PI
40 If Abs(PP - PI) > E Then 30
50 Print "PI ="; PI
60 End

100 A = 0: B = N
120 S = (B - A) / 10
130 If Abs(A * B - N) < E Then R = (A + B) / 2: Return
140 If Abs(A * A - N) < E Then R = A: Return
150 If Abs(B * B - N) < E Then R = B: Return
160 I = A
170 If N < I * I Then B = I: A = I - S: GoTo 120
180 I = I + S: If I > B Then B = I + S: GoTo 120
190 GoTo 170
300 'Lines 100-190 form a more accurate square root subroutine.

301 'The custom square-root subroutine was for the TRS-80, to improve accuracy.

302 'It is not needed for QB64PE, FB, QBasic, GWBasic, or Turbo Basic,
303 'where it actually ruins the accuracy of the last digit of Pi.
304 'The Basics listed above can use the built-in SQR function and nail
305 'every single digit under double-precision.
If you actually run this version on any of the Basics listed above, you will see that this home-brewed SQR function falls a bit short and the last calculated digit of Pi is wrong.  ¯\_(ツ)_/¯


I haven't tried compiling these programs in BasCom, nor shall I.  After spending a couple hours one day tracking down info on the goofy long-forgotten command-line switches for that compiler & linker, I'm sick of early MS dev tools now.
Reply


Messages In This Thread
Program to calculate pi - by Kernelpanic - 02-26-2023, 10:18 PM
RE: Program to calculate pi - by david_uwi - 03-04-2023, 07:29 PM
RE: Program to calculate pi - by Kernelpanic - 03-04-2023, 10:51 PM
RE: Program to calculate pi - by bplus - 03-04-2023, 11:43 PM
RE: Program to calculate pi - by RokCoder - 03-06-2023, 12:04 PM
RE: Program to calculate pi - by BSpinoza - 03-06-2023, 05:05 PM
RE: Program to calculate pi - by bplus - 03-06-2023, 06:18 PM
RE: Program to calculate pi - by RokCoder - 03-06-2023, 07:29 PM
RE: Program to calculate pi - by JRace - 03-07-2023, 12:34 AM



Users browsing this thread: 8 Guest(s)