public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/48874] New: Sign of zeros sometimes lost in literals
@ 2011-05-04 15:58 jb at gcc dot gnu.org
  2011-05-04 16:27 ` [Bug c/48874] " joseph at codesourcery dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: jb at gcc dot gnu.org @ 2011-05-04 15:58 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48874

           Summary: Sign of zeros sometimes lost in literals
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: jb@gcc.gnu.org


Consider

#include <stdio.h>
#include <complex.h>

int main()
{
  double _Complex a = 0.0 + I*0.0;
  double _Complex b = 0.0 - I*0.0;
  double _Complex c = -0.0 + I*0.0;
  double _Complex d = -0.0 - I*0.0;
  printf("a= (%g,%g)\n", creal(a), cimag(a));
  printf("b= (%g,%g)\n", creal(b), cimag(b));
  printf("c= (%g,%g)\n", creal(c), cimag(c));
  printf("d= (%g,%g)\n", creal(d), cimag(d));
}

This program, compiled with "gcc zero1.c -O2 -pedantic -Wall -std=c99" (or
-std=gnu99) prints

a= (0,0)
b= (0,-0)
c= (0,0)
d= (-0,-0)

That is, the sign of the real part of "c" is lost. Add -fdump-tree-original to
the compile flags shows the dump as

;; Function main (null)
;; enabled by -tree-original

{
  complex double a = __complex__ (0.0, 0.0);
  complex double b = __complex__ (0.0, -0.0);
  complex double c = __complex__ (0.0, 0.0);
  complex double d = __complex__ (-0.0, -0.0);

    complex double a = __complex__ (0.0, 0.0);
    complex double b = __complex__ (0.0, -0.0);
    complex double c = __complex__ (0.0, 0.0);
    complex double d = __complex__ (-0.0, -0.0);
  printf ((const char * restrict) "a= (%g,%g)\n", REALPART_EXPR <a>,
IMAGPART_EXPR <a>);
  printf ((const char * restrict) "b= (%g,%g)\n", REALPART_EXPR <b>,
IMAGPART_EXPR <b>);
  printf ((const char * restrict) "c= (%g,%g)\n", REALPART_EXPR <c>,
IMAGPART_EXPR <c>);
  printf ((const char * restrict) "d= (%g,%g)\n", REALPART_EXPR <d>,
IMAGPART_EXPR <d>);
}
return 0;

so it seems the negative sign of the real part of c is lost already in the
frontend.

Version of the compiler: 

gcc version 4.7.0 20110504 (experimental) (GCC) 

Target: x86_64-unknown-linux-gnu


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/48874] Sign of zeros sometimes lost in literals
  2011-05-04 15:58 [Bug c/48874] New: Sign of zeros sometimes lost in literals jb at gcc dot gnu.org
@ 2011-05-04 16:27 ` joseph at codesourcery dot com
  2011-05-04 17:15 ` jb at gcc dot gnu.org
  2011-05-04 17:22 ` kargl at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: joseph at codesourcery dot com @ 2011-05-04 16:27 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48874

--- Comment #1 from joseph at codesourcery dot com <joseph at codesourcery dot com> 2011-05-04 16:17:12 UTC ---
On Wed, 4 May 2011, jb at gcc dot gnu.org wrote:

> #include <stdio.h>
> #include <complex.h>
> 
> int main()
> {
>   double _Complex a = 0.0 + I*0.0;
>   double _Complex b = 0.0 - I*0.0;
>   double _Complex c = -0.0 + I*0.0;
>   double _Complex d = -0.0 - I*0.0;
>   printf("a= (%g,%g)\n", creal(a), cimag(a));
>   printf("b= (%g,%g)\n", creal(b), cimag(b));
>   printf("c= (%g,%g)\n", creal(c), cimag(c));
>   printf("d= (%g,%g)\n", creal(d), cimag(d));
> }
> 
> This program, compiled with "gcc zero1.c -O2 -pedantic -Wall -std=c99" (or
> -std=gnu99) prints
> 
> a= (0,0)
> b= (0,-0)
> c= (0,0)
> d= (-0,-0)
> 
> That is, the sign of the real part of "c" is lost. Add -fdump-tree-original to
> the compile flags shows the dump as

