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
Fortran只给出第一个术语的结果?_Fortran - Fatal编程技术网

Fortran只给出第一个术语的结果?

Fortran只给出第一个术语的结果?,fortran,Fortran,我正要计算cos(x)+1/4*cos(2x),但是结果总是只给出cos(x)。我的代码中的错误在哪里 program write implicit none integer, parameter :: N=8 integer :: j real :: h, L real, dimension(0:N-1) ::x, fx real(8), parameter :: pi=4.0_8*atan(1.0_8) L=2*pi h=L/N do j=0,N-1

我正要计算cos(x)+1/4*cos(2x),但是结果总是只给出
cos(x)
。我的代码中的错误在哪里

  program write
  implicit none
  integer, parameter :: N=8
  integer :: j
  real :: h, L
  real, dimension(0:N-1) ::x, fx
  real(8), parameter :: pi=4.0_8*atan(1.0_8)
  L=2*pi

  h=L/N
  do j=0,N-1
     x(j)=h*j
  end do
  do j=0,N-1
     fx(j)=cos(x(j))+1/4*cos(2*x(j))
  end do

  write(*,*),fx


  end program write

您的问题是
1/4

由于
1
4
都是整数,
1/4
被解释为整数除法,任何提示都会被删除。简而言之:
1/4==0
,而
1.0/4==1/4.0==real(1)/4==0.25


请注意,
real(1/4)==real(0)==0.0
您的问题是
1/4

由于
1
4
都是整数,
1/4
被解释为整数除法,任何提示都会被删除。简而言之:
1/4==0
,而
1.0/4==1/4.0==real(1)/4==0.25


请注意,
real(1/4)==real(0)==0.0

real(8)
很难看,不便于携带。相反,可以使用
ISO_Fortran_env
中的名称常量或使用
selected_real_kind
以可移植的方式控制精度。语句
1/4*cos(2*x(j))
有问题。请阅读1/4是整数除法结果0
real(8)
丑陋且不可移植。相反,可以使用
ISO_Fortran_env
中的名称常量或使用
selected_real_kind
以可移植的方式控制精度。语句
1/4*cos(2*x(j))
有问题。请阅读1/4是整数除法的结果,非常感谢!非常感谢你!