'namespace' & document math functions

This commit is contained in:
Edwin Svensson 2021-05-31 01:04:33 +02:00
parent 8491bc61fc
commit 10a388aaf3
No known key found for this signature in database
GPG Key ID: 7F9EC4DD0C67951F
3 changed files with 82 additions and 38 deletions

View File

@ -320,16 +320,38 @@ For more information about user-defined functions, refer to the respective [sect
This crate offers a set of builtin functions. This crate offers a set of builtin functions.
| Identifier | Argument Amount | Argument Types | Description | | Identifier | Argument Amount | Argument Types | Description |
|------------|-----------------|----------------|-------------| |----------------------|-----------------|------------------------|-------------|
| `min` | >= 1 | Numeric | Returns the minimum of the arguments | | `min` | >= 1 | Numeric | Returns the minimum of the arguments |
| `max` | >= 1 | Numeric | Returns the maximum of the arguments | | `max` | >= 1 | Numeric | Returns the maximum of the arguments |
| `len` | 1 | String/Tuple | Returns the character length of a string, or the amount of elements in a tuple (not recursively) | | `len` | 1 | String/Tuple | Returns the character length of a string, or the amount of elements in a tuple (not recursively) |
| `str::regex_matches` | 2 | String, String | Returns true if the first argument matches the regex in the second argument (Requires `regex_support` feature flag) | | `floor` | 1 | Numeric | Returns the largest integer less than or equal to a number |
| `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) | | `round` | 1 | Numeric | Returns the nearest integer to a number. Round half-way cases away from 0.0 |
| `str::to_lowercase` | 1 | String | Returns the lower-case version of the string | | `ceil` | 1 | Numeric | Returns the smallest integer greater than or equal to a number |
| `str::to_uppercase` | 1 | String | Returns the upper-case version of the string | | `math::ln` | 1 | Numeric | Returns the natural logarithm of the number |
| `str::trim` | 1 | String | Strips whitespace from the start and the end of the string | | `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::cos` | 1 | Numeric | Computes the cosine of a number (in radians) |
| `math::acos` | 1 | Numeric | Computes the arccosine of a number. 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::acosh` | 1 | Numeric | Inverse hyperbolic cosine function |
| `math::sin` | 1 | Numeric | Computes the sine of a number (in radians) |
| `math::asin` | 1 | Numeric | Computes the arcsine of a number. Return value is in radians in the range [-pi/2, pi/2] or NaN if the number is outside the range [-1, 1] |
| `math::sinh` | 1 | Numeric | Hyperbolic sine function |
| `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. Return value is in radians in the range [-pi/2, pi/2] |
| `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 if a negative number |
| `math::cbrt` | 1 | Numeric | Returns the cube root 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 |
| `str::to_uppercase` | 1 | String | Returns the upper-case version of the string |
| `str::trim` | 1 | String | Strips whitespace from the start and the end of the string |
| `str::from` | >= 0 | Any | Returns passed value as string |
The `min` and `max` functions can deal with a mixture of integer and floating point arguments. The `min` and `max` functions can deal with a mixture of integer and floating point arguments.
If the maximum or minimum is an integer, then an integer is returned. If the maximum or minimum is an integer, then an integer is returned.

View File

@ -18,32 +18,32 @@ macro_rules! simple_math {
pub fn builtin_function(identifier: &str) -> Option<Function> { pub fn builtin_function(identifier: &str) -> Option<Function> {
match identifier { match identifier {
// Log // Log
"ln" => simple_math!(ln), "math::ln" => simple_math!(ln),
"log" => Some(Function::new(|argument| { "math::log" => Some(Function::new(|argument| {
let tuple = argument.as_fixed_len_tuple(2)?; let tuple = argument.as_fixed_len_tuple(2)?;
let (a, b) = (tuple[0].as_number()?, tuple[1].as_number()?); let (a, b) = (tuple[0].as_number()?, tuple[1].as_number()?);
Ok(Value::Float(a.log(b))) Ok(Value::Float(a.log(b)))
})), })),
"log2" => simple_math!(log2), "math::log2" => simple_math!(log2),
"log10" => simple_math!(log10), "math::log10" => simple_math!(log10),
// Cos // Cos
"cos" => simple_math!(cos), "math::cos" => simple_math!(cos),
"acos" => simple_math!(acos), "math::acos" => simple_math!(acos),
"cosh" => simple_math!(cosh), "math::cosh" => simple_math!(cosh),
"acosh" => simple_math!(acosh), "math::acosh" => simple_math!(acosh),
// Sin // Sin
"sin" => simple_math!(sin), "math::sin" => simple_math!(sin),
"asin" => simple_math!(asin), "math::asin" => simple_math!(asin),
"sinh" => simple_math!(sinh), "math::sinh" => simple_math!(sinh),
"asinh" => simple_math!(asinh), "math::asinh" => simple_math!(asinh),
// Tan // Tan
"tan" => simple_math!(tan), "math::tan" => simple_math!(tan),
"atan" => simple_math!(atan), "math::atan" => simple_math!(atan),
"tanh" => simple_math!(tanh), "math::tanh" => simple_math!(tanh),
"atanh" => simple_math!(atanh), "math::atanh" => simple_math!(atanh),
// Root // Root
"sqrt" => simple_math!(sqrt), "math::sqrt" => simple_math!(sqrt),
"cbrt" => simple_math!(cbrt), "math::cbrt" => simple_math!(cbrt),
// Rounding // Rounding
"floor" => simple_math!(floor), "floor" => simple_math!(floor),
"round" => simple_math!(round), "round" => simple_math!(round),

View File

@ -307,16 +307,38 @@
//! //!
//! This crate offers a set of builtin functions. //! This crate offers a set of builtin functions.
//! //!
//! | Identifier | Argument Amount | Argument Types | Description | //! | Identifier | Argument Amount | Argument Types | Description |
//! |------------|-----------------|----------------|-------------| //! |----------------------|-----------------|------------------------|-------------|
//! | `min` | >= 1 | Numeric | Returns the minimum of the arguments | //! | `min` | >= 1 | Numeric | Returns the minimum of the arguments |
//! | `max` | >= 1 | Numeric | Returns the maximum of the arguments | //! | `max` | >= 1 | Numeric | Returns the maximum of the arguments |
//! | `len` | 1 | String/Tuple | Returns the character length of a string, or the amount of elements in a tuple (not recursively) | //! | `len` | 1 | String/Tuple | Returns the character length of a string, or the amount of elements in a tuple (not recursively) |
//! | `str::regex_matches` | 2 | String, String | Returns true if the first argument matches the regex in the second argument (Requires `regex_support` feature flag) | //! | `floor` | 1 | Numeric | Returns the largest integer less than or equal to a number |
//! | `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) | //! | `round` | 1 | Numeric | Returns the nearest integer to a number. Round half-way cases away from 0.0 |
//! | `str::to_lowercase` | 1 | String | Returns the lower-case version of the string | //! | `ceil` | 1 | Numeric | Returns the smallest integer greater than or equal to a number |
//! | `str::to_uppercase` | 1 | String | Returns the upper-case version of the string | //! | `math::ln` | 1 | Numeric | Returns the natural logarithm of the number |
//! | `str::trim` | 1 | String | Strips whitespace from the start and the end of the string | //! | `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::cos` | 1 | Numeric | Computes the cosine of a number (in radians) |
//! | `math::acos` | 1 | Numeric | Computes the arccosine of a number. 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::acosh` | 1 | Numeric | Inverse hyperbolic cosine function |
//! | `math::sin` | 1 | Numeric | Computes the sine of a number (in radians) |
//! | `math::asin` | 1 | Numeric | Computes the arcsine of a number. Return value is in radians in the range [-pi/2, pi/2] or NaN if the number is outside the range [-1, 1] |
//! | `math::sinh` | 1 | Numeric | Hyperbolic sine function |
//! | `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. Return value is in radians in the range [-pi/2, pi/2] |
//! | `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 if a negative number |
//! | `math::cbrt` | 1 | Numeric | Returns the cube root 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 |
//! | `str::to_uppercase` | 1 | String | Returns the upper-case version of the string |
//! | `str::trim` | 1 | String | Strips whitespace from the start and the end of the string |
//! | `str::from` | >= 0 | Any | Returns passed value as string |
//! //!
//! The `min` and `max` functions can deal with a mixture of integer and floating point arguments. //! The `min` and `max` functions can deal with a mixture of integer and floating point arguments.
//! If the maximum or minimum is an integer, then an integer is returned. //! If the maximum or minimum is an integer, then an integer is returned.