Add support for is_nan, is_finite, and other floating-point testing functions

This commit is contained in:
Ophir LOJKINE 2022-03-23 13:04:09 +01:00
parent d2ee6cd3ad
commit ea42cf6355
3 changed files with 32 additions and 0 deletions

View File

@ -23,6 +23,16 @@ macro_rules! simple_math {
}; };
} }
fn float_is(func: fn(f64) -> bool) -> Option<Function> {
Some(Function::new(move |argument| {
if let Ok(num) = argument.as_float() {
Ok(func(num).into())
} else {
Ok(false.into())
}
}))
}
macro_rules! int_function { macro_rules! int_function {
($func:ident) => { ($func:ident) => {
Some(Function::new(|argument| { Some(Function::new(|argument| {
@ -76,6 +86,11 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
"floor" => simple_math!(floor), "floor" => simple_math!(floor),
"round" => simple_math!(round), "round" => simple_math!(round),
"ceil" => simple_math!(ceil), "ceil" => simple_math!(ceil),
"is_nan" => float_is(f64::is_nan),
"is_finite" => float_is(f64::is_finite),
"is_infinite" => float_is(f64::is_infinite),
"is_normal" => float_is(f64::is_normal),
"is_subnormal" => float_is(f64::is_subnormal),
// Other // Other
"min" => Some(Function::new(|argument| { "min" => Some(Function::new(|argument| {
let arguments = argument.as_tuple()?; let arguments = argument.as_tuple()?;

View File

@ -328,6 +328,11 @@
//! | `round` | 1 | Numeric | Returns the nearest integer to a number. Rounds half-way cases away from 0.0 | //! | `round` | 1 | Numeric | Returns the nearest integer to a number. Rounds half-way cases away from 0.0 |
//! | `ceil` | 1 | Numeric | Returns the smallest integer greater than or equal to a number | //! | `ceil` | 1 | Numeric | Returns the smallest integer greater than or equal to a number |
//! | `if` | 3 | Boolean, Any, Any | If the first argument is true, returns the second argument, otherwise, returns the third | //! | `if` | 3 | Boolean, Any, Any | If the first argument is true, returns the second argument, otherwise, returns the third |
//! | `is_nan` | 1 | Numeric | Returns true if the argument is the floating-point value NaN, false otherwise |
//! | `is_finite` | 1 | Numeric | Returns true if the argument is a finite floating-point number, false otherwise |
//! | `is_infinite` | 1 | Numeric | Returns true if the argument is an infinite floating-point number, false otherwise |
//! | `is_normal` | 1 | Numeric | Returns true if the argument is a floating-point number that is neither zero, infinite, subnormal, or NaN, false otherwise |
//! | `is_subnormal` | 1 | Numeric | Returns true if the argument is a [subnormal](https://en.wikipedia.org/wiki/Subnormal_number) number, false otherwise |
//! | `math::ln` | 1 | Numeric | Returns the natural logarithm of the number | //! | `math::ln` | 1 | Numeric | Returns the natural logarithm of the number |
//! | `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 |

View File

@ -362,6 +362,18 @@ fn test_builtin_functions() {
assert_eq!(eval("round(1.9)"), Ok(Value::Float(2.0))); assert_eq!(eval("round(1.9)"), Ok(Value::Float(2.0)));
assert_eq!(eval("ceil(1.1)"), Ok(Value::Float(2.0))); assert_eq!(eval("ceil(1.1)"), Ok(Value::Float(2.0)));
assert_eq!(eval("ceil(1.9)"), Ok(Value::Float(2.0))); assert_eq!(eval("ceil(1.9)"), Ok(Value::Float(2.0)));
assert_eq!(eval("is_nan(\"xxx\")"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_nan(1.0/0.0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_nan(0.0/0.0)"), Ok(Value::Boolean(true)));
assert_eq!(eval("is_finite(1.0/0.0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_finite(0.0/0.0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_finite(0.0)"), Ok(Value::Boolean(true)));
assert_eq!(eval("is_infinite(0.0/0.0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_infinite(1.0/0.0)"), Ok(Value::Boolean(true)));
assert_eq!(eval("is_normal(1.0/0.0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_normal(0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_subnormal(0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_subnormal(1.0e-308)"), Ok(Value::Boolean(true)));
// Other // Other
assert_eq!(eval("min(4.0, 3)"), Ok(Value::Int(3))); assert_eq!(eval("min(4.0, 3)"), Ok(Value::Int(3)));
assert_eq!(eval("max(4.0, 3)"), Ok(Value::Float(4.0))); assert_eq!(eval("max(4.0, 3)"), Ok(Value::Float(4.0)));