aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-13 16:36:41 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-13 16:36:41 -0400
commite56fd1aa94db77c6210c649c9cf8163678326355 (patch)
tree9e487108faea515f34dd6e5a3664d1272f12f74d
parente0223a4c201b42ca35c73bfad84477efc61f06b2 (diff)
Add Int:sqrt()
-rw-r--r--builtins/integers.c8
-rw-r--r--builtins/integers.h1
-rw-r--r--environment.c1
3 files changed, 10 insertions, 0 deletions
diff --git a/builtins/integers.c b/builtins/integers.c
index e55d2ab0..b7d96a90 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -302,6 +302,14 @@ public Int_t Int$power(Int_t base, Int_t exponent)
return Int$from_mpz(result);
}
+public Int_t Int$sqrt(Int_t i)
+{
+ mpz_t result;
+ mpz_init_set_int(result, i);
+ mpz_sqrt(result, result);
+ 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)
diff --git a/builtins/integers.h b/builtins/integers.h
index 8250e0dc..98b58b0f 100644
--- a/builtins/integers.h
+++ b/builtins/integers.h
@@ -60,6 +60,7 @@ Range_t Int$to(Int_t from, Int_t to);
Int_t Int$from_text(CORD text);
Int_t Int$abs(Int_t x);
Int_t Int$power(Int_t base, Int_t exponent);
+Int_t Int$sqrt(Int_t i);
#define BIGGEST_SMALL_INT ((1<<29)-1)
diff --git a/environment.c b/environment.c
index c8056cc5..95cd4673 100644
--- a/environment.c
+++ b/environment.c
@@ -107,6 +107,7 @@ env_t *new_compilation_unit(CORD *libname)
{"negative", "Int$negative", "func(x:Int)->Int"},
{"negated", "Int$negated", "func(x:Int)->Int"},
{"abs", "Int$abs", "func(x:Int)->Int"},
+ {"sqrt", "Int$sqrt", "func(x:Int)->Int"},
{"power", "Int$power", "func(base:Int,exponent:Int)->Int"},
)},
{"Int64", Type(IntType, .bits=64), "Int64_t", "$Int64", TypedArray(ns_entry_t,