This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/18882] wrong results of pow( complex<long double>(2, 1), odd integer )
- From: "chris at bubblescope dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 8 Dec 2004 14:15:39 -0000
- Subject: [Bug libstdc++/18882] wrong results of pow( complex<long double>(2, 1), odd integer )
- References: <20041208084814.18882.mikulik@physics.muni.cz>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Additional Comments From chris at bubblescope dot net 2004-12-08 14:15 -------
Heres a much smaller testcase which shows this bug:
#include<stdio.h>
class complex
{
public:
complex(long double r=0, long double i=0)
{
__real__ _M_value = r;
__imag__ _M_value = i;
}
long double real() const
{ return __real__ _M_value; }
long double imag() const
{ return __imag__ _M_value; }
__complex__ long double _M_value;
};
int main(void)
{
complex x(2,2),y,z;
int n = 1;
y = (n==1) ? x : complex(3,3);
n=2;
z = (n==1) ? x : complex(3,3);
printf("(%Lf,%Lf),(%Lf,%Lf),(%Lf,%Lf)\n",x.real(),x.imag(),y.real(),y.imag(),z.real(),z.imag());
}
This should print (2.0,2.0),(2.0,2.0),(3.0,3.0). Instead it prints
(2.0,2.0),(2.0,0.0),(3.0,3.0).
This bug is fairly obscure: As far as I can see it only appears if:
a) Using long double complex
b) Using n ? y : z operator
c) y is a previously declared variable, z is a temporary created here.
It turns up in bits/cmath.tcc, __cmath_power. A quick hacky fix is to change the
first line of this function from:
_Tp __y = __n % 2 ? __x : 1;
to
_Tp __temp=1;
_Tp __y = __n % 2 ? __x : __temp;
If you want to get your code to work.
I confirm this bug occurs in g++ 3.3.3 (on cygwin), and despite some fiddling I
can't get a similar thing to happen with current CVS.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18882