Fix anomalies in math functions (#222)

Co-authored-by: tsunyoku <mbruhyo@gmail.com>
This commit is contained in:
James 2022-09-20 08:26:38 +01:00 committed by GitHub
parent 5843edddac
commit 13b14380c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -68,14 +68,20 @@ pub fn max(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
pub fn mean(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> { pub fn mean(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
match args.remove(0) { 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), _ => Ok(Value::None),
} }
} }
pub fn median(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> { pub fn median(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
match args.remove(0) { 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), _ => Ok(Value::None),
} }
} }
@ -137,7 +143,10 @@ pub fn spread(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
} }
pub fn sqrt(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> { pub fn sqrt(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
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<Value>) -> Result<Value, Error> { pub fn stddev(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {