The complex arcsin and arccos functions can each be expressed in two ways:
arcsin(z) = -j*log( sqrt(1.0 - z*z) + j*z ) Eqn 1
= j*log( sqrt(1.0 - z*z) - j*z ) Eqn 2
arccos(z) = -j*log( j*sqrt(1.0 – z*z) + z ) Eqn 3
= pi/2 – arcsin(z) Eqn 4
The square root term 1.0 – zz is common to three of these, and the addition of +jz or -j*z in eqns 1 and 2 determine what is passed to the LOG function for the arcsin.
Under certain conditions this could result in both the real and imaginary parts of the argument to the log function being zero.
Now consider the following program:
program p
use iso_fortran_env
implicit none
integer, parameter :: wp = kind(1.d0)
complex(kind=wp) :: z1,z2
print*, compiler_version()
print*
z1 = cmplx(500.0D6,500.0D6,kind=wp)
z2 = cmplx(500.0D6,-500.0D6,kind=wp)
print*, acos(z1)
print*, acos(z2) !Real and imaginary parts of argument to LOG are both zero
print*, asin(z1) !Real and imaginary parts of argument to LOG are both zero
print*, asin(z2)
end program p
With the problematic lines commented out, trying three different compilers:
FTN95 v9.10.0
(0.78539816339,-21.0698394272)
(0.78539816339,-21.0698394272)
GCC version 12.3.0
(1.5707963267948966,-Inf)
(0.78539816339744828,-21.069839427226384)
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel
(R) 64, Version 2021.4.0 Build 20210910_000000
(0.785398163397448,-21.0698394272264)
(0.785398163397448,-21.0698394272264)
We see that FTN95 returns the same values as Ifort, but gFortran does something different for ACOS(Z1).