如何在 Fortran 中以双精度进行所有计算?
How do I make all the calculations in double precision in fortran?
在下面给出的Fortran代码中,我将所有涉及PI计算的数字都设为双精度,但我得到的PI值只是一个实数,最后有大量的零或9。如何让程序以双精度给出 PI?我正在使用 gfortran
编译器。
!This program determines the value of pi using Monte-Carlo algorithm.
program findpi
implicit none
double precision :: x,y,radius,truepi,cnt
double precision,allocatable,dimension(:) :: pi,errpi
integer :: seedsize,i,t,iter,j,k,n
integer,allocatable,dimension(:) :: seed
!Determining the true value of pi to compare with the calculated value
truepi=4.D0*ATAN(1.D0)
call random_seed(size=seedsize)
allocate(seed(seedsize))
do i=1,seedsize
call system_clock(t) !Using system clock to randomise the seed to
!random number generator
seed(i)=t
enddo
call random_seed(put=seed)
n=2000 !Number of times value of pi is determined
allocate(pi(n),errpi(n))
do j=1,n
iter=n*100 !Number of random points
cnt=0.D0
do i=1,iter
call random_number(x)
call random_number(y)
radius=sqrt(x*x + y*y)
if (radius < 1) then
cnt = cnt+1.D0
endif
enddo
pi(j)=(4.D0*cnt)/dble(iter)
print*, j,pi(j)
enddo
open(10,file="pi.dat",status="replace")
write(10,"(F15.8,I10)") (pi(k),k,k=1,n)
call system("gnuplot --persist piplot.gnuplot")
end program findpi
你的计算是双精度的,但我发现了两个问题:
- 第一个是系统错误...你通过
确定圆周率
pi(j)=(4.D0*cnt)/dble(iter)
iter
最多为 2000*100,因此 1/iter
至少为 5e-6
,因此您无法解析除此之外的任何查找器 ;-)
- 第二个问题是您的 IO 例程以单精度打印结果!该行
write(10,"(F15.8,I10)") (pi(k),k,k=1,n)
更具体地说,需要调整格式说明符 "(F15.8,I10)"
。目前它告诉编译器总共使用 15 个字符来打印数字,小数点后有 8 位数字。作为第一个措施,您可以使用 *
:
write(10,*) (pi(k),k,k=1,n)
这总共使用了 22 个字符,所有 15 个数字都用于双精度:
write(10,"(F22.15,I10)") (pi(k),k,k=1,n)
在下面给出的Fortran代码中,我将所有涉及PI计算的数字都设为双精度,但我得到的PI值只是一个实数,最后有大量的零或9。如何让程序以双精度给出 PI?我正在使用 gfortran
编译器。
!This program determines the value of pi using Monte-Carlo algorithm.
program findpi
implicit none
double precision :: x,y,radius,truepi,cnt
double precision,allocatable,dimension(:) :: pi,errpi
integer :: seedsize,i,t,iter,j,k,n
integer,allocatable,dimension(:) :: seed
!Determining the true value of pi to compare with the calculated value
truepi=4.D0*ATAN(1.D0)
call random_seed(size=seedsize)
allocate(seed(seedsize))
do i=1,seedsize
call system_clock(t) !Using system clock to randomise the seed to
!random number generator
seed(i)=t
enddo
call random_seed(put=seed)
n=2000 !Number of times value of pi is determined
allocate(pi(n),errpi(n))
do j=1,n
iter=n*100 !Number of random points
cnt=0.D0
do i=1,iter
call random_number(x)
call random_number(y)
radius=sqrt(x*x + y*y)
if (radius < 1) then
cnt = cnt+1.D0
endif
enddo
pi(j)=(4.D0*cnt)/dble(iter)
print*, j,pi(j)
enddo
open(10,file="pi.dat",status="replace")
write(10,"(F15.8,I10)") (pi(k),k,k=1,n)
call system("gnuplot --persist piplot.gnuplot")
end program findpi
你的计算是双精度的,但我发现了两个问题:
- 第一个是系统错误...你通过 确定圆周率
pi(j)=(4.D0*cnt)/dble(iter)
iter
最多为 2000*100,因此 1/iter
至少为 5e-6
,因此您无法解析除此之外的任何查找器 ;-)
- 第二个问题是您的 IO 例程以单精度打印结果!该行
write(10,"(F15.8,I10)") (pi(k),k,k=1,n)
更具体地说,需要调整格式说明符 "(F15.8,I10)"
。目前它告诉编译器总共使用 15 个字符来打印数字,小数点后有 8 位数字。作为第一个措施,您可以使用 *
:
write(10,*) (pi(k),k,k=1,n)
这总共使用了 22 个字符,所有 15 个数字都用于双精度:
write(10,"(F22.15,I10)") (pi(k),k,k=1,n)