aboutsummaryrefslogtreecommitdiff
path: root/builtins/integers.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-13 16:32:00 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-13 16:32:00 -0400
commite0223a4c201b42ca35c73bfad84477efc61f06b2 (patch)
tree7f83903c19433c67e102a8688c3af02fd63eab0a /builtins/integers.c
parentaaac5dff28b2fa4835dd9a3a352f274069a7b432 (diff)
Support ^ exponentiation for integers
Diffstat (limited to 'builtins/integers.c')
-rw-r--r--builtins/integers.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/builtins/integers.c b/builtins/integers.c
index 53e38aab..e55d2ab0 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -291,6 +291,17 @@ public Int_t Int$abs(Int_t x)
return Int$from_mpz(result);
}
+public Int_t Int$power(Int_t base, Int_t exponent)
+{
+ int64_t exp = Int$as_i64(exponent);
+ if (__builtin_expect(exp < 0, 0))
+ fail("Cannot take a negative power of an integer!");
+ mpz_t result;
+ mpz_init_set_int(result, base);
+ mpz_pow_ui(result, result, exp);
+ return Int$from_mpz(result);
+}
+
public Int_t Int$random(Int_t min, Int_t max) {
int32_t cmp = Int$compare(&min, &max, &$Int);
if (cmp > 0)