From 4bb412a9ab354196f550b2fc5ba7d34449ee2f1e Mon Sep 17 00:00:00 2001 From: Bojan Date: Sat, 27 Oct 2018 13:14:35 +0200 Subject: [PATCH 1/3] Added __lt__ for float --- vm/src/obj/objfloat.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index affd18a95..13b93378a 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -61,6 +61,22 @@ fn float_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.ctx.new_bool(result)) } +fn float_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!( + vm, + args, + required = [ + (zelf, Some(vm.ctx.float_type())), + (other, Some(vm.ctx.float_type())) + ] + ); + let zelf = get_value(zelf); + let other = get_value(other); + let result = zelf < other; + Ok(vm.ctx.new_bool(result)) +} + + fn float_abs(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(i, Some(vm.ctx.float_type()))]); Ok(vm.ctx.new_float(get_value(i).abs())) @@ -171,6 +187,7 @@ fn float_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { pub fn init(context: &PyContext) { let ref float_type = context.float_type; float_type.set_attr("__eq__", context.new_rustfunc(float_eq)); + float_type.set_attr("__lt__", context.new_rustfunc(float_lt)); float_type.set_attr("__abs__", context.new_rustfunc(float_abs)); float_type.set_attr("__add__", context.new_rustfunc(float_add)); float_type.set_attr("__divmod__", context.new_rustfunc(float_divmod)); From 6df02536c85a581a44b45f5043be163124522d40 Mon Sep 17 00:00:00 2001 From: Bojan Date: Sat, 27 Oct 2018 21:03:40 +0200 Subject: [PATCH 2/3] Removed unused line --- vm/src/obj/objfloat.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index 2c13cc0b0..c28674eec 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -76,7 +76,6 @@ fn float_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.ctx.new_bool(result)) } - fn float_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!( vm, From a2889f10e9fca65abcbbdd3dd58700c90a0d412f Mon Sep 17 00:00:00 2001 From: Bojan Date: Sat, 27 Oct 2018 22:08:39 +0200 Subject: [PATCH 3/3] Added some tests --- tests/snippets/floats.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/snippets/floats.py b/tests/snippets/floats.py index d4cc279a8..9bc8b7277 100644 --- a/tests/snippets/floats.py +++ b/tests/snippets/floats.py @@ -1 +1,8 @@ 1 + 1.1 + +a = 1.2 +b = 1.3 +c = 1.2 +assert a < b +assert a <= b +assert a <= c