From 9883aeb52ca264074ac0bc8590626377c0f6c8b4 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Wed, 30 Jun 2021 22:11:58 +0200 Subject: [PATCH 1/3] Add additional math builtins --- src/function/builtin.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/function/builtin.rs b/src/function/builtin.rs index 68f6239..48a9677 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -13,19 +13,27 @@ macro_rules! simple_math { Ok(Value::Float(num.$func())) })) }; + ($func:ident, 2) => { + Some(Function::new(|argument| { + let tuple = argument.as_fixed_len_tuple(2)?; + let (a, b) = (tuple[0].as_number()?, tuple[1].as_number()?); + Ok(Value::Float(a.$func(b))) + })) + }; } pub fn builtin_function(identifier: &str) -> Option { match identifier { // Log "math::ln" => simple_math!(ln), - "math::log" => Some(Function::new(|argument| { - let tuple = argument.as_fixed_len_tuple(2)?; - let (a, b) = (tuple[0].as_number()?, tuple[1].as_number()?); - Ok(Value::Float(a.log(b))) - })), + "math::log" => simple_math!(log, 2), "math::log2" => simple_math!(log2), "math::log10" => simple_math!(log10), + // Exp + "math::exp" => simple_math!(exp), + "math::exp2" => simple_math!(exp2), + // Pow + "math::pow" => simple_math!(powf, 2), // Cos "math::cos" => simple_math!(cos), "math::acos" => simple_math!(acos), @@ -41,9 +49,12 @@ pub fn builtin_function(identifier: &str) -> Option { "math::atan" => simple_math!(atan), "math::tanh" => simple_math!(tanh), "math::atanh" => simple_math!(atanh), + "math::atan2" => simple_math!(atan2, 2), // Root "math::sqrt" => simple_math!(sqrt), "math::cbrt" => simple_math!(cbrt), + // Hypotenuse + "math::hypot" => simple_math!(hypot, 2), // Rounding "floor" => simple_math!(floor), "round" => simple_math!(round), From 2c2c31efd7cfa3dbcd25dff7b1294f95f9cb1197 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Sun, 4 Jul 2021 14:25:16 +0200 Subject: [PATCH 2/3] Document math builtins --- src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 8818252..1c94ecc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -322,6 +322,9 @@ //! | `math::log` | 2 | Numeric, Numeric | Returns the logarithm of the number with respect to an arbitrary base | //! | `math::log2` | 1 | Numeric | Returns the base 2 logarithm of the number | //! | `math::log10` | 1 | Numeric | Returns the base 10 logarithm of the number | +//! | `math::exp` | 1 | Numeric | Returns `e^(number)`, (the exponential function) | +//! | `math::exp2` | 1 | Numeric | Returns `2^(number)` | +//! | `math::pow` | 2 | Numeric, Numeric | Raises a number to the power of the other number | //! | `math::cos` | 1 | Numeric | Computes the cosine of a number (in radians) | //! | `math::acos` | 1 | Numeric | Computes the arccosine of a number. The return value is in radians in the range [0, pi] or NaN if the number is outside the range [-1, 1] | //! | `math::cosh` | 1 | Numeric | Hyperbolic cosine function | @@ -332,10 +335,12 @@ //! | `math::asinh` | 1 | Numeric | Inverse hyperbolic sine function | //! | `math::tan` | 1 | Numeric | Computes the tangent of a number (in radians) | //! | `math::atan` | 1 | Numeric | Computes the arctangent of a number. The return value is in radians in the range [-pi/2, pi/2] | +//! | `math::atan2` | 2 | Numeric | Computes the four quadrant arctangent in radians | //! | `math::tanh` | 1 | Numeric | Hyperbolic tangent function | //! | `math::atanh` | 1 | Numeric | Inverse hyperbolic tangent function. | //! | `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 | //! | `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 26b52736b85feeeb0283cf72f75105597aed16d2 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Sun, 4 Jul 2021 14:30:16 +0200 Subject: [PATCH 3/3] Run cargo sync-readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 367a325..4fddd6a 100644 --- a/README.md +++ b/README.md @@ -339,6 +339,9 @@ This crate offers a set of builtin functions. | `math::log` | 2 | Numeric, Numeric | Returns the logarithm of the number with respect to an arbitrary base | | `math::log2` | 1 | Numeric | Returns the base 2 logarithm of the number | | `math::log10` | 1 | Numeric | Returns the base 10 logarithm of the number | +| `math::exp` | 1 | Numeric | Returns `e^(number)`, (the exponential function) | +| `math::exp2` | 1 | Numeric | Returns `2^(number)` | +| `math::pow` | 2 | Numeric, Numeric | Raises a number to the power of the other number | | `math::cos` | 1 | Numeric | Computes the cosine of a number (in radians) | | `math::acos` | 1 | Numeric | Computes the arccosine of a number. The return value is in radians in the range [0, pi] or NaN if the number is outside the range [-1, 1] | | `math::cosh` | 1 | Numeric | Hyperbolic cosine function | @@ -349,10 +352,12 @@ This crate offers a set of builtin functions. | `math::asinh` | 1 | Numeric | Inverse hyperbolic sine function | | `math::tan` | 1 | Numeric | Computes the tangent of a number (in radians) | | `math::atan` | 1 | Numeric | Computes the arctangent of a number. The return value is in radians in the range [-pi/2, pi/2] | +| `math::atan2` | 2 | Numeric | Computes the four quadrant arctangent in radians | | `math::tanh` | 1 | Numeric | Hyperbolic tangent function | | `math::atanh` | 1 | Numeric | Inverse hyperbolic tangent function. | | `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 | | `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 |