This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

C++ complex number tests


I was surprised to find out just recently that:
sqrt(complex<double>(-1)) != pow(complex<double>(-1),.5)

It seems the latter, of course, leaves some floating point slop in the
real part of its result.  But when I checked out other Fortran
compilers' implementations of complex, they did the same thing.  Does
anyone know a method to avoid this inconsistency?  I guess it's not
really that important, but it would still be nice.

Anyway, in the course of doing this little investigation, I added a
couple tests to the testsuite for complex numbers, so I thought I'd at
least forward the context diffs to y'all.
-- 
Ian Haggard  ||  ian@shellus.com (work)  ||  IanHaggard@juno.com (home)
GNU/Linux -- "Oh, no, Mr Bill!" || #define employer_opinion !my_opinion
*** libstdc++/tests/ChangeLog.old	Fri Oct 10 02:43:27 1997
--- libstdc++/tests/ChangeLog	Fri Oct 24 17:45:53 1997
***************
*** 1,3 ****
--- 1,10 ----
+ Fri Oct 24 17:40:27 1997  Ian Haggard    <ian@shellus.com>
+ 
+ 	* tcomplex.cc (main): Test sqrt special cases, pow(cplx,.5), and
+ 	different input formats.
+ 	* tcomplex.exp: Add output for new tests.
+ 	* tcomplex.inp: Add additional input to test different input formats.
+ 
  Fri Oct 10 00:40:31 1997  Jason Merrill  <jason@yorick.cygnus.com>
  
  	* tstring.cc (identitytest): s/remove/erase/.
*** libstdc++/tests/tcomplex.cc.old	Thu Aug 21 17:58:42 1997
--- libstdc++/tests/tcomplex.cc	Fri Oct 24 17:47:34 1997
***************
*** 72,77 ****
--- 72,79 ----
    cout << "neg_one = " << neg_one << "\n";
    cout << "sqrt(neg_one) = " << sqrt(neg_one) << "\n";
  
+   cout << "pow(neg_one,.5) = " << pow(neg_one,.5) << "\n";
+ 
    double_complex a (2.0, 3.0);
    double_complex b (4.0, 5.0);
  
***************
*** 122,127 ****
--- 124,133 ----
    cout << "log(a) = " << log(a) << "\n";
    cout << "exp(a) = " << exp(a) << "\n";
    cout << "sqrt(a) = " << sqrt(a) << "\n";
+   cout << "sqrt(conj(a)) = " << sqrt(conj(a)) << "\n";
+   cout << "sqrt(-a) = " << sqrt(-a) << "\n";
+   cout << "sqrt(-conj(a)) = " << sqrt(-conj(a)) << "\n";
+ 
    cout << "pow(a, 2) = " << pow(a, 2) << "\n";
    {
       double_complex p = pow(a, b);
***************
*** 143,148 ****
--- 149,162 ----
    test3(d, d, i);
  
    cout << "enter a complex number in form a or (a) or (a, b): ";
+   cin >> c;
+   cout << "number = " << c << "\n";
+ 
+   cout << "enter another complex number in form a or (a) or (a, b): ";
+   cin >> c;
+   cout << "number = " << c << "\n";
+ 
+   cout << "enter another complex number in form a or (a) or (a, b): ";
    cin >> c;
    cout << "number = " << c << "\n";
  
*** libstdc++/tests/tcomplex.exp.old	Thu Aug 21 17:58:42 1997
--- libstdc++/tests/tcomplex.exp	Fri Oct 24 16:54:59 1997
***************
*** 2,7 ****
--- 2,8 ----
  i = (0,1)
  neg_one = (-1,0)
  sqrt(neg_one) = (0,1)
+ pow(neg_one,.5) = (6.12323e-17,1)
  a = (2,3)
  b = (4,5)
  a + one = (3,3)
***************
*** 30,37 ****
--- 31,43 ----
  log(a) = (1.28247,0.982794)
  exp(a) = (-7.31511,1.04274)
  sqrt(a) = (1.67415,0.895977)
+ sqrt(conj(a)) = (1.67415,-0.895977)
+ sqrt(-a) = (0.895977,-1.67415)
+ sqrt(-conj(a)) = (0.895977,1.67415)
  pow(a, 2) = (-5,12)
  pow(a, b) = (-0.753046,-0.986429)
  enter a complex number in form a or (a) or (a, b): number = (1.2,-34)
+ enter another complex number in form a or (a) or (a, b): number = (99,0)
+ enter another complex number in form a or (a) or (a, b): number = (5,0)
  
  End of test
*** libstdc++/tests/tcomplex.inp.old	Thu Aug 21 17:58:42 1997
--- libstdc++/tests/tcomplex.inp	Tue Oct 21 18:19:22 1997
***************
*** 1 ****
--- 1,4 ----
  (1.2, -34)
+ (99)
+ 5
+ 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]