From f4e6e5dec88d373ab3283eecf68b977a79f6464a Mon Sep 17 00:00:00 2001 From: Adam Gutglick Date: Sun, 24 Feb 2019 04:02:24 +0200 Subject: [PATCH] Addred real property and is_integer function to float. --- tests/snippets/floats.py | 4 ++++ vm/src/obj/objfloat.rs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/snippets/floats.py b/tests/snippets/floats.py index 003cf1ff0b..ac24a4dbfa 100644 --- a/tests/snippets/floats.py +++ b/tests/snippets/floats.py @@ -83,3 +83,7 @@ assert 1.0.__rtruediv__(2) == 2.0 assert 2.0.__mul__(1) == 2.0 assert 2.0.__rsub__(1) == -1.0 + +assert (1.7).real == 1.7 +assert (1.3).is_integer() == False +assert (1.0).is_integer() == True diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index f299251f19..c7b46299d3 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -418,6 +418,19 @@ fn float_rmul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { float_mul(vm, args) } +fn float_real(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(i, Some(vm.ctx.float_type()))]); + let v = get_value(i); + Ok(vm.ctx.new_float(v)) +} + +fn float_is_integer(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(i, Some(vm.ctx.float_type()))]); + let v = get_value(i); + let result = v == v.round(); + Ok(vm.ctx.new_bool(result)) +} + pub fn init(context: &PyContext) { let float_type = &context.float_type; @@ -465,4 +478,10 @@ pub fn init(context: &PyContext) { ); context.set_attr(&float_type, "__mul__", context.new_rustfunc(float_mul)); context.set_attr(&float_type, "__rmul__", context.new_rustfunc(float_rmul)); + context.set_attr(&float_type, "real", context.new_property(float_real)); + context.set_attr( + &float_type, + "is_integer", + context.new_rustfunc(float_is_integer), + ); }