-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathest_pi.mpl
29 lines (28 loc) · 1.03 KB
/
est_pi.mpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
est_pi := proc(num_steps::integer)
local step, s, x, mypi, i, start_time, stop_time:
printf(" Estimating Pi using: %1d slices\n", num_steps):
start_time := time[real]():
step := 1.0/num_steps:
s:=0:
for i from 1 to num_steps do
x:=(i-0.5)*step:
s:=s+(4.0/(1.0 + x*x)):
end do:
mypi:=s*step:
stop_time := time[real]():
printf("Estimated value of Pi: %.16g\n", mypi):
printf(" Elapsed time(s): %.16g\n", stop_time - start_time):
printf(" Observed difference: %.16g\n", abs(Pi - mypi)):
mypi;
end proc:
native_est_pi := proc(num_steps::integer)
local step, mypi, k, start_time, stop_time:
printf(" Estimating Pi using: %1d slices\n", num_steps):
start_time := time[real]():
mypi:=Re(sum((4/(1+(((k-0.5)/num_steps)^2))),k=1..num_steps)/num_steps):
stop_time := time[real]():
printf("Estimated value of Pi: %.16g\n", mypi):
printf(" Elapsed time(s): %.16g\n", stop_time - start_time):
printf(" Observed difference: %.16g\n", abs(Pi - mypi)):
mypi;
end proc: