From 13b14380c6452732d66d07179e22859531605e65 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 20 Sep 2022 08:26:38 +0100 Subject: [PATCH] Fix anomalies in `math` functions (#222) Co-authored-by: tsunyoku --- lib/src/fnc/math.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/src/fnc/math.rs b/lib/src/fnc/math.rs index 5e444205..e664c2e3 100644 --- a/lib/src/fnc/math.rs +++ b/lib/src/fnc/math.rs @@ -68,14 +68,20 @@ pub fn max(_: &Context, mut args: Vec) -> Result { pub fn mean(_: &Context, mut args: Vec) -> Result { match args.remove(0) { - Value::Array(v) => Ok(v.as_numbers().mean().into()), + Value::Array(v) => match v.is_empty() { + true => Ok(Value::None), + false => Ok(v.as_numbers().mean().into()), + }, _ => Ok(Value::None), } } pub fn median(_: &Context, mut args: Vec) -> Result { match args.remove(0) { - Value::Array(v) => Ok(v.as_numbers().median().into()), + Value::Array(v) => match v.is_empty() { + true => Ok(Value::None), + false => Ok(v.as_numbers().median().into()), + }, _ => Ok(Value::None), } } @@ -137,7 +143,10 @@ pub fn spread(_: &Context, mut args: Vec) -> Result { } pub fn sqrt(_: &Context, mut args: Vec) -> Result { - Ok(args.remove(0).as_number().sqrt().into()) + match args.remove(0).as_number() { + v if v >= Number::Int(0) => Ok(v.sqrt().into()), + _ => Ok(Value::None), + } } pub fn stddev(_: &Context, mut args: Vec) -> Result {