Fortran只给出第一个任期的结果? [英] Fortran only give outcome of the first term?
本文介绍了Fortran只给出第一个任期的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正要计算 cos(x)+ 1/4 * cos(2x)
,但结果总是只给出<$ c $的结果C> COS(x)的。
程序写入
隐式无
整型,参数:: N = 8
integer :: j
real :: h,L
real,dimension(0:N-1):: x,fx
real(8),参数: :pi = 4.0_8 * atan(1.0_8)
L = 2 * pi
h = L / N
do j = 0,N-1
x(j )= cos(x(j))+ 1/4 * cos(2 * x(j)= h * j
end do
do j = 0,N-1
fx j))
end do
write(*,*),fx
结束程序写入
解决方案
你的问题是 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
。
I was about to calculate the cos(x)+1/4*cos(2x)
, however the result is always giving me outcome of only cos(x)
. Where is the error in my code?
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
解决方案
Your problem is the 1/4
.
Since both 1
and 4
are integer, 1/4
is interpreted as an integer division, and any reminder is dropped. In short: 1/4 == 0
, whereas 1.0/4 == 1/4.0 == real(1)/4 == 0.25
.
Note that real(1/4) == real(0) == 0.0
.
这篇关于Fortran只给出第一个任期的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文