Add __ne__ for float

Issue #1442
This commit is contained in:
HyeockJinKim
2019-10-09 19:14:47 +09:00
parent 100416f4f2
commit 37e85df527
2 changed files with 59 additions and 9 deletions

View File

@@ -229,3 +229,34 @@ assert float('nan').hex() == 'nan'
# Test float exponent:
assert 1 if 1else 0 == 1
a = 3.
assert a.__eq__(3) is True
assert a.__eq__(3.) is True
assert a.__eq__(3.00000) is True
assert a.__eq__(3.01) is False
pi = 3.14
assert pi.__eq__(3.14) is True
assert pi.__ne__(3.14) is False
assert pi.__eq__(3) is False
assert pi.__ne__(3) is True
assert pi.__eq__('pi') is NotImplemented
assert pi.__ne__('pi') is NotImplemented
assert pi.__eq__(float('inf')) is False
assert pi.__ne__(float('inf')) is True
assert float('inf').__eq__(pi) is False
assert float('inf').__ne__(pi) is True
assert float('inf').__eq__(float('inf')) is True
assert float('inf').__ne__(float('inf')) is False
assert float('inf').__eq__(float('nan')) is False
assert float('inf').__ne__(float('nan')) is True
assert pi.__eq__(float('nan')) is False
assert pi.__ne__(float('nan')) is True
assert float('nan').__eq__(pi) is False
assert float('nan').__ne__(pi) is True
assert float('nan').__eq__(float('nan')) is False
assert float('nan').__ne__(float('nan')) is True
assert float('nan').__eq__(float('inf')) is False
assert float('nan').__ne__(float('inf')) is True

View File

@@ -179,20 +179,39 @@ impl PyFloat {
PyFloat::from(float_val?).into_ref_with_type(vm, cls)
}
fn float_eq(&self, other: PyObjectRef) -> bool {
let other = get_value(&other);
self.value == other
}
fn int_eq(&self, other: PyObjectRef) -> bool {
let other_int = objint::get_value(&other);
let value = self.value;
if let (Some(self_int), Some(other_float)) = (value.to_bigint(), other_int.to_f64()) {
value == other_float && self_int == *other_int
} else {
false
}
}
#[pymethod(name = "__eq__")]
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
let value = self.value;
let result = if objtype::isinstance(&other, &vm.ctx.float_type()) {
let other = get_value(&other);
value == other
self.float_eq(other)
} else if objtype::isinstance(&other, &vm.ctx.int_type()) {
let other_int = objint::get_value(&other);
self.int_eq(other)
} else {
return vm.ctx.not_implemented();
};
vm.ctx.new_bool(result)
}
if let (Some(self_int), Some(other_float)) = (value.to_bigint(), other_int.to_f64()) {
value == other_float && self_int == *other_int
} else {
false
}
#[pymethod(name = "__ne__")]
fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
let result = if objtype::isinstance(&other, &vm.ctx.float_type()) {
!self.float_eq(other)
} else if objtype::isinstance(&other, &vm.ctx.int_type()) {
!self.int_eq(other)
} else {
return vm.ctx.not_implemented();
};