From c1bec3b44349fef6ab08ba165e0d1fa5ddd1db9b Mon Sep 17 00:00:00 2001 From: Shaurya Shubham Date: Wed, 12 Feb 2020 19:08:04 +0530 Subject: [PATCH] Return error on a divide by zero (#1376) Return error on a divide by zero --- src/commands/calc.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/commands/calc.rs b/src/commands/calc.rs index 1f926dadbb..43333f66cf 100644 --- a/src/commands/calc.rs +++ b/src/commands/calc.rs @@ -49,9 +49,15 @@ fn calc(input: Value, args: &RawCommandArgs) -> Result } pub fn parse(math_expression: &str, tag: impl Into) -> Result { + 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()), } }