From bf716b2e872a2743bba2bc674a843289efc7212e Mon Sep 17 00:00:00 2001 From: Heki <75025611+LinuxHeki@users.noreply.github.com> Date: Sat, 20 May 2023 13:16:24 +0000 Subject: [PATCH 1/5] add math::abs function --- src/function/builtin.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/function/builtin.rs b/src/function/builtin.rs index 350a810..95aecda 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -78,6 +78,8 @@ pub fn builtin_function(identifier: &str) -> Option { "math::cbrt" => simple_math!(cbrt), // Hypotenuse "math::hypot" => simple_math!(hypot, 2), + // Absolute + "math::abs" => simple_math!(abs), // Rounding "floor" => simple_math!(floor), "round" => simple_math!(round), From 1fc137a2330f2030b140f602eac111f2e024462e Mon Sep 17 00:00:00 2001 From: LinuxHeki Date: Sat, 20 May 2023 15:34:46 +0200 Subject: [PATCH 2/5] add math::abs to docs --- README.md | 1 + src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index f58d1d1..62e290e 100644 --- a/README.md +++ b/README.md @@ -375,6 +375,7 @@ This crate offers a set of builtin functions. | `math::sqrt` | 1 | Numeric | Returns the square root of a number. Returns NaN for a negative number | | `math::cbrt` | 1 | Numeric | Returns the cube root of a number | | `math::hypot` | 2 | Numeric | Calculates the length of the hypotenuse of a right-angle triangle given legs of length given by the two arguments | +| `math::abs` | 1 | Numeric | Returns the absolute value of a number | | `str::regex_matches` | 2 | String, String | Returns true if the first argument matches the regex in the second argument (Requires `regex_support` feature flag) | | `str::regex_replace` | 3 | String, String, String | Returns the first argument with all matches of the regex in the second argument replaced by the third argument (Requires `regex_support` feature flag) | | `str::to_lowercase` | 1 | String | Returns the lower-case version of the string | diff --git a/src/lib.rs b/src/lib.rs index cfd5c40..bc0dd29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -358,6 +358,7 @@ //! | `math::sqrt` | 1 | Numeric | Returns the square root of a number. Returns NaN for a negative number | //! | `math::cbrt` | 1 | Numeric | Returns the cube root of a number | //! | `math::hypot` | 2 | Numeric | Calculates the length of the hypotenuse of a right-angle triangle given legs of length given by the two arguments | +//! | `math::abs` | 1 | Numeric | Returns the absolute value of a number | //! | `str::regex_matches` | 2 | String, String | Returns true if the first argument matches the regex in the second argument (Requires `regex_support` feature flag) | //! | `str::regex_replace` | 3 | String, String, String | Returns the first argument with all matches of the regex in the second argument replaced by the third argument (Requires `regex_support` feature flag) | //! | `str::to_lowercase` | 1 | String | Returns the lower-case version of the string | From 9bbb152b4a386c6607bde4dc67ff48c17ac16e1f Mon Sep 17 00:00:00 2001 From: LinuxHeki Date: Sat, 20 May 2023 15:36:45 +0200 Subject: [PATCH 3/5] add tests for math::abs --- tests/integration.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/integration.rs b/tests/integration.rs index e6c114e..ab0bfe4 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -363,6 +363,9 @@ fn test_builtin_functions() { eval("math::hypot(8.2, 1.1)"), Ok(Value::Float((8.2 as FloatType).hypot(1.1))) ); + // Absolute + assert_eq!(eval("math::abs(15)"), Ok(Value::Float(15.0))); + assert_eq!(eval("math::abs(-15)"), Ok(Value::Float(15.0))); // Rounding assert_eq!(eval("floor(1.1)"), Ok(Value::Float(1.0))); assert_eq!(eval("floor(1.9)"), Ok(Value::Float(1.0))); From d9698df268490f6ed9ec239cab4a5b939f7d5b95 Mon Sep 17 00:00:00 2001 From: LinuxHeki Date: Sun, 21 May 2023 09:12:12 +0200 Subject: [PATCH 4/5] math::abs of int returns int --- src/function/builtin.rs | 11 ++++++++--- tests/integration.rs | 6 ++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/function/builtin.rs b/src/function/builtin.rs index 95aecda..639584e 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -78,9 +78,6 @@ pub fn builtin_function(identifier: &str) -> Option { "math::cbrt" => simple_math!(cbrt), // Hypotenuse "math::hypot" => simple_math!(hypot, 2), - // Absolute - "math::abs" => simple_math!(abs), - // Rounding "floor" => simple_math!(floor), "round" => simple_math!(round), "ceil" => simple_math!(ceil), @@ -89,6 +86,14 @@ pub fn builtin_function(identifier: &str) -> Option { "math::is_finite" => float_is(FloatType::is_finite), "math::is_infinite" => float_is(FloatType::is_infinite), "math::is_normal" => float_is(FloatType::is_normal), + // Absolute + "math::abs" => Some(Function::new(|argument| { + match argument { + Value::Float(num) => Ok(Value::Float(num.abs())), + Value::Int(num) => Ok(Value::Int(num.abs())), + _ => Err(EvalexprError::ExpectedNumber { actual: argument.clone() }), + } + })), // Other "typeof" => Some(Function::new(move |argument| { Ok(match argument { diff --git a/tests/integration.rs b/tests/integration.rs index ab0bfe4..2665362 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -364,8 +364,10 @@ fn test_builtin_functions() { Ok(Value::Float((8.2 as FloatType).hypot(1.1))) ); // Absolute - assert_eq!(eval("math::abs(15)"), Ok(Value::Float(15.0))); - assert_eq!(eval("math::abs(-15)"), Ok(Value::Float(15.0))); + assert_eq!(eval("math::abs(15.4)"), Ok(Value::Float(15.4))); + assert_eq!(eval("math::abs(-15.4)"), Ok(Value::Float(15.4))); + assert_eq!(eval("math::abs(15)"), Ok(Value::Int(15))); + assert_eq!(eval("math::abs(-15)"), Ok(Value::Int(15))); // Rounding assert_eq!(eval("floor(1.1)"), Ok(Value::Float(1.0))); assert_eq!(eval("floor(1.9)"), Ok(Value::Float(1.0))); From 199a42ebab202af20fc5976a8ed037b70d691341 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Sat, 27 May 2023 11:04:56 +0300 Subject: [PATCH 5/5] Fixes for pull request #130. --- README.md | 2 +- src/function/builtin.rs | 11 +++++------ src/lib.rs | 2 +- tests/integration.rs | 10 +++++----- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 81bf0f0..9dc5326 100644 --- a/README.md +++ b/README.md @@ -388,7 +388,7 @@ Symmetrically, the `EmptyContextWithBuiltinFunctions` has builtin functions enab | `math::sqrt` | 1 | Numeric | Returns the square root of a number. Returns NaN for a negative number | | `math::cbrt` | 1 | Numeric | Returns the cube root of a number | | `math::hypot` | 2 | Numeric | Calculates the length of the hypotenuse of a right-angle triangle given legs of length given by the two arguments | -| `math::abs` | 1 | Numeric | Returns the absolute value of a number | +| `math::abs` | 1 | Numeric | Returns the absolute value of a number, returning an integer if the argument was an integer, and a float otherwise | | `str::regex_matches` | 2 | String, String | Returns true if the first argument matches the regex in the second argument (Requires `regex_support` feature flag) | | `str::regex_replace` | 3 | String, String, String | Returns the first argument with all matches of the regex in the second argument replaced by the third argument (Requires `regex_support` feature flag) | | `str::to_lowercase` | 1 | String | Returns the lower-case version of the string | diff --git a/src/function/builtin.rs b/src/function/builtin.rs index 639584e..7105743 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -78,6 +78,7 @@ pub fn builtin_function(identifier: &str) -> Option { "math::cbrt" => simple_math!(cbrt), // Hypotenuse "math::hypot" => simple_math!(hypot, 2), + // Rounding "floor" => simple_math!(floor), "round" => simple_math!(round), "ceil" => simple_math!(ceil), @@ -87,12 +88,10 @@ pub fn builtin_function(identifier: &str) -> Option { "math::is_infinite" => float_is(FloatType::is_infinite), "math::is_normal" => float_is(FloatType::is_normal), // Absolute - "math::abs" => Some(Function::new(|argument| { - match argument { - Value::Float(num) => Ok(Value::Float(num.abs())), - Value::Int(num) => Ok(Value::Int(num.abs())), - _ => Err(EvalexprError::ExpectedNumber { actual: argument.clone() }), - } + "math::abs" => Some(Function::new(|argument| match argument { + Value::Float(num) => Ok(Value::Float(num.abs())), + Value::Int(num) => Ok(Value::Int(num.abs())), + _ => Err(EvalexprError::expected_number(argument.clone())), })), // Other "typeof" => Some(Function::new(move |argument| { diff --git a/src/lib.rs b/src/lib.rs index 76c74ee..cd28209 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -371,7 +371,7 @@ //! | `math::sqrt` | 1 | Numeric | Returns the square root of a number. Returns NaN for a negative number | //! | `math::cbrt` | 1 | Numeric | Returns the cube root of a number | //! | `math::hypot` | 2 | Numeric | Calculates the length of the hypotenuse of a right-angle triangle given legs of length given by the two arguments | -//! | `math::abs` | 1 | Numeric | Returns the absolute value of a number | +//! | `math::abs` | 1 | Numeric | Returns the absolute value of a number, returning an integer if the argument was an integer, and a float otherwise | //! | `str::regex_matches` | 2 | String, String | Returns true if the first argument matches the regex in the second argument (Requires `regex_support` feature flag) | //! | `str::regex_replace` | 3 | String, String, String | Returns the first argument with all matches of the regex in the second argument replaced by the third argument (Requires `regex_support` feature flag) | //! | `str::to_lowercase` | 1 | String | Returns the lower-case version of the string | diff --git a/tests/integration.rs b/tests/integration.rs index 9e8cf38..0c97576 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -363,11 +363,6 @@ fn test_builtin_functions() { eval("math::hypot(8.2, 1.1)"), Ok(Value::Float((8.2 as FloatType).hypot(1.1))) ); - // Absolute - assert_eq!(eval("math::abs(15.4)"), Ok(Value::Float(15.4))); - assert_eq!(eval("math::abs(-15.4)"), Ok(Value::Float(15.4))); - assert_eq!(eval("math::abs(15)"), Ok(Value::Int(15))); - assert_eq!(eval("math::abs(-15)"), Ok(Value::Int(15))); // Rounding assert_eq!(eval("floor(1.1)"), Ok(Value::Float(1.0))); assert_eq!(eval("floor(1.9)"), Ok(Value::Float(1.0))); @@ -389,6 +384,11 @@ fn test_builtin_functions() { assert_eq!(eval("math::is_infinite(1.0/0.0)"), Ok(Value::Boolean(true))); assert_eq!(eval("math::is_normal(1.0/0.0)"), Ok(Value::Boolean(false))); assert_eq!(eval("math::is_normal(0)"), Ok(Value::Boolean(false))); + // Absolute + assert_eq!(eval("math::abs(15.4)"), Ok(Value::Float(15.4))); + assert_eq!(eval("math::abs(-15.4)"), Ok(Value::Float(15.4))); + assert_eq!(eval("math::abs(15)"), Ok(Value::Int(15))); + assert_eq!(eval("math::abs(-15)"), Ok(Value::Int(15))); // Other assert_eq!(eval("typeof(4.0, 3)"), Ok(Value::String("tuple".into()))); assert_eq!(eval("typeof(4.0)"), Ok(Value::String("float".into())));