Double precision assignment

It appears that double precision assignment has an issue. The following program:

program test
implicit none
real(kind=4) :: pi1=acos(-1.)
real(kind=8) :: pi2=acos(-1._8)
print *,pi1,pi2
pi2 = real(pi1,8)
print *,pi1,pi2
pi1 = 1.
pi2 = real(pi1,8)
print *,pi1,pi2
end program test

produces the following output:

3.141593 3.141592653589793
3.141593 3.141592741012573
1.000000 1.000000000000000

It appears that the second assignment to p2 is incorrect. Shouldn’t the extra double precision digits in variable ‘pi2’ be zeroed out as in the 3rd assignment? Is this a problem?

Thanks.

Tom

Your computer does not usually do decimal arithmetic, and few Fortran compilers support decimal arithmetic. Numbers such as 0.1 do not have an exact representation in IEEE binary floating point format.

Change the PRINT statements to

PRINT 10, Pi1, Pi2
10 Format(2(2x,Z16))

and you will see why your program gave output that was correct, even if different from what you expected.