Bug 10689 - pow(std::complex(0),1/3) returns (nan, nan) instead of 0.
Summary: pow(std::complex(0),1/3) returns (nan, nan) instead of 0.
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: 3.3.1
Assignee: Gabriel Dos Reis
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2003-05-08 18:16 UTC by daniel.levine
Modified: 2013-07-24 18:45 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description daniel.levine 2003-05-08 18:16:00 UTC
Executing the pow(0,y) function call for doubles correctly produces return values of 0 as long as y != 0.  Doing the same with the pow(std::complex<double>(0),y) will return (nan, nan).  This appears to be caused by the log(x) when x is 0 returning nan as it should.  Further exp() will never return 0 so some check needs to be implemented so pow(std::complex<double>(0),y) can return 0 when y != 0.

This problem is not specific to complex<double>.  Other complex floating point types would suffer from the same problem.

One final note, pow(std::complex<double>(0), 1) works because it triggers other code while pow(std::complex<double>(0), 1.0) fails.

Release:
gcc 3.2-7

Environment:
Any.

How-To-Repeat:
#include <iostream>
using namespace std;
complex<double> complexZero;
complex<double> cubeRootOf0 = pow(complexZero, 1.0/3.0);
cerr << cubeRootof0 << endl; // Should be 0.0;
complex<double> zeroToThe1.0 = pow(complexZero, 1.0);
cerr << zeroToThe1.0 << endl; // Should be 0.0;
complex<double> zeroToThe1 = pow(complexZero, 1);
cerr << zeroToThe1 << endl; // Is 0.0;
Comment 1 Dara Hazeghi 2003-05-10 12:26:42 UTC
From: Dara Hazeghi <dhazeghi@yahoo.com>
To: gcc-gnats@gcc.gnu.org, Daniel.Levine@jhuapl.edu
Cc:  
Subject: Re: libstdc++/10689: pow(std::complex(0),1/3) returns (nan, nan) instead of 0.
Date: Sat, 10 May 2003 12:26:42 -0700

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit- 
 trail&database=gcc&pr=10689
 
 Hello,
 
 I can confirm this problem with 3.3 branch and mainline (20030508).  
 However, next time you send in a testcase, make sure it compilers first.
 
 Dara
 
 #include <iostream>
 #include <complex>
   using namespace std;
 int main() {
   complex<double> complexZero;
   complex<double> cubeRootof0;
   complex<double> zeroToThe1;
   cubeRootof0 = pow(complexZero, 1.0/3.0);
   cerr << cubeRootof0 << endl; // Should be 0.0;
   zeroToThe1 = pow(complexZero, 1.0);
   cerr << zeroToThe1 << endl; // Should be 0.0;
   zeroToThe1 = pow(complexZero, 1);
   cerr << zeroToThe1 << endl; // Is 0.0;
 }
 
Comment 2 Benjamin Kosnik 2003-05-16 20:04:27 UTC
Responsible-Changed-From-To: unassigned->gdr
Responsible-Changed-Why: Maybe you could triage this? It's either runtime or __builtin I think.
    
    best,
    benjamin
Comment 3 Gabriel Dos Reis 2003-05-18 14:57:42 UTC
From: Gabriel Dos Reis <gdr@integrable-solutions.net>
To: bkoz@gcc.gnu.org
Cc: Daniel.Levine@jhuapl.edu, gcc-bugs@gcc.gnu.org, gcc-gnats@gcc.gnu.org
Subject: Re: libstdc++/10689: pow(std::complex(0),1/3) returns (nan, nan) instead of 0.
Date: 18 May 2003 14:57:42 +0200

 bkoz@gcc.gnu.org writes:
 
 | Synopsis: pow(std::complex(0),1/3) returns (nan, nan) instead of 0.
 | 
 | Responsible-Changed-From-To: unassigned->gdr
 | Responsible-Changed-By: bkoz
 | Responsible-Changed-When: Fri May 16 20:04:27 2003
 | Responsible-Changed-Why:
 |     Maybe you could triage this? It's either runtime or __builtin I think.
 
 It is not __builtin.  It is runtime.
 A proper way to fix this is to to use __builtin where appropriate and
 runtime where not.  The traditional approach is through  traits.  Will
 handle that, after I applied the name lookup patch.  This week is too
 heavy...
 
 -- Gaby
Comment 4 Gabriel Dos Reis 2003-05-20 06:46:06 UTC
From: gdr@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: libstdc++/10689
Date: 20 May 2003 06:46:06 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_3-branch
 Changes by:	gdr@gcc.gnu.org	2003-05-20 06:46:06
 
 Modified files:
 	libstdc++-v3   : ChangeLog 
 	libstdc++-v3/include/std: std_complex.h 
 Added files:
 	libstdc++-v3/testsuite/26_numerics/complex: pow.C 
 
 Log message:
 	PR libstdc++/10689
 	* include/std/std_complex.h (pow): Tidy.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.1464.2.111&r2=1.1464.2.112
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/std/std_complex.h.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.3.2.1&r2=1.3.2.2
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/26_numerics/complex/pow.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
 

Comment 5 Gabriel Dos Reis 2003-05-20 06:52:12 UTC
From: gdr@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: libstdc++/10689
Date: 20 May 2003 06:52:12 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	gdr@gcc.gnu.org	2003-05-20 06:52:12
 
 Modified files:
 	libstdc++-v3   : ChangeLog 
 	libstdc++-v3/include/std: std_complex.h 
 Added files:
 	libstdc++-v3/testsuite/26_numerics/complex: pow.C 
 
 Log message:
 	PR libstdc++/10689
 	* include/std/std_complex.h (pow): Tidy
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&r1=1.1722&r2=1.1723
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/std/std_complex.h.diff?cvsroot=gcc&r1=1.5&r2=1.6
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/26_numerics/complex/pow.C.diff?cvsroot=gcc&r1=1.1&r2=1.2
Comment 6 Gabriel Dos Reis 2003-05-20 06:53:13 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Fixed.