Is it an inconcistency for real(10)?

With reference to the following code

program pg_real
  implicit none

  real(4)  :: r4
  real(8)  :: r8
  real(10) :: r10
  real(16) :: r16

  r4  = 1_4
  r8  = 1_8
  !r10 = 1_10 ! NOT accepted
  r10 = 1._10 ! ACCEPTED
  r16 = 1_16

  print *, r4, r8, r10, r16
end program pg_real

which prints

1.00000000       1.0000000000000000        1.00000000000000000000         1.00000000000000000000000000000000000

Shouldn’t 1_10 be accepted like the rest ones ie 1_4, 1_8, 1_16 in which 1 is in integer form and not in decimal form 1.? If not, please provide a documentation link which justifies this small inconsistency.

COMPILER_VERSION(): GCC version 10.3.0

Tia

2 Likes

@FLNewbiee welcome to the Discourse!

You’re assigning integer values to your reals, and your compiler has no such thing as integer(kind=10)

7 Likes

Ohh, I see. 1_[4|8|16] are integer values. Tal

This is a common mistake, even among experienced programmers. It sometimes doesn’t matter because the integer is converted to a real anyway, just not in the straightforward way that the programmer intended. Some compilers have separate KIND values for integers and reals, so the compiler can catch the mistake.

1 Like

I thought that semantics/notation _n was interpreted based, at first, on real declaration. On the contrary, it is interpreted on the previous number type and next is coerced to a real number. I hope there is a point in the documentation which clarifies this detail (X_n = X(kind=n)).

We all forget that every now and then, but that’s item no.1 in each Fortran textbook about what an assignment is:

  • first, the RHS is evaluated
  • then, it is assigned to the LHS

For the same reason, you could also have

real :: x = 2/3

that would return 0.0, because 2/3 is a division between two integers.

2 Likes

In this Fortran statement,
“1_4” is an integer constant “1” whose kind attribute is kind=4
“r4” is a real variable, whose kind attribute is kind=4, from the declaration.
The properties of “1_4” are not affected by “r4”.

Using gfortran, a real kind=10 is different, as it refers to an 80-bit real format, which is stored in a 128 bit (16 byte) storage allocation. This is different to the real*10 format associated with a Win-32 8087 register.

Thanks to all for your comments and your time of course. Coming back, after 30 years of absence, to Fortran is a little bit weird to me. :smiley:

@FLNewbiee welcome to the forum!

Indeed, it’s confusing and the compilers could potentially warn about some such usage that is almost always a bug in the code that we would prefer for compilers to point out.