简单的程序,只产生零,错误? [英] Simple program, that only produce zeros, bug?
本文介绍了简单的程序,只产生零,错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它似乎是它的一小部分,它混乱了。如果我设置x等于一些十进制数它可以工作。
程序主
隐式无
整型iMax,jMax
双精度,维(:, :),allocatable :: T
双精度x,dx,f,L2old,L2norm,y
整数i,j,n,bc
n = 10
分配(T(1:n + 2,1:n + 2))
T = 0.0d0
do i = 2,n + 1
do j = 2,n + 1
x =( j + 1)* 1/24
y =(i + 1)* 1/24
T(i,j)= - 18 *(x ** 2 + y ** 2)** 2
写(*,*)'T(',i,'',j,'',T(i,j)
$ b $ (*,*)'T(1,1)',T(1,1)
结束做
结束做
end program main
解决方案
x =(j + 1)* 1/24
1/24
是一个整数除法,舍入为0.您应该能够通过至少使用一个操作数浮点数
来强制进行浮点除法。例如
x =(j + 1)* 1.0 / 24.0
Why does this fortran program produce only zeros? When I print it out i get -0.00000 everywhere! What have I done wrong? In matlab it runs perfectly. I dont see any reason why its not working to be honest!
It seems like its the fraction that messes it up. if I set x equal to some decimal number it works.
program main
implicit none
integer iMax, jMax
double precision, dimension(:,:), allocatable :: T
double precision x, dx,f,L2old,L2norm,y
integer i, j,n,bc
n=10
allocate(T(1:n+2, 1:n+2))
T=0.0d0
do i=2,n+1
do j=2,n+1
x=(j+1)*1/24
y=(i+1)*1/24
T(i,j)= -18*(x**2+y**2)**2
Write(*,*)'T(',i,'',j,'', T(i,j)
end do
end do
Write(*,*)'T(1,1)',T(1,1)
end program main
解决方案
x=(j+1)*1/24
1/24
is an integer division that rounds down to 0. You should be able to force floating point division by making at least one of the operands floating point,
e.g.
x=(j+1)*1.0/24.0
这篇关于简单的程序,只产生零,错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文