Red Hat Bugzilla – Bug 175913
Wrong calculation result with complex SQRT in gfortran
Last modified: 2007-11-30 17:11:19 EST
The square root of -4.0 should be (0.0,2.0), but in the following
test program it gives (0.0,-2.0). For this test the following version
of gcc were used: gcc version 4.0.2 20051125 (Red Hat 4.0.2-8).
[taschna@localhost ~]$ cat ctest.f
DOUBLE COMPLEX Z1, Z2, Z3
WRITE(*,*) 'SQRT((-4.0d0,0.0d0))=', SQRT((-4.0d0,0.0d0))
WRITE(*,*) 'Z2=', Z2
WRITE(*,*) 'Z3=SQRT(Z2)=', Z3
[taschna@localhost ~]$ gfortran -O0 ctest.f -o ctest;./ctest
SQRT((-4.0d0,0.0d0))= ( 0.00000000000000 , 2.00000000000000 )
Z2= ( -4.00000000000000 , 0.00000000000000 )
Z3=SQRT(Z2)= ( 0.00000000000000 , -2.00000000000000 )
The program gives also the wrong result, when DOUBLE COMPLEX is
substituted with COMPLEX.
The result is certainly the same as with Intel Fortran 9.0
(as well as the same thing rewritten into C).
The reason is that:
Z1**2 is (4.0d0,0.0d0), but -Z1**2 is (-4.0d0,-0.0d0).
And while SQRT((-4.0d0,0.0d0)) is really (0.0d0,2.0d0), SQRT((-4.0d0,-0.0d0))
This is a changed behaviour compared to g77 (3.4.4 20050721 (Red Hat 3.4.4-2)).
On Windows the Compaq Visual Fortran 6 gives also the result (0,2).
Therefore I am not sure whether it is a bug or not. I came across the bug when
testing to compile the CERNLIB. There the math test C208 failed because of
the new behaviour.