Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在fortran中以双精度进行所有计算?_Fortran_Fortran90_Gfortran - Fatal编程技术网

如何在fortran中以双精度进行所有计算?

如何在fortran中以双精度进行所有计算?,fortran,fortran90,gfortran,Fortran,Fortran90,Gfortran,在下面给出的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 doubl

在下面给出的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值。
程序findpi
隐式无
双精度::x,y,半径,truepi,cnt
双精度,可分配,维度(:)::pi,errpi
整数::种子大小,i,t,iter,j,k,n
整数,可分配,维度(:)::种子
!确定pi的真实值以与计算值进行比较
truepi=4.D0*ATAN(1.D0)
调用随机种子(大小=种子大小)
分配(种子(种子大小))
i=1,种子大小
呼叫系统时钟(t)!使用系统时钟将种子随机分配到
!随机数发生器
种子(i)=t
结束循环
调用随机种子(put=seed)
n=2000!确定pi值的次数
分配(pi(n),errpi(n))
do j=1,n
iter=n*100!随机点数
cnt=0.D0
i=1,国际热核聚变实验堆
随机呼叫号码(x)
呼叫随机号码(y)
半径=sqrt(x*x+y*y)
如果(半径<1),则
cnt=cnt+1.D0
恩迪夫
结束循环
pi(j)=(4.D0*cnt)/dble(iter)
打印*,j,pi(j)
结束循环
打开(10,file=“pi.dat”,status=“replace”)
写(10,“(F15.8,I10)”)(π(k),k,k=1,n)
调用系统(“gnuplot——persist piplot.gnuplot”)
结束程序findpi

您的计算是双精度的,但我发现两个问题:

  • 第一个是系统误差。。。你可以通过
iter
最多是2000*100,因此
1/iter
至少是
5e-6
,因此您无法解决任何问题;-)

  • 第二个问题是IO例程以单精度打印结果!线路
更具体地说,需要调整格式说明符
(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)
write(10,*) (pi(k),k,k=1,n)
write(10,"(F22.15,I10)") (pi(k),k,k=1,n)