From 171d80ac1e5bd006c2c600f36a42440dfb635605 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 18 Apr 2025 15:52:21 -0400 Subject: Make Int.prev_prime() optional instead of erroring --- src/environment.c | 2 +- src/stdlib/integers.c | 4 ++-- src/stdlib/integers.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/environment.c b/src/environment.c index 35c76fe5..651bc9ca 100644 --- a/src/environment.c +++ b/src/environment.c @@ -121,7 +121,7 @@ env_t *global_env(bool source_mapping) {"power", "Int$power", "func(base:Int,exponent:Int -> Int)"}, #if __GNU_MP_VERSION >= 6 #if __GNU_MP_VERSION_MINOR >= 3 - {"prev_prime", "Int$prev_prime", "func(x:Int -> Int)"}, + {"prev_prime", "Int$prev_prime", "func(x:Int -> Int?)"}, #endif #endif {"right_shifted", "Int$right_shifted", "func(x,y:Int -> Int)"}, diff --git a/src/stdlib/integers.c b/src/stdlib/integers.c index b4ca69d3..bb39d99c 100644 --- a/src/stdlib/integers.c +++ b/src/stdlib/integers.c @@ -434,12 +434,12 @@ public Int_t Int$next_prime(Int_t x) #if __GNU_MP_VERSION >= 6 #if __GNU_MP_VERSION_MINOR >= 3 -public Int_t Int$prev_prime(Int_t x) +public OptionalInt_t Int$prev_prime(Int_t x) { mpz_t p; mpz_init_set_int(p, x); if (unlikely(mpz_prevprime(p, p) == 0)) - fail("There is no prime number before ", x); + return NONE_INT; return Int$from_mpz(p); } #endif diff --git a/src/stdlib/integers.h b/src/stdlib/integers.h index c7643a8d..7b7cf12e 100644 --- a/src/stdlib/integers.h +++ b/src/stdlib/integers.h @@ -144,7 +144,7 @@ bool Int$is_prime(Int_t x, Int_t reps); Int_t Int$next_prime(Int_t x); #if __GNU_MP_VERSION >= 6 #if __GNU_MP_VERSION_MINOR >= 3 -Int_t Int$prev_prime(Int_t x); +OptionalInt_t Int$prev_prime(Int_t x); #endif #endif Int_t Int$choose(Int_t n, Int_t k); -- cgit v1.2.3