fortrangfortran

Numeric range of integer type according to Fortran standard


gfortran will fail to compile an executable with the code print*,-2147483648;end, because it considers -2147483648=-2^31 too big for its kind. However, this number is certainly not too big for the storage size of 4 bytes and this code compiles successfully with other compilers, like ifx or nvfortran (even with the compiler option to enforce standard compliance).

The maintainers of gfortran on GCC Bugzilla claim that it is not a bug and complies with the Fortran standard, which would mean that other compilers are wrong. gfortran treats -2147483648 not as a literal but as a negation operation applied to 2147483648, for some reason. Incidentally, ifx generates a warning in the case of 2147483648 but not in the case of -2147483648, which an ordinary user would expect.

So my questions are:


Solution

  • OK, according to Metcalf, Reid, Cohen and Bader "Modern Fortran Explained incorporating Fortran 2023" the model for an integer number i of a given kind is (stupid stackoverflow not supporting MathJax...)

    i = s * Sum_k w_k * r^{k-1}

    where

    I don't possess the standard but this is the same formula and interpretation as given in section 16.4 of "J3/24-007(Fortran 2023 Interpretation Document)"

    Note how the above model is symmetric about zero. Thus if the most positive value supported for a given kind of integer is 2147483647, the most negative number that need be supported is -2147483647. Thus as I understand it gfortran is perfectly within its rights here to reject -2147483648 if the maximum is +2147483647.

    I see nothing that stops a given implementation supporting integers for a given kind outside the range, only that as regards intrinsic numeric inquiry functions they behave as if the number were modelled by the above equation. Thus the intel compiler is also within its rights here, though personally I would like to to provide a diagnostic as a quality of implementation issue - but as I see it it is not required.