mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Add complex.{__mod__, __rmod__, __divmod__, __rdivmod__}
This commit is contained in:
@@ -36,6 +36,17 @@ assert 5 * complex(2, -3) == complex(2, -3) * 5
|
||||
assert complex(2, -3) / 2 == complex(1, -1.5)
|
||||
assert 5 / complex(3, -4) == complex(0.6, 0.8)
|
||||
|
||||
# __mod__, __rmod__
|
||||
|
||||
assert_raises(
|
||||
TypeError,
|
||||
lambda: complex(2, -3) % 2,
|
||||
"can't mod complex numbers.")
|
||||
assert_raises(
|
||||
TypeError,
|
||||
lambda: 2 % complex(2, -3),
|
||||
"can't mod complex numbers.")
|
||||
|
||||
# __floordiv__, __rfloordiv__
|
||||
|
||||
assert_raises(
|
||||
@@ -47,6 +58,17 @@ assert_raises(
|
||||
lambda: 2 // complex(2, -3),
|
||||
"can't take floor of complex number.")
|
||||
|
||||
# __divmod__, __rdivmod__
|
||||
|
||||
assert_raises(
|
||||
TypeError,
|
||||
lambda: divmod(complex(2, -3), 2),
|
||||
"can't take floor or mod of complex number.")
|
||||
assert_raises(
|
||||
TypeError,
|
||||
lambda: divmod(2, complex(2, -3)),
|
||||
"can't take floor or mod of complex number.")
|
||||
|
||||
# __neg__
|
||||
|
||||
assert -complex(1, -1) == complex(-1, 1)
|
||||
|
||||
@@ -162,6 +162,16 @@ impl PyComplex {
|
||||
)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mod__")]
|
||||
fn mod_(&self, _other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
Err(vm.new_type_error("can't mod complex numbers.".to_string()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rmod__")]
|
||||
fn rmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.mod_(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__floordiv__")]
|
||||
fn floordiv(&self, _other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
Err(vm.new_type_error("can't take floor of complex number.".to_string()))
|
||||
@@ -172,6 +182,16 @@ impl PyComplex {
|
||||
self.floordiv(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__divmod__")]
|
||||
fn divmod(&self, _other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
Err(vm.new_type_error("can't take floor or mod of complex number.".to_string()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rdivmod__")]
|
||||
fn rdivmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.divmod(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__neg__")]
|
||||
fn neg(&self, _vm: &VirtualMachine) -> Complex64 {
|
||||
-self.value
|
||||
|
||||
Reference in New Issue
Block a user