简体   繁体   English

Fortran 95和Python上的相同方程式返回不同的结果

[英]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.

相关问题 为什么相同的逻辑方程会在 python 和 c++ 中返回 2 个不同的结果? - Why does the same logical equation return 2 different results in python and c++? 公式为同一变量返回不同的值 - Equation return different values for the same variable PHP和Python解压缩从同一来源返回不同的结果 - PHP and Python unpack return different results from same source 确定Python列表是否相同95%? - Determine if a Python list is 95% the same? python`list`和`for`返回不同的结果 - python `list` and `for` return different results 用Python编写的Friends-of-friends算法必须在Fortran 90/95中 - Friends-of-friends algorithm written in Python need to be in Fortran 90/95 相同的python脚本,但结果不同 - same python script, but different results 如果在本地和云上运行,`requests` 会为同一脚本返回不同的结果 - `requests` returning different results for the same script if run both locally and on the cloud Python contains 和 icontains 都返回不区分大小写的结果 - Python contains and icontains both return case insensitive results Python Scikit-LinearRegression和Ridge返回不同​​的结果 - Python Scikit - LinearRegression and Ridge return different results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM