Skip to content

Commit 37e85df

Browse files
committed
Add __ne__ for float
Issue RustPython#1442
1 parent 100416f commit 37e85df

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

tests/snippets/floats.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,34 @@
229229
# Test float exponent:
230230
assert 1 if 1else 0 == 1
231231

232+
a = 3.
233+
assert a.__eq__(3) is True
234+
assert a.__eq__(3.) is True
235+
assert a.__eq__(3.00000) is True
236+
assert a.__eq__(3.01) is False
237+
238+
pi = 3.14
239+
assert pi.__eq__(3.14) is True
240+
assert pi.__ne__(3.14) is False
241+
assert pi.__eq__(3) is False
242+
assert pi.__ne__(3) is True
243+
assert pi.__eq__('pi') is NotImplemented
244+
assert pi.__ne__('pi') is NotImplemented
245+
246+
assert pi.__eq__(float('inf')) is False
247+
assert pi.__ne__(float('inf')) is True
248+
assert float('inf').__eq__(pi) is False
249+
assert float('inf').__ne__(pi) is True
250+
assert float('inf').__eq__(float('inf')) is True
251+
assert float('inf').__ne__(float('inf')) is False
252+
assert float('inf').__eq__(float('nan')) is False
253+
assert float('inf').__ne__(float('nan')) is True
254+
255+
assert pi.__eq__(float('nan')) is False
256+
assert pi.__ne__(float('nan')) is True
257+
assert float('nan').__eq__(pi) is False
258+
assert float('nan').__ne__(pi) is True
259+
assert float('nan').__eq__(float('nan')) is False
260+
assert float('nan').__ne__(float('nan')) is True
261+
assert float('nan').__eq__(float('inf')) is False
262+
assert float('nan').__ne__(float('inf')) is True

vm/src/obj/objfloat.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,39 @@ impl PyFloat {
179179
PyFloat::from(float_val?).into_ref_with_type(vm, cls)
180180
}
181181

182+
fn float_eq(&self, other: PyObjectRef) -> bool {
183+
let other = get_value(&other);
184+
self.value == other
185+
}
186+
187+
fn int_eq(&self, other: PyObjectRef) -> bool {
188+
let other_int = objint::get_value(&other);
189+
let value = self.value;
190+
if let (Some(self_int), Some(other_float)) = (value.to_bigint(), other_int.to_f64()) {
191+
value == other_float && self_int == *other_int
192+
} else {
193+
false
194+
}
195+
}
196+
182197
#[pymethod(name = "__eq__")]
183198
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
184-
let value = self.value;
185199
let result = if objtype::isinstance(&other, &vm.ctx.float_type()) {
186-
let other = get_value(&other);
187-
value == other
200+
self.float_eq(other)
188201
} else if objtype::isinstance(&other, &vm.ctx.int_type()) {
189-
let other_int = objint::get_value(&other);
202+
self.int_eq(other)
203+
} else {
204+
return vm.ctx.not_implemented();
205+
};
206+
vm.ctx.new_bool(result)
207+
}
190208

191-
if let (Some(self_int), Some(other_float)) = (value.to_bigint(), other_int.to_f64()) {
192-
value == other_float && self_int == *other_int
193-
} else {
194-
false
195-
}
209+
#[pymethod(name = "__ne__")]
210+
fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
211+
let result = if objtype::isinstance(&other, &vm.ctx.float_type()) {
212+
!self.float_eq(other)
213+
} else if objtype::isinstance(&other, &vm.ctx.int_type()) {
214+
!self.int_eq(other)
196215
} else {
197216
return vm.ctx.not_implemented();
198217
};

0 commit comments

Comments
 (0)