Add complex.{__mod__, __rmod__, __divmod__, __rdivmod__}

This commit is contained in:
Jeong YunWon
2019-05-01 02:57:23 +09:00
parent 9523baf5ac
commit 930c8eef50
2 changed files with 42 additions and 0 deletions

View File

@@ -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)

View File

@@ -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