Add int and float methods for the complex type

This commit is contained in:
Nicolas Trinquier
2019-04-28 22:57:56 +02:00
parent f8373aa38a
commit eebdbfe679
2 changed files with 13 additions and 3 deletions

View File

@@ -161,12 +161,22 @@ impl PyComplex {
vm.ctx.new_bool(result)
}
#[pymethod(name = "__float__")]
fn float(&self, vm: &VirtualMachine) -> PyResult {
return Err(vm.new_type_error(String::from("Can't convert complex to float")));
}
#[pymethod(name = "__int__")]
fn int(&self, vm: &VirtualMachine) -> PyResult {
return Err(vm.new_type_error(String::from("Can't convert complex to int")));
}
#[pymethod(name = "__mul__")]
fn mul(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
match to_complex(other, vm) {
Ok(Some(other)) => Ok(vm.ctx.new_complex(Complex64::new(
self.value.re * other.re - self.value.im * other.im,
self.value.re * other.im + self.value.re * other.im,
self.value.re * other.im + self.value.im * other.re,
))),
Ok(None) => Ok(vm.ctx.not_implemented()),
Err(err) => Err(err),