Return error on a divide by zero (#1376)

Return error on a divide by zero
This commit is contained in:
Shaurya Shubham 2020-02-12 19:08:04 +05:30 committed by GitHub
parent c0be02a434
commit c1bec3b443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,9 +49,15 @@ fn calc(input: Value, args: &RawCommandArgs) -> Result<OutputStream, ShellError>
}
pub fn parse(math_expression: &str, tag: impl Into<Tag>) -> Result<Value, String> {
use std::f64;
let num = meval::eval_str(math_expression);
match num {
Ok(num) => Ok(UntaggedValue::from(Primitive::from(num)).into_value(tag)),
Ok(num) => {
if num == f64::INFINITY || num == f64::NEG_INFINITY {
return Err(String::from("cannot represent result"));
}
Ok(UntaggedValue::from(Primitive::from(num)).into_value(tag))
}
Err(error) => Err(error.to_string()),
}
}