Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add complex.{__eq__, __neg__}
  • Loading branch information
OddCoincidence committed Feb 9, 2019
commit d66ca54a2d05d83b1629b53921cd79a6eba0350f
19 changes: 19 additions & 0 deletions tests/snippets/builtin_complex.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# __abs__

assert complex(3, 4).__abs__() == 5
assert complex(3, -4).__abs__() == 5
assert complex(1.5, 2.5).__abs__() == 2.9154759474226504

# __eq__

assert complex(1, -1).__eq__(complex(1, -1))
assert complex(1, 0).__eq__(1)
assert not complex(1, 1).__eq__(1)
assert complex(1, 0).__eq__(1.0)
assert not complex(1, 1).__eq__(1.0)
assert not complex(1, 0).__eq__(1.5)
assert complex(1, 0).__eq__(True)
assert not complex(1, 2).__eq__(complex(1, 1))
#assert complex(1, 2).__eq__('foo') == NotImplemented

# __neg__

assert complex(1, -1).__neg__() == complex(-1, 1)
assert complex(0, 0).__neg__() == complex(0, 0)
32 changes: 32 additions & 0 deletions vm/src/obj/objcomplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use super::super::pyobject::{
};
use super::super::vm::VirtualMachine;
use super::objfloat;
use super::objint;
use super::objtype;
use num_complex::Complex64;
use num_traits::ToPrimitive;

pub fn init(context: &PyContext) {
let complex_type = &context.complex_type;
Expand All @@ -15,6 +17,8 @@ pub fn init(context: &PyContext) {

context.set_attr(&complex_type, "__abs__", context.new_rustfunc(complex_abs));
context.set_attr(&complex_type, "__add__", context.new_rustfunc(complex_add));
context.set_attr(&complex_type, "__eq__", context.new_rustfunc(complex_eq));
context.set_attr(&complex_type, "__neg__", context.new_rustfunc(complex_neg));
context.set_attr(&complex_type, "__new__", context.new_rustfunc(complex_new));
context.set_attr(
&complex_type,
Expand Down Expand Up @@ -100,6 +104,34 @@ fn complex_conjugate(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.ctx.new_complex(v1.conj()))
}

fn complex_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(zelf, Some(vm.ctx.complex_type())), (other, None)]
);

let z = get_value(zelf);
let result = if objtype::isinstance(other, &vm.ctx.complex_type()) {
z == get_value(other)
} else if objtype::isinstance(other, &vm.ctx.int_type()) {
match objint::get_value(other).to_f64() {
Some(f) => z.im == 0.0f64 && z.re == f,
None => false,
}
} else if objtype::isinstance(other, &vm.ctx.float_type()) {
z.im == 0.0 && z.re == objfloat::get_value(other)
} else {
false
};
Ok(vm.ctx.new_bool(result))
}

fn complex_neg(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.complex_type()))]);
Ok(vm.ctx.new_complex(-get_value(zelf)))
}

fn complex_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, Some(vm.ctx.complex_type()))]);
let v = get_value(obj);
Expand Down