Bug 175913

Summary: Wrong calculation result with complex SQRT in gfortran
Product: [Fedora] Fedora Reporter: Alexander Täschner <taschna>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact:
Severity: high Docs Contact:
Priority: medium    
Version: 4   
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2005-12-16 13:44:28 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Alexander Täschner 2005-12-16 08:01:42 UTC
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
      PROGRAM CTEST
      DOUBLE COMPLEX Z1, Z2, Z3

        Z1=(2.0d0,0.0d0)
        Z2=-Z1**2
        Z3=SQRT(Z2)

        WRITE(*,*) 'SQRT((-4.0d0,0.0d0))=', SQRT((-4.0d0,0.0d0))
        WRITE(*,*) 'Z2=', Z2
        WRITE(*,*) 'Z3=SQRT(Z2)=', Z3

      END
[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.

Comment 1 Jakub Jelinek 2005-12-16 13:44:28 UTC
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))
is (0.0d0,-2.0d0).

Comment 2 Alexander Täschner 2005-12-18 12:04:37 UTC
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.