forked from Rust-related/RustPython
@@ -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
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user