问题描述
我正在写出数组的元素如下:
I'm writing out the elements of an array as follows:
write(6,'(i4,200(1x,e15.7))')Jtot0, (a*PJjv(i,Jtot0,j,iv),i=1,nenerdif,100)
其中 a
是一个常数.但是,当此常数等于 1/2 或 1/3 时,输出为零,如果等于 1,则一切正常.数组元素是real*8
.
where a
is a constant. However, when this constant is equal to 1/2 or 1/3 the output is zeros, and if it's equal to 1, every thing goes well. The array elements are real*8
.
如果我有义务乘以 1/3 倍,我该如何克服这个问题?
How can I overcome this, giving that I'm obligated to multiply by a factor of 1/3?
推荐答案
在 Fortran 中 1/2
是一个整数除法运算,在这种情况下会向下舍入为 0代码>.
1/3
也一样.如果你想要一个真正的结果,做一个真正的除法运算,比如1.0/2.0
.注意将1/2
的结果赋值给一个实数变量会将实数变量设置为0.0
,也就是整数除法会得到0
并且接下来发生的赋值将该值转换为最接近的实数表示.
In Fortran 1/2
is an integer division operation which will round down to, in this case, 0
. Same for 1/3
. If you want a real result, do a real division operation, such as 1.0/2.0
. Note that assigning the result of 1/2
to a real variable will set the real variable to 0.0
, that is the integer division will result in 0
and the assignment, which happens next, will cast that value to its nearest real representation.
这种产生整数结果的整数除法业务在编程语言中非常普遍.
This business of integer division producing integer results is very common in programming languages.
这篇关于为什么数组的元素在乘以 1/2 或 1/3 时会被格式化为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!