4

I am trying to compile a piece of software written in Fortran 77. I should point out that I don't know much at all about Fortran, and would really rather not start modifying the code for this software - particularly as I'm not sure what the licensing of the software is, and I don't know if I would be able to redistribute my modified version.

The code compiles fine on OS X and Windows using the g77 compiler that is (fairly easily) available for these systems. However, I cannot get it to work on my Ubuntu distribution, as I can't seem to get hold of g77 for Ubuntu anymore, and if I try and install an old version of it, it seems to muck up my entire GCC installation. I have tried compiling the code with both gfortran and g95, but it doesn't work with either as:

  • The code uses real variables as loop indices (yes, I know, bad idea). g95 supports this with the -freal-loops option, but gfortran doesn't.
  • The code uses real variables to index into arrays, which gfortran will support (with a warning), but g95 won't support.

Can anyone suggest a way to compile this code with those two 'dodgy' features using a modern and easily-available compiler such as g95 or gfortran?

robintw
  • 27,571
  • 51
  • 138
  • 205
  • 3
    You could try adding `-std=legacy`, but I don't know if it will work. See [this](http://gcc.gnu.org/ml/fortran/2005-05/msg00371.html) for more details. – Dan Nov 15 '12 at 21:24
  • 1
    I tried -std=legacy with gfortran, as suggested by Dan. Using a real variable as loop index and as an array index was accepted without error messages and the program worked. – M. S. B. Nov 15 '12 at 23:49
  • Why would you try to redistribute code that you admit that you did not write and do not have a license for said code? – Michael McGuire Nov 16 '12 at 01:13
  • Actually, using `real` or `double precision` variables as loop indices and subscripts is completely safe. Yes, I know that many people will say this is a terrible idea but they are wrong. FP calculations on integer values are exact. People who are aware that decimal fractions are usually imprecise without understanding exactly why often simply assume incorrectly that the same issue can arise with integral values. (But they have to have never been fractional.) The JavaScript implementors knew this and used `double` for integers. See http://stackoverflow.com/a/9650037/140740 – DigitalRoss Nov 16 '12 at 01:25
  • This is true on IEEE fp model. But is it treue generally. Also I have seen noninteger loop step couple of times in my students' programs, they are not safe at all. – Vladimir F Героям слава Nov 16 '12 at 07:59
  • @Dan: Thanks, adding -std=legacy worked, once I told it to also treat the code as fixed-form but with no character limit. – robintw Nov 16 '12 at 08:13
  • @MichaelMcGuire: I'm not trying to redistribute the code - that's why I want to be able to compile it with no modifications. The code is available freely online, but there is no licensing details with it. Therefore, I want to be able to produce instructions for any users who want to use it to be able to download it and compile it themselves. – robintw Nov 16 '12 at 08:15
  • 2
    @DigitalRoss, this is simply not true. The 32-bit IEEE 754 has 23 bits for the mantissa or 24 bits in total with the implict `1` in the integer part. This limits the range of exactly representable integers to `[0, 2^24-1]` which is less than the full range of the 32-bit `int` (there are other exactly representable integers above that range, but they are not consecutive). With double-precision this range is extended to `[0, 2^53-1]` - less than the full range of `long`. – Hristo Iliev Nov 16 '12 at 08:30
  • Of course, negative numbers in that range are exactly representable too. – Hristo Iliev Nov 16 '12 at 08:41
  • you all seem to be assuming that in this dodgy code the floats are only used to store integer values. If thats the case change the declarion and be done with it. If you have actual floating loop indices you need to accept the ambiguity in the last pass or figure out the intent and fix it right. – agentp Nov 16 '12 at 16:57

1 Answers1

3

Pass the argument -std=legacy to gfortran. Features removed in F95, like real loop and array indices, should compile (perhaps with a warning) in legacy mode.

Dan
  • 12,157
  • 12
  • 50
  • 84