@@ -87,6 +87,10 @@ def assertClose(self, x, y, eps=1e-9):
8787        self .assertCloseAbs (x .real , y .real , eps )
8888        self .assertCloseAbs (x .imag , y .imag , eps )
8989
90+     def  assertSameSign (self , x , y ):
91+         if  copysign (1. , x ) !=  copysign (1. , y ):
92+             self .fail (f'{ x !r} { y !r}  )
93+ 
9094    def  check_div (self , x , y ):
9195        """Compute complex z=x*y, and check that z/x==y and z/y==x.""" 
9296        z  =  x  *  y 
@@ -456,16 +460,14 @@ def test_pow_with_small_integer_exponents(self):
456460                self .assertComplexesAreIdentical (c ** 1 , c )
457461                self .assertComplexesAreIdentical (c ** 2 , c * c )
458462                self .assertComplexesAreIdentical (c ** 3 , c * (c * c ))
459-                 self .assertComplexesAreIdentical (c ** 4 , (c * c )* (c * c ))
460-                 self .assertComplexesAreIdentical (c ** 5 , c * ((c * c )* (c * c )))
461-                 self .assertComplexesAreIdentical (c ** 6 , (c * c )* ((c * c )* (c * c )))
462-                 self .assertComplexesAreIdentical (c ** 7 , c * (c * c )* ((c * c )* (c * c )))
463-                 self .assertComplexesAreIdentical (c ** 8 , ((c * c )* (c * c ))* ((c * c )* (c * c )))
463+                 self .assertComplexesAreIdentical (c ** 3 , (c * c )* c )
464464                if  not  c :
465465                    continue 
466466                for  n  in  range (1 , 9 ):
467467                    with  self .subTest (exponent = - n ):
468468                        self .assertComplexesAreIdentical (c ** - n , 1 / (c ** n ))
469+ 
470+         # Special cases for complex division. 
469471        for  x  in  [+ 2 , - 2 ]:
470472            for  y  in  [+ 0.0 , - 0.0 ]:
471473                c  =  complex (x , y )
@@ -485,6 +487,25 @@ def test_pow_with_small_integer_exponents(self):
485487                    self .assertComplexesAreIdentical (c ** - 1 , complex (+ 0.0 * y , - 1 / x ))
486488                    self .assertComplexesAreIdentical (c ** - 2 , complex (- 0.0 , - y / x ))
487489
490+         # Test that zeroes has the same sign as small non-zero values. 
491+         eps  =  1e-8 
492+         pairs  =  [(complex (x , y ), complex (x , copysign (0.0 , y )))
493+                  for  x  in  [+ 1 , - 1 ] for  y  in  [+ eps , - eps ]]
494+         pairs  +=  [(complex (y , x ), complex (copysign (0.0 , y ), x ))
495+                   for  x  in  [+ 1 , - 1 ] for  y  in  [+ eps , - eps ]]
496+         for  c1 , c2  in  pairs :
497+             for  n  in  exponents :
498+                 with  self .subTest (value = c1 , exponent = n ):
499+                     r1  =  c1 ** n 
500+                     r2  =  c2 ** n 
501+                     self .assertClose (r1 , r2 )
502+                     self .assertSameSign (r1 .real , r2 .real )
503+                     self .assertSameSign (r1 .imag , r2 .imag )
504+                     self .assertNotEqual (r1 .real , 0.0 )
505+                     if  n  !=  0 :
506+                         self .assertNotEqual (r1 .imag , 0.0 )
507+                     self .assertTrue (r2 .real  ==  0.0  or  r2 .imag  ==  0.0 )
508+ 
488509    def  test_boolcontext (self ):
489510        for  i  in  range (100 ):
490511            self .assertTrue (complex (random () +  1e-6 , random () +  1e-6 ))
0 commit comments