blueloveTH 2 lat temu
rodzic
commit
889cf99695
3 zmienionych plików z 20 dodań i 12 usunięć
  1. 1 1
      include/typings/c.pyi
  2. 8 2
      python/cmath.py
  3. 11 9
      tests/10_cmath.py

+ 1 - 1
include/typings/c.pyi

@@ -1,4 +1,4 @@
-from typing import overload, Generic, TypeVar
+from typing import Generic, TypeVar
 
 def malloc(size: int) -> 'void_p': ...
 def free(ptr: 'void_p') -> None: ...

+ 8 - 2
python/cmath.py

@@ -21,9 +21,9 @@ class complex:
     
     def __eq__(self, other):
         if type(other) is complex:
-            return math.isclose(self.real, other.real) and math.isclose(self.imag, other.imag)
+            return self.real == other.real and self.imag == other.imag
         if type(other) in (int, float):
-            return math.isclose(self.real, other) and self.imag == 0
+            return self.real == other and self.imag == 0
         return NotImplemented
     
     def __add__(self, other):
@@ -69,6 +69,9 @@ class complex:
     
     def __abs__(self) -> float:
         return math.sqrt(self.real ** 2 + self.imag ** 2)
+    
+    def __hash__(self):
+        return hash((self.real, self.imag))
 
 
 # Conversions to and from polar coordinates
@@ -147,6 +150,9 @@ def isinf(z: complex):
 def isnan(z: complex):
     return math.isnan(z.real) or math.isnan(z.imag)
 
+def isclose(a: complex, b: complex):
+    return math.isclose(a.real, b.real) and math.isclose(a.imag, b.imag)
+
 # Constants
 
 pi = math.pi

+ 11 - 9
tests/10_cmath.py

@@ -1,20 +1,22 @@
+from cmath import isclose, sqrt
+
 assert 1+2j == complex(1, 2) == 2j+1
 
-assert 1+2j + 3+4j == 4+6j
+assert isclose(1+2j + 3+4j, 4+6j)
 
-assert 1+2j - 3+4j == -2+6j
+assert isclose(1+2j - 3+4j, -2+6j)
 
 assert (1+2j).real == 1
 assert (1+2j).imag == 2
 
-assert (1+2j)*(3+4j) == -5+10j
-assert (1+2j)*3 == 3+6j
-
-import cmath
+assert isclose((1+2j)*(3+4j), -5+10j)
+assert isclose((1+2j)*3, 3+6j)
 
-assert (1+2j)**2 == -3+4j
+assert isclose((1+2j)**2, -3+4j)
 
 assert (1+2j).conjugate() == 1-2j
 
-res = cmath.sqrt(1+2j)
-assert res == 1.272019649514069+0.7861513777574233j
+res = sqrt(1+2j)
+assert isclose(res, 1.272019649514069+0.7861513777574233j)
+
+assert {1+2j: 1}[1+2j] == 1