Question

In the Fortran95 code below the variable NMOM is an integer and always equal to 3.

What would the value of P0 evaluate to, 1 or -1? The divide by 2 then multiply by 2 bit has me confused, I'm not sure why you would do this, but this was written by a non-programmer scientist back in the 90s who is no longer around to ask.

P0=1
IF(NMOM-NMOM/2*2.EQ.1)P0=-1

The code is compiled with lf95 on a Linux machine.

Was it helpful?

Solution

I don't know Fortran, but my guess is that it is testing if NMOM is odd or even. First, rewrite with parens to simulate operator precedence:

IF(NMOM-((NMOM/2)*2).EQ.1)P0=-1

And if we are using integer math then (NMOM/2)*2 == NMOM if it's even else it will equal NMOM-1 if it's odd.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top