From 2d58ae9fb24da93e5810af4eb4bf2ea0eb1cacbb Mon Sep 17 00:00:00 2001 From: Lewis Van Winkle Date: Tue, 23 Aug 2016 15:05:59 -0500 Subject: [PATCH] Changed the way POW_FROM_RIGHT works with negative constants. --- test.c | 6 ++++++ tinyexpr.c | 8 +++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/test.c b/test.c index 21d270f..cd5779c 100644 --- a/test.c +++ b/test.c @@ -525,6 +525,10 @@ void test_pow() { #ifdef TE_POW_FROM_RIGHT test_equ cases[] = { {"2^3^4", "2^(3^4)"}, + {"-2^2", "-(2^2)"}, + {"-(2)^2", "-(2^2)"}, + {"-(2*1)^2", "-(2^2)"}, + {"-2^2", "-4"}, {"2^1.1^1.2^1.3", "2^(1.1^(1.2^1.3))"}, {"-a^b", "-(a^b)"}, {"-a^-b", "-(a^-b)"} @@ -532,6 +536,8 @@ void test_pow() { #else test_equ cases[] = { {"2^3^4", "(2^3)^4"}, + {"-2^2", "(-2)^2"}, + {"-2^2", "4"}, {"2^1.1^1.2^1.3", "((2^1.1)^1.2)^1.3"}, {"-a^b", "(-a)^b"}, {"-a^-b", "(-a)^(-b)"} diff --git a/tinyexpr.c b/tinyexpr.c index f9b9015..58717c2 100644 --- a/tinyexpr.c +++ b/tinyexpr.c @@ -384,11 +384,9 @@ static te_expr *factor(state *s) { if (ret->type == (TE_FUNCTION1 | TE_FLAG_PURE) && ret->function == negate) { te_expr *se = ret->parameters[0]; - if (se->type != TE_CONSTANT) { - free(ret); - ret = se; - neg = 1; - } + free(ret); + ret = se; + neg = 1; } while (s->type == TOK_INFIX && (s->function == pow)) {