fortrangfortranintel-fortranfortran2008

Strange behavior of "gfortran -Wconversion"


Consider the following code.

! test.f90

program test
    use iso_fortran_env, only: INT64, REAL64
    print *, real(0_INT64, REAL64)
    print *, real(1000_INT64, REAL64)
    print *, real(huge(0_INT64), REAL64)      
end program test

When compiling it with gfortran in the following way:

$ gfortran -Wconversion -std=f2008 test.f90

I got the following warning:

test.f90:5:18:

    5 |     print *, real(huge(0_INT64), REAL64)
      |                  1
Warning: Change of value in conversion from ‘INTEGER(8)’ to ‘REAL(8)’ at (1) [-Wconversion]

Note that gfortran is happy with the first two conversions, but not the last one.

Question: Is the warning illustrated above an expected behavior of gfortran? I thought that NO warning should be produced in any of the three cases, since the conversion is done explicitly by REAL( , INT64).

Here is the version information of my gfortran:

$ gfortran --version

GNU Fortran (Ubuntu 9.3.0-10ubuntu2) 9.3.0

As a reference, ifort 19.1.127 compiles test.f90 without any complaint:

$ ifort -warn all -stand f08 test.f90

Thank you very much for any comments or critics.


Solution

  • Answer by @dave_thompson_085 in the comments:

    “0 and 1000 can be represented exactly in REAL64 (and even in REAL32). HUGE(INT64) is 9223372036854775807 and it cannot. REAL64 has 53 bits for the 'mantissa' (really, significand), and after subtracting the sign and adding the hidden bit this supports just under 16 decimal digits of magnitude. 9223372036854775807 is 19 decimal digits. This is not a diagnostic required by the standard, so it's up to each 'processor' (compiler) what to do about it.”

    Thank you very much, @dave_thompson_085.