aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2024-02-06 16:45:04 +0300
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2024-02-06 16:45:04 +0300
commit21c5be3706f70d573f8d0195760846fce46f6807 (patch)
tree9240d238d5b183bdaf57ecad69c10595736690e0 /src
parent37eef3cf0ab0b0ed3dabc1c96515f4e30011f9db (diff)
downloadsciteco-21c5be3706f70d573f8d0195760846fce46f6807.tar.gz
fixed the power (^*) operator: did not handle corner cases and was inefficient
* in fact, with a negative exponent the previous naive implementation would even hang indefinitely! * Now uses the squaring algorithm. This is slightly longer but significantly more efficient. * added test cases
Diffstat (limited to 'src')
-rw-r--r--src/expressions.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/expressions.c b/src/expressions.c
index 93e3fdc..cec3492 100644
--- a/src/expressions.c
+++ b/src/expressions.c
@@ -184,7 +184,28 @@ teco_expressions_calc(GError **error)
switch (op) {
case TECO_OP_POW:
- for (result = 1; vright--; result *= vleft);
+ if (!vright) {
+ result = vleft < 0 ? -1 : 1;
+ break;
+ }
+ if (vright < 0) {
+ if (!vleft) {
+ g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
+ "Negative power of 0 is not defined");
+ return FALSE;
+ }
+ result = ABS(vleft) == 1 ? vleft : 0;
+ break;
+ }
+ result = 1;
+ for (;;) {
+ if (vright & 1)
+ result *= vleft;
+ vright >>= 1;
+ if (!vright)
+ break;
+ vleft *= vleft;
+ }
break;
case TECO_OP_MUL:
result = vleft * vright;