[英]integer, do loop, fortran, error
I have the following fortran code defined under. 我在下面定义了以下fortran代码。 I am trying to change the length of the do loop if i change the value of n.
如果我更改n的值,我试图更改do循环的长度。 When i try to compile i get the error:
当我尝试编译时出现错误:
'a' argument of 'floor' intrinsic at (1) must be REAL. (1)固有的'floor'自变量的'a'参数必须为REAL。 But when i change q and w to be defined as real i get another error message.
但是,当我将q和w更改为实数时,会收到另一条错误消息。 How can i fix this?
我怎样才能解决这个问题? q and w is clearly a integer when i use floor(...)
当我使用floor(...)时q和w显然是整数
subroutine boundrycon(n,bc,u,v)
!input
integer :: n,bc
!output
real(8) :: u(n+2,n+2), v(n+2,n+2)
!lokale
integer :: j,i,w,q
n=30
q=floor(n/2)
w=(floor(n/2)+floor(n/6))
do j=q,w
u(q,j)=0.0;
v(q+1,j)=-v(q,j);
u(w,j)=0.0;
v(w+1,j)=-v(w,j);
end do
do i=q,w
v(i,q)=0.0;
u(i,q)=-u(i,q+1);
u(i,w+1)=-u(i,w);
v(i,w)=0;
end do
end subroutine boundrycon
Many people have already pointed this out in the comments to your question, but here it is again as an answer: 许多人已经在对您的问题的评论中指出了这一点,但在此再次作为答案:
In Fortran, if you do a division of two integer values, the result is an integer value. 在Fortran中,如果将两个整数值相除,则结果是一个整数值。
6/3 = 2
If the numerator is not evenly divisible by the denominator, then the remainder is dropped: 如果分子不能被分母均分,则余数将被丢弃:
7/3 = 2
Let's look at your code: 让我们看看您的代码:
q=floor(n/2)
It first evaluates n/2
which, since both n
and 2
are integers, is such an integer division. 首先评估
n/2
,由于n
和2
均为整数,因此为整数除法。 As mentioned before, this result is an integer. 如前所述,该结果是整数。
This integer is then passed as argument to floor
. 然后将此整数作为参数传递给
floor
。 But floor
expects a floating point variable (or, as Fortran calls it: REAL
). 但是
floor
需要一个浮点变量(或者,如Fortran所说的: REAL
)。 Hence the error message: 因此,错误消息:
"[The] argument of floor
... must be REAL
." “
floor
...的参数必须是REAL
。”
So, the easiest way to get what you want is to just remove the floor
altogether, since the integer division does exactly what you want: 因此,最简单的方法就是完全删除
floor
,因为整数除法恰好满足您的要求:
q = n/2 ! Integer Division
If you need to make a floating point division, that is if you want two integer variables to divide into a real variable, you have to convert at least one of them to floating point before the division: 如果需要进行浮点除法,即要将两个整数变量划分为实数,则必须在除法之前将其中至少一个转换为浮点:
print *, 3/2 ! wrong, prints 1
print *, real(3)/2 ! right
print *, 3/2.0 ! right
print *, (3 * 1.0) / 2 ! right
print *, real(3/2) ! wrong, prints 1.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.