Integer output formatting with print statement

前端 未结 3 1004
余生分开走
余生分开走 2020-12-20 00:09

I\'ve noted that if I use integer(16) variables, when I use print, the output contains the exact number of spaces expected. Thus, when I use (some

3条回答
  •  悲哀的现实
    2020-12-20 00:22

    List-directed output is processor-specific (note that Fortran standard calls "processor" what we'd normally refer to as "compiler"). Here is what the Fortran 90 standard says about list-directed output:

    10.8.2 List-directed output

    ...

    Integer output constants are produced with the effect of an Iw edit descriptor.

    Real constants are produced with the effect of either an F edit descriptor or an E edit descriptor, depending on the magnitude x of the value and a range 10d1 <= x < 10d2, where d1 and d2 are processor-dependent integers. If the magnitude x is within this range, the constant is produced using 0PFw.d; otherwise, 1PEw.dEe is used.

    For numeric output, reasonable processor-dependent values of w, d, and e are used for each of the numeric constants output.

    (emphasis mine)

    The same rules are used for numerical values. The text in §10.10.4 of the Fortran 2008 standard copies that from the Fortran 90 standard so no changes in list-directed output were introduced.

    As noted already by M. S. B., you can use the I0 edit descriptor to print integer values with the least amount of space necessary:

    ! Note the space in the string
    PRINT '("The difference is ",I0)', diff
    

提交回复
热议问题