Reputation: 21
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.
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. 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.
The equation in 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
And in 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
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.
Upvotes: 0
Views: 273
Reputation: 94
supplement:
Interesting that in Python 3.5.3, e is printed as 14056.99999999999.
I can't comment due to insufficient reputation, so put it in Answer as a record of my research.
Upvotes: 2
Reputation: 1190
Remembering my 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:
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
Upvotes: 2