0

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.

i'mlaguiar
  • 21
  • 2
  • 7
  • 1
    have you tried printing the results of each part of the operation to determine the problem? If so, you have not mentioned it (or any effort to solve this on your own). –  May 22 '18 at 13:20
  • 3
    What are the data types of the variables used in Fortran, I think i, j are integers. Always use `IMPLICIT NONE` in your Fortran code. When posting make a MWE so we can reproduce your results. – albert May 22 '18 at 13:20
  • 1
    It may be your division in python. See https://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-division-keeps-rounding-down-to-0 –  May 22 '18 at 13:22
  • @SurestTexas I tried, even they return different results. As albert mentioned, i and j are integers, I guess that is the problem, I think I can solve it based on the the tips you and albert gave me. Thank you. – i'mlaguiar May 22 '18 at 13:31
  • Thank you, @albert. – i'mlaguiar May 22 '18 at 13:32
  • When you post a code here, make it a [mcve]. Namely, show the declarations of all your variables and `implicit none`. Please [edit] your question to include the declarations. – Vladimir F Героям слава May 22 '18 at 13:44
  • @VladimirF not everyone uses implicit none! Assumptions: make ass out of U and me (well not me in this case0) – Paula Thomas May 22 '18 at 13:48
  • If they don't use them in their code, so be it, but they **really should** use it when posing a problem to Stack Overflow, especially in simple cases like this one. It can easily be a reason to downvote a question, although I do not do that, when it is missing without any good reason especially when it causes problems. – Vladimir F Героям слава May 22 '18 at 13:55
  • @VladimirF why should they mislead us like that. It might just, as it did here, be crack the case. – Paula Thomas May 22 '18 at 14:18
  • @PaulaThomas They would,hopefully, find their own error (and errors, in the future, in their far larger code) and learn more from it. – albert May 22 '18 at 14:30
  • @albert but if they put implicit:none in here and don't use it in the code they ran that is misleading, no? – Paula Thomas May 22 '18 at 14:36
  • @PaulaThomas Well they at least are pointed in the right direction and if they don't use the sensible hint about `IMPLICIT NONE` it is their own fault (at work at the beginning people din't like it either when I forced to use it, by means of a compiler option, but after a little while they were used to it and the people where happy with and some errors were caught). – albert May 22 '18 at 15:19

2 Answers2

2

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

Paula Thomas
  • 1,152
  • 1
  • 8
  • 13
2

supplement:

Interesting that in Python 3.5.3, e is printed as 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.

I can't comment due to insufficient reputation, so put it in Answer as a record of my research.

marya
  • 94
  • 6