[英]Same equation on both Fortran 95 and Python return different results
There is a equation in Fortran 95 to calculate some position for an array, I copied and pasted the same equation in Python, but they return different results. Fortran 95中有一个方程式可以计算数组的某个位置,我在Python中复制并粘贴了相同的方程式,但是它们返回的结果不同。
EDIT: on the rush for an answer, I forgot to show the declarations of the variables, but they are in the Fortran code example now. 编辑:急于寻求答案,我忘了显示变量的声明,但是它们现在在Fortran代码示例中。 And it turns out the declaration was the problem, thanks to @SurestTexas and @albert for pointing it out in the comments, and everyone else who helped. 事实证明,声明是问题所在,这要归功于@SurestTexas和@albert在评论中指出这一点,以及其他提供帮助的人。
The equation in Fortran: Fortran中的方程式:
integer(2) :: i, j
integer(4) :: e, n_x
n_x = 1162
j = ((-2.8 - (-8.4)) / 0.05) + 1
i = ((-4.5 - (-5.1)) / 0.05) + 1
e = ((i-1)*n_x+j)
I print e
, which results in: 12894
我打印e
,结果是: 12894
And in Python: 在Python中:
n_x = 1162
j = ((-2.8 - (-8.4)) / 0.05) + 1
i = ((-4.5 - (-5.1)) / 0.05) + 1
e = ((i-1)*n_x+j)
I print e
, which results in: 14057.0
我打印e
,结果为: 14057.0
As you can see, they are exactly the same, I can't find out what is wrong and how I can solve the issue, please help me. 如您所见,它们是完全一样的,我无法找出问题所在以及如何解决该问题,请帮助我。
Remembering my FORTRAN. 记住我的FORTRAN。 I think it assumes datatype based on the first letter of the variable, in partilar i and j would be integers So to simulate this in Python I did: 我认为它假定数据类型基于变量的第一个字母,特别是i和j将是整数,因此在Python中模拟此操作:
n_x = 1162
j = int(((-2.8 - (-8.4)) / 0.05) + 1)
i = int(((-4.5 - (-5.1)) / 0.05) + 1 )
e = ((i-1)*n_x+j)
Which gave me 12895 这给了我12895
supplement: 补充:
Interesting that in Python 3.5.3, e is printed as 14056.99999999999. 有趣的是,在Python 3.5.3中,e打印为14056.99999999999。
The implicit none statement is used to inhibit a very old feature of Fortran that by default treats all variables that start with the letters i, j, k, l, m and n as integers and all other variables as real arguments. 隐式none语句用于禁止Fortran的一项非常老的功能,该功能默认情况下会将以字母i,j,k,l,m和n开头的所有变量视为整数,并将所有其他变量视为实参。
I can't comment due to insufficient reputation, so put it in Answer as a record of my research. 由于声誉不足,我无法发表评论,因此请将其放在“答案”中作为我的研究记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.