From dae76f3d56f846bca325f7fd7e7e0f5f2d775f1e Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 7 Jun 2023 15:21:36 -0700 Subject: [PATCH] Added check for negative powers --- src/tests.rs | 1 + src/tokens/operator.rs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index a8ddfd0..e62eadf 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -171,6 +171,7 @@ fn operators() { good_expr("7", "3!+1"); good_expr("18", "3!3"); bad_expr("3.1!"); + bad_expr("0^(-1)"); bad_expr("pi!"); } diff --git a/src/tokens/operator.rs b/src/tokens/operator.rs index 07b2c39..1473161 100644 --- a/src/tokens/operator.rs +++ b/src/tokens/operator.rs @@ -460,6 +460,10 @@ impl Operator{ return Err(EvalError::IncompatibleUnit); } + if va.is_zero() && vb.is_negative() { + return Err(EvalError::ZeroDivision); + } + let p = va.pow(vb); if p.is_nan() {return Err(EvalError::BadMath);} return Ok(Token::Number(p));