From 256f3562c2f8802099bb170f284b8ae62cb35134 Mon Sep 17 00:00:00 2001 From: Adolfo Gonzalez III Date: Thu, 7 Nov 2019 12:07:02 -0600 Subject: [PATCH] Changed to use existing method Made try_to_bigint public and then used for math.ceil and math.floor --- vm/src/obj/objfloat.rs | 2 +- vm/src/stdlib/math.rs | 20 ++++---------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index 17eb76a9f..896c47e65 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -89,7 +89,7 @@ fn inner_mod(v1: f64, v2: f64, vm: &VirtualMachine) -> PyResult { } } -fn try_to_bigint(value: f64, vm: &VirtualMachine) -> PyResult { +pub fn try_to_bigint(value: f64, vm: &VirtualMachine) -> PyResult { match value.to_bigint() { Some(int) => Ok(int), None => { diff --git a/vm/src/stdlib/math.rs b/vm/src/stdlib/math.rs index 9627ce6cc..da9cb983e 100644 --- a/vm/src/stdlib/math.rs +++ b/vm/src/stdlib/math.rs @@ -220,17 +220,11 @@ fn math_trunc(value: PyObjectRef, vm: &VirtualMachine) -> PyResult { /// /// * `value` - Either a float or a python object which implements __ceil__ /// * `vm` - Represents the python state. -/// -/// # Remarks -/// -/// Currently the code is applying ceil to the float using rust's builtin -/// method and then converts to integral using python float's __int__ method. -/// fn math_ceil(value: PyObjectRef, vm: &VirtualMachine) -> PyResult { if objtype::isinstance(&value, &vm.ctx.float_type()) { let v = objfloat::get_value(&value); - let v = vm.ctx.new_float(v.ceil()); - try_magic_method("__int__", vm, &v) + let v = objfloat::try_to_bigint(v.ceil(), vm)?; + Ok(vm.ctx.new_int(v)) } else { try_magic_method("__ceil__", vm, &value) } @@ -242,17 +236,11 @@ fn math_ceil(value: PyObjectRef, vm: &VirtualMachine) -> PyResult { /// /// * `value` - Either a float or a python object which implements __ceil__ /// * `vm` - Represents the python state. -/// -/// # Remarks -/// -/// Currently the code is applying floor to the float using rust's builtin -/// method and then converts to integral using python float's __int__ method. -/// fn math_floor(value: PyObjectRef, vm: &VirtualMachine) -> PyResult { if objtype::isinstance(&value, &vm.ctx.float_type()) { let v = objfloat::get_value(&value); - let v = vm.ctx.new_float(v.floor()); - try_magic_method("__int__", vm, &v) + let v = objfloat::try_to_bigint(v.floor(), vm)?; + Ok(vm.ctx.new_int(v)) } else { try_magic_method("__floor__", vm, &value) }