public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/13450] New: std::pow(std::complex<double>(-1.,0.),0.5) yields (NaN,0)
@ 2003-12-19 17:46 pkienzle at nist dot gov
  2003-12-19 18:28 ` [Bug libstdc++/13450] " paolo at gcc dot gnu dot org
                   ` (22 more replies)
  0 siblings, 23 replies; 24+ messages in thread
From: pkienzle at nist dot gov @ 2003-12-19 17:46 UTC (permalink / raw)
  To: gcc-bugs

In gcc/libstdc++-v3/std/std_complex.h between revision 1.5 and 1.6, the
definition of pow(cplx,real) was changed from:

  return exp(__y * log(__x));

to:

   if (__x.imag() == _Tp())
      return pow(__x.real(), __y);
 	 
   complex<_Tp> __t = log(__x);
   return polar(exp(__y * __t.real()), __y * __t.imag());

It should be testing for negative numbers as well:

   if (__x.imag() == _Tp() && __x.real() >= _Tp())
      ...


Similarly, pow(real,cplx) should also test for real<0.  

Instead of:

   return __x == _Tp()
      ? _Tp()
      : polar(pow(__x, __y.real()), __y.imag() * log(__x));

use something like:

   if (__x == _Tp()) 
      return _Tp();
   else if (__x > _Tp()) 
      return polar(pow(__x, __y.real()), __y.imag() * log(__x));
   else
      return pow(complex<_Tp>(__x,_Tp()), __y);


The following program demonstrates the problem:

#include <cmath>
#include <iostream>
#include <complex>

using namespace std;

typedef double _Tp;

complex<_Tp> mypow(complex<_Tp> __x, _Tp __y)
{
   if (__x.imag() == _Tp() && __x.real() >= _Tp())
      return pow(__x.real(), __y);
 	 
   complex<_Tp> __t = log(__x);
   return polar(exp(__y * __t.real()), __y * __t.imag());
}

complex<_Tp> mypow(_Tp __x, complex<_Tp> __y)
{
  if (__x == _Tp()) 
    return _Tp();
  else if (__x > _Tp())
    return polar(pow(__x, __y.real()), __y.imag() * log(__x));
  else
    return pow(complex<_Tp>(__x,_Tp()),__y);
}

typedef complex<double> cplx;
void test(double a, double b)
{
  cout << "a=" << a << ", b=" << b << endl;
  cout << "pow(-cplx,cplx)   =" << pow(cplx(a,0),cplx(b,0)) << endl;
  cout << "pow(-real,cplx)   =" << pow(a,cplx(b,0)) << endl;
  cout << "pow(-cplx,real)   =" << pow(cplx(a,0),b) << endl;
  cout << "mypow(-real,cplx) =" << mypow(a,cplx(b,0)) << endl;
  cout << "mypow(-cplx,real) =" << mypow(cplx(a,0),b) << endl;
}

int main(int argc, char *argv[])
{
  test(0,0.5);
  test(-1,0.5);
  test(-3.2,1.4);
  test(3.2,1.4);
  return 0;
}

-- 
           Summary: std::pow(std::complex<double>(-1.,0.),0.5) yields
                    (NaN,0)
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: pkienzle at nist dot gov
                CC: gcc-bugs at gcc dot gnu dot org


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


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

end of thread, other threads:[~2004-09-28 13:27 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-19 17:46 [Bug libstdc++/13450] New: std::pow(std::complex<double>(-1.,0.),0.5) yields (NaN,0) pkienzle at nist dot gov
2003-12-19 18:28 ` [Bug libstdc++/13450] " paolo at gcc dot gnu dot org
2003-12-20  4:01 ` gdr at integrable-solutions dot net
2004-01-11  4:42 ` gdr at integrable-solutions dot net
2004-01-11 10:07 ` paolo at gcc dot gnu dot org
2004-01-11 15:16 ` gdr at integrable-solutions dot net
2004-01-11 16:41 ` paolo at gcc dot gnu dot org
2004-01-11 18:14 ` gdr at integrable-solutions dot net
2004-01-11 18:57 ` paolo at gcc dot gnu dot org
2004-01-11 19:38 ` gdr at integrable-solutions dot net
2004-01-12 16:07 ` pkienzle at nist dot gov
2004-01-12 20:23 ` gdr at integrable-solutions dot net
2004-03-03  2:20 ` [Bug libstdc++/13450] [3.3/3.4/3.5 Regression] " giovannibajo at libero dot it
2004-03-10  9:16 ` cvs-commit at gcc dot gnu dot org
2004-03-10 20:47 ` [Bug libstdc++/13450] [3.3/3.4 " andreas dot meier_ at gmx dot de
2004-03-10 21:16 ` mmitchel at gcc dot gnu dot org
2004-03-10 22:21 ` pcarlini at suse dot de
2004-03-11 18:33 ` cvs-commit at gcc dot gnu dot org
2004-04-08  3:15 ` [Bug libstdc++/13450] [3.3 " jlquinn at gcc dot gnu dot org
2004-05-18 18:42 ` andreas dot meier_ at gmx dot de
2004-06-11 22:22 ` pinskia at gcc dot gnu dot org
2004-08-08 19:46 ` cvs-commit at gcc dot gnu dot org
2004-09-28 13:24 ` gdr at gcc dot gnu dot org
2004-09-28 13:27 ` pinskia at gcc dot gnu dot 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).