From b1e1dab4cb86d477d226a92480e95f9513055702 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 26 Jan 2021 12:42:34 -0600 Subject: [PATCH] add % operator for modulus, work with decimals (#2975) * add % operator, work with decimals * removed the % operator to reserve for something else --- crates/nu-data/src/value.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/crates/nu-data/src/value.rs b/crates/nu-data/src/value.rs index d77996f306..ea49a4c2d7 100644 --- a/crates/nu-data/src/value.rs +++ b/crates/nu-data/src/value.rs @@ -155,6 +155,12 @@ pub fn compute_values( } Ok(x / bigdecimal::BigDecimal::from(y.clone())) } + Operator::Modulo => { + if y.is_zero() { + return Ok(zero_division_error()); + } + Ok(x % bigdecimal::BigDecimal::from(y.clone())) + } _ => Err((left.type_name(), right.type_name())), }?; Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) @@ -170,6 +176,13 @@ pub fn compute_values( } Ok(bigdecimal::BigDecimal::from(x.clone()) / y) } + Operator::Modulo => { + if y.is_zero() { + return Ok(zero_division_error()); + } + Ok(bigdecimal::BigDecimal::from(x.clone()) % y) + } + _ => Err((left.type_name(), right.type_name())), }?; Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) @@ -185,6 +198,13 @@ pub fn compute_values( } Ok(x / y) } + Operator::Modulo => { + if y.is_zero() { + return Ok(zero_division_error()); + } + Ok(x % y) + } + _ => Err((left.type_name(), right.type_name())), }?; Ok(UntaggedValue::Primitive(Primitive::Decimal(result)))