mecej4
Joined: 31 Oct 2006 Posts: 1886
|
Posted: Sun Oct 31, 2021 12:18 pm Post subject: Unexpected double precision when compiling for x86 |
|
|
The intrinsic function CMPLX(x, y) should return a complex result of default kind, regardless of whether the arguments x and y are single or double precision. FTN95 /64 adheres to this expectation, but an EXE from a FTN95 32-bit compilation may give a double precision result. The following test code illustrates the issue.
Code: | program xcmplx
implicit none
integer, parameter :: dp = kind(0d0)
real(dp) :: dx = 1d-1
complex(dp) :: x, y
!
x = cmplx(1d0, 1d-1) ! warning: expression is single precision
y = cmplx(1d0, dx)
print 10,x,y
10 format(2F25.15)
end |
The output from FTN95 /64:
Code: | 1.000000000000000 0.100000001490116
1.000000000000000 0.100000001490116 |
The output from FTN95 32-bit:
Code: | 1.000000000000000 0.100000001490116
1.000000000000000 0.100000000000000 |
Gfortran gives the same result as FTN95 /64. If it is used with its -Wall option, Gfortran also gives a warning:
Code: | 7 | x = cmplx(1d0, 1d-1) ! warning: expression is single precision
| 1
Warning: Conversion from REAL(8) to default-kind COMPLEX(4) at (1) might lose precision, consider using the KIND argument [-Wconversion] |
It would be useful to have FTN95 issue a similar warning.
In the larger code where the issue arose, it took quite a bit of effort to isolate the problem to the missing third argument to CMPLX, i.e., writing
instead of
Code: | x = cmplx(1.0d0,h,kind(1d0)) |
The variable h had been declared double precision. |
|