Bug 143057

Summary: complex<double> / inf gives nan instead of 0
Product: [Fedora] Fedora Reporter: Dmitri A. Sergatskov <dasergatskov>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED UPSTREAM QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: 3   
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
URL: http://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2004-12-23 14:18:49 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 Dmitri A. Sergatskov 2004-12-16 01:25:31 UTC
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5)
Gecko/20041111 Firefox/1.0

Description of problem:
The following program produces "nan" output instead of correct 0.
#include <complex>
#include <iostream>

int main()
{
  std::complex<double> z1(1.0,-1.0);
  double x = 1.0/0.0;
  std::complex<double> z3 = z1/x;

  std::cout
        << "z1: " << z1 << std::endl
        << "x: " << x << std::endl 
        << "z3: " << z3 << std::endl;
  return 0;
}


Version-Release number of selected component (if applicable):
gcc-3.4.3-10 (and quite a few versions before)

How reproducible:
Always

Steps to Reproduce:
1. compile program
2. run it
3.
    

Actual Results:  z1: (1,-1)
x: inf
z3: (nan,nan)


Expected Results:  z1: (1,-1)
x: inf
z3: 0

Additional info:

As alois.schloegl pointed out
<<<<
Prof. W .Kahan mentions in his "Lecture Notes on the Status of IEEE
754" p.10
http://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF

"when (0+3i)/inf is turned naively into (0+3i)*(inf-i)/(inf^2+0^2) it
generates
a NaN instead of the expected 0."
>>>>

Comment 1 Jakub Jelinek 2004-12-23 14:18:49 UTC
Please file this upstream (http://gcc.gnu.org/bugzilla), it is better discussed
there.  Reproduceable even with:
#include <stdio.h>

_Complex double a, r;
double b = 1.0 / 0.0;

int
main (void)
{
  __real__ a = 1.0;
  __imag__ a = -1.0;
  r = a / b;
  printf ("%f %f\n", __real__ r, __imag__ r);
  return 0;
}


Comment 2 Dmitri A. Sergatskov 2004-12-23 14:37:17 UTC
Done. See
Bug c/19138