That output appears correct to me.  Each initializer is a real+complex 
addition, and the sum of -0.0 and +0.0 is +0.0 except when rounding 
towards negative infinity.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/48874] Sign of zeros sometimes lost in literals
  2011-05-04 15:58 [Bug c/48874] New: Sign of zeros sometimes lost in literals jb at gcc dot gnu.org
  2011-05-04 16:27 ` [Bug c/48874] " joseph at codesourcery dot com
@ 2011-05-04 17:15 ` jb at gcc dot gnu.org
  2011-05-04 17:22 ` kargl at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: jb at gcc dot gnu.org @ 2011-05-04 17:15 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48874

Janne Blomqvist <jb at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #3 from Janne Blomqvist <jb at gcc dot gnu.org> 2011-05-04 17:03:56 UTC ---
Ah, I see. while sizeof(I) == 8, due to the addition it gets expanded to (0.0,
1.0), and then, as you say, the negative zero is lost. So to generate the
literal constant (-0, 0) one needs

  double _Complex c2 = -(0.0 - I*0.0);

Thank you both for the explanation.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/48874] Sign of zeros sometimes lost in literals
  2011-05-04 15:58 [Bug c/48874] New: Sign of zeros sometimes lost in literals jb at gcc dot gnu.org
  2011-05-04 16:27 ` [Bug c/48874] " joseph at codesourcery dot com
  2011-05-04 17:15 ` jb at gcc dot gnu.org
@ 2011-05-04 17:22 ` kargl at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: kargl at gcc dot gnu.org @ 2011-05-04 17:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48874

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kargl at gcc dot gnu.org

--- Comment #2 from kargl at gcc dot gnu.org 2011-05-04 17:00:31 UTC ---
(In reply to comment #0)
> Consider
> 
> #include <stdio.h>
> #include <complex.h>
> 
> int main()
> {
>   double _Complex a = 0.0 + I*0.0;
>   double _Complex b = 0.0 - I*0.0;
>   double _Complex c = -0.0 + I*0.0;
>   double _Complex d = -0.0 - I*0.0;
>   printf("a= (%g,%g)\n", creal(a), cimag(a));
>   printf("b= (%g,%g)\n", creal(b), cimag(b));
>   printf("c= (%g,%g)\n", creal(c), cimag(c));
>   printf("d= (%g,%g)\n", creal(d), cimag(d));
> }
> 
> This program, compiled with "gcc zero1.c -O2 -pedantic -Wall -std=c99" (or
> -std=gnu99) prints
> 
> a= (0,0)
> b= (0,-0)
> c= (0,0)
> d= (-0,-0)
> 
> That is, the sign of the real part of "c" is lost. Add -fdump-tree-original to
> the compile flags shows the dump as

To some of us this, this is a well-known problem/issue/feature of gcc.
In the expression, -0.0 + I * 0.0, the I is treated as 0 + i 1 where
i = sqrt(-1).  So, you get -0.0 + (0 + i 1) * 0.0 = -0.0 + 0.0 + i 0.0,
which yields 0.0 + i 0.0.

There were/are fun issues with NaN and +-Inf.  Joseph fixed some/all
of those problems.  For details, see

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24581


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-05-04 17:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-04 15:58 [Bug c/48874] New: Sign of zeros sometimes lost in literals jb at gcc dot gnu.org
2011-05-04 16:27 ` [Bug c/48874] " joseph at codesourcery dot com
2011-05-04 17:15 ` jb at gcc dot gnu.org
2011-05-04 17:22 ` kargl at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).