diff --git a/src/tinyexpr.cpp b/src/tinyexpr.cpp index e16b87b6f..8c22259c7 100644 --- a/src/tinyexpr.cpp +++ b/src/tinyexpr.cpp @@ -444,11 +444,23 @@ static te_expr *factor(state *s) { /* = {"^" } */ te_expr *ret = power(s); + te_expr *insertion = 0; + while (s->type == TOK_INFIX && (s->function == (const void *)(te_fun2)pow)) { te_fun2 t = (te_fun2)s->function; next_token(s); - ret = NEW_EXPR(TE_FUNCTION2, ret, power(s)); - ret->function = (const void *)t; + + if (insertion) { + /* Make exponentiation go right-to-left. */ + te_expr *insert = NEW_EXPR(TE_FUNCTION2, insertion->parameters[1], power(s)); + insert->function = (const void *)t; + insertion->parameters[1] = insert; + insertion = insert; + } else { + ret = NEW_EXPR(TE_FUNCTION2, ret, power(s)); + ret->function = (const void *)t; + insertion = ret; + } } return ret; diff --git a/tests/checks/math.fish b/tests/checks/math.fish index a4fa92f69..321a03a15 100644 --- a/tests/checks/math.fish +++ b/tests/checks/math.fish @@ -59,6 +59,11 @@ math '-10^15' # CHECK: 100000000000000 # CHECK: -1000000000000000 +math 3^0.5^2 +math -2^2 +# CHECK: 1.316074 +# CHECK: 4 + math -s0 '1.0 / 2.0' math -s0 '3.0 / 2.0' math -s0 '10^15 / 2.0'