The following program returns the correct results when compiled with Win32.
When compiled with X64 the results are incorrect. The error occurs at line 8, in which there is the product of two complex numbers, the latter defined by two nested function calls.
module problem_mod
integer, parameter :: wp = kind(1.0)
contains
! Rotate phasor by specified angle (deg)
complex(kind=wp) function crotphasordeg(phasor,angle)
complex(kind=wp), intent(in) :: phasor
real(kind=wp), intent(in) :: angle
crotphasordeg = phasor*cang2phasor(sdeg2rad(angle)) ! ERROR OCCURS IN THIS EXPRESSION
! crotphasordeg = cang2phasor(sdeg2rad(angle)) ! This alternative form gives the
! crotphasordeg = phasor*crotphasordeg ! correct result.
end function crotphasordeg
! Define the unit phasor at the specified angle
complex(kind=wp) function cang2phasor(angle)
real(kind=wp), intent(in) :: angle
cang2phasor = exp(cmplx(0.0,1.0,kind=wp)*angle) ! e^(j*angle_rad)
end function cang2phasor
! Convert supplied angle in deg to radians
real(kind=wp) function sdeg2rad(angle)
real(kind=wp), intent(in) :: angle
sdeg2rad = angle*(4.0*atan(1.0))/180.0
end function sdeg2rad
end module problem_mod
program p
use problem_mod
complex(kind=wp) z, z1
real(kind=wp) angle
z = cmplx(1.0,0.0,kind=wp)
angle = 30.0
z1 = crotphasordeg(z,angle)
print*, 'The phasor ', z, 'rotated by ', angle, 'degrees is', z1
angle = -30.0
z1 = crotphasordeg(z,angle)
print*, 'The phasor ', z, 'rotated by ', angle, 'degrees is', z1
print*
end program p
If the module parameter wp is changed to kind(1.d0) everything is as expected.
Evaluating the required values in a single line of code (kind=1), i.e. no function calls returns the correct results for both win32 and x64.
print*, '+30', cmplx(1.0,0.0)*exp(cmplx(0.0,1.0)*(+30.0*(4.0*atan(1.0))/180.0))
print*, '-30', cmplx(1.0,0.0)*exp(cmplx(0.0,1.0)*(-30.0*(4.0*atan(1.0))/180.0))
end