Merge pull request #85 from mulimoen/feature/math_builtins
Add additional math builtins
This commit is contained in:
commit
6a74ba8f94
@ -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::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::log2` | 1 | Numeric | Returns the base 2 logarithm of the number |
|
||||||
| `math::log10` | 1 | Numeric | Returns the base 10 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::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::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 |
|
| `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::asinh` | 1 | Numeric | Inverse hyperbolic sine function |
|
||||||
| `math::tan` | 1 | Numeric | Computes the tangent of a number (in radians) |
|
| `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::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::tanh` | 1 | Numeric | Hyperbolic tangent function |
|
||||||
| `math::atanh` | 1 | Numeric | Inverse 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::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::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_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::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 |
|
| `str::to_lowercase` | 1 | String | Returns the lower-case version of the string |
|
||||||
|
@ -13,19 +13,27 @@ macro_rules! simple_math {
|
|||||||
Ok(Value::Float(num.$func()))
|
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<Function> {
|
pub fn builtin_function(identifier: &str) -> Option<Function> {
|
||||||
match identifier {
|
match identifier {
|
||||||
// Log
|
// Log
|
||||||
"math::ln" => simple_math!(ln),
|
"math::ln" => simple_math!(ln),
|
||||||
"math::log" => Some(Function::new(|argument| {
|
"math::log" => simple_math!(log, 2),
|
||||||
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::log2" => simple_math!(log2),
|
"math::log2" => simple_math!(log2),
|
||||||
"math::log10" => simple_math!(log10),
|
"math::log10" => simple_math!(log10),
|
||||||
|
// Exp
|
||||||
|
"math::exp" => simple_math!(exp),
|
||||||
|
"math::exp2" => simple_math!(exp2),
|
||||||
|
// Pow
|
||||||
|
"math::pow" => simple_math!(powf, 2),
|
||||||
// Cos
|
// Cos
|
||||||
"math::cos" => simple_math!(cos),
|
"math::cos" => simple_math!(cos),
|
||||||
"math::acos" => simple_math!(acos),
|
"math::acos" => simple_math!(acos),
|
||||||
@ -41,9 +49,12 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
|
|||||||
"math::atan" => simple_math!(atan),
|
"math::atan" => simple_math!(atan),
|
||||||
"math::tanh" => simple_math!(tanh),
|
"math::tanh" => simple_math!(tanh),
|
||||||
"math::atanh" => simple_math!(atanh),
|
"math::atanh" => simple_math!(atanh),
|
||||||
|
"math::atan2" => simple_math!(atan2, 2),
|
||||||
// Root
|
// Root
|
||||||
"math::sqrt" => simple_math!(sqrt),
|
"math::sqrt" => simple_math!(sqrt),
|
||||||
"math::cbrt" => simple_math!(cbrt),
|
"math::cbrt" => simple_math!(cbrt),
|
||||||
|
// Hypotenuse
|
||||||
|
"math::hypot" => simple_math!(hypot, 2),
|
||||||
// Rounding
|
// Rounding
|
||||||
"floor" => simple_math!(floor),
|
"floor" => simple_math!(floor),
|
||||||
"round" => simple_math!(round),
|
"round" => simple_math!(round),
|
||||||
|
@ -322,6 +322,9 @@
|
|||||||
//! | `math::log` | 2 | Numeric, Numeric | Returns the logarithm of the number with respect to an arbitrary base |
|
//! | `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::log2` | 1 | Numeric | Returns the base 2 logarithm of the number |
|
||||||
//! | `math::log10` | 1 | Numeric | Returns the base 10 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::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::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 |
|
//! | `math::cosh` | 1 | Numeric | Hyperbolic cosine function |
|
||||||
@ -332,10 +335,12 @@
|
|||||||
//! | `math::asinh` | 1 | Numeric | Inverse hyperbolic sine function |
|
//! | `math::asinh` | 1 | Numeric | Inverse hyperbolic sine function |
|
||||||
//! | `math::tan` | 1 | Numeric | Computes the tangent of a number (in radians) |
|
//! | `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::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::tanh` | 1 | Numeric | Hyperbolic tangent function |
|
||||||
//! | `math::atanh` | 1 | Numeric | Inverse 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::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::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_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::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 |
|
//! | `str::to_lowercase` | 1 | String | Returns the lower-case version of the string |
|
||||||
|
Loading…
Reference in New Issue
Block a user