Anonymous user
Arithmetic-geometric mean/Calculate Pi: Difference between revisions
Arithmetic-geometric mean/Calculate Pi (view source)
Revision as of 15:02, 13 July 2022
, 1 year agoUpdated to F2008 and used iso_fortran_env, as real(16) is deprecated
m (→{{header|J}}) |
(Updated to F2008 and used iso_fortran_env, as real(16) is deprecated) |
||
Line 519:
=={{header|Fortran}}==
{{works with|Fortran|
{{libheader|iso_fortran_env}}
{{trans|Julia}}
<lang fortran>program CalcPi
! Use real128 numbers: (append '_rf')
use iso_fortran_env, only: rf => real128
implicit none
real(
real(
integer :: k,k1,i
old_pi = 0.
a = 1.
do i=1,100
call approx_pi_step(a,g,s,k,a1,g1,s1,k1)
new_pi = 4.0_rf * (a1**2.0_rf) / (1.0_rf - s1)
if (abs(new_pi - old_pi).lt.(2.0_rf*epsilon(new_pi))) then
! If the difference between the newly and previously
! calculated pi is negligible, stop the calculations
Line 546 ⟶ 549:
subroutine approx_pi_step(x,y,z,n,a,g,s,k)
real(
integer, intent(in) :: n
real(
integer, intent(out) :: k
a = 0.
g = sqrt(x*y)
k = n + 1
s = z + (2.0_rf)**(real(k)+1.0_rf) * (a**(2.0_rf) - g**(2.0_rf))
end subroutine
end program CalcPi
Line 560 ⟶ 563:
{{out}}
<pre>
Iteration: 1 Diff: 3.
Iteration: 2 Diff: 4.
Iteration: 3 Diff: 8.
Iteration: 4 Diff: 3.
Iteration: 5 Diff: 3.
</pre>
|