diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-04-07 01:17:02 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-04-07 01:17:02 -0400 |
| commit | f857f38f718fff586e373815a1bcad2701b4d983 (patch) | |
| tree | 29b3e367437099f39f6fc9a4e7d1dd2f177afb59 | |
| parent | fb4e4cef1382a5e37d99ccb8e97fe1d2a8cd7e93 (diff) | |
Add `is_between()` for various types
| -rw-r--r-- | docs/bytes.md | 27 | ||||
| -rw-r--r-- | docs/integers.md | 27 | ||||
| -rw-r--r-- | docs/nums.md | 27 | ||||
| -rw-r--r-- | src/environment.c | 8 | ||||
| -rw-r--r-- | src/stdlib/bytes.h | 3 | ||||
| -rw-r--r-- | src/stdlib/integers.h | 6 | ||||
| -rw-r--r-- | src/stdlib/nums.h | 6 | ||||
| -rw-r--r-- | test/integers.tm | 7 |
8 files changed, 111 insertions, 0 deletions
diff --git a/docs/bytes.md b/docs/bytes.md index 7cbf7b0c..2e139622 100644 --- a/docs/bytes.md +++ b/docs/bytes.md @@ -10,6 +10,7 @@ the `Byte()` constructor: `Byte(5)`. # Byte Methods - [`func hex(byte: Byte, uppercase=no, prefix=yes -> Text)`](#hex) +- [`func is_between(x: Byte, low: Byte, high: Byte -> Bool)`](#is_between) - [`func parse(text: Text -> Byte?)`](#parse) - [`func to(first: Byte, last: Byte, step: Int8? = none -> Text)`](#to) @@ -21,6 +22,32 @@ TODO: write docs --------- +### `is_between` +Determines if an integer is between two numbers (inclusive). + +```tomo +func is_between(x: Byte, low: Byte, high: Byte -> Bool) +``` + +- `x`: The integer to be checked. +- `low`: The lower bound to check (inclusive). +- `high`: The upper bound to check (inclusive). + +**Returns:** +`yes` if `low <= x and x <= high`, otherwise `no` + +**Example:** +```tomo +>> Byte(7).is_between(1, 10) += yes +>> Byte(7).is_between(100, 200) += no +>> Byte(7).is_between(1, 7) += yes +``` + +--- + ## `parse` TODO: write docs diff --git a/docs/integers.md b/docs/integers.md index 84a2d1a2..1ee47cbd 100644 --- a/docs/integers.md +++ b/docs/integers.md @@ -128,6 +128,7 @@ can be called either on the type itself: `Int.sqrt(x)` or as a method call: - [`func factorial(n: Int -> Text)`](#factorial) - [`func format(i: Int, digits: Int = 0 -> Text)`](#format) - [`func hex(i: Int, digits: Int = 0, uppercase: Bool = yes, prefix: Bool = yes -> Text)`](#hex) +- [`func is_between(x: Int, low: Int, high: Int -> Bool)`](#is_between) - [`func is_prime(x: Int, reps: Int = 50 -> Bool)`](#is_prime) - [`func next_prime(x: Int -> Int)`](#next_prime) - [`func octal(i: Int, digits: Int = 0, prefix: Bool = yes -> Text)`](#octal) @@ -268,6 +269,32 @@ The hexadecimal string representation of the integer. --- +### `is_between` +Determines if an integer is between two numbers (inclusive). + +```tomo +func is_between(x: Int, low: Int, high: Int -> Bool) +``` + +- `x`: The integer to be checked. +- `low`: The lower bound to check (inclusive). +- `high`: The upper bound to check (inclusive). + +**Returns:** +`yes` if `low <= x and x <= high`, otherwise `no` + +**Example:** +```tomo +>> (7).is_between(1, 10) += yes +>> (7).is_between(100, 200) += no +>> (7).is_between(1, 7) += yes +``` + +--- + ### `is_prime` Determines if an integer is a prime number. diff --git a/docs/nums.md b/docs/nums.md index 1cc3782a..62cf3564 100644 --- a/docs/nums.md +++ b/docs/nums.md @@ -143,6 +143,7 @@ called either on the type itself: `Num.sqrt(x)` or as a method call: - [`func format(n: Num, precision: Int = 0 -> Text)`](#format) - [`func hypot(x: Num, y: Num -> Num)`](#hypot) - [`func isfinite(n: Num -> Bool)`](#isfinite) +- [`func is_between(n: Num, low: Num, high: Num -> Bool)`](#is_between) - [`func isinf(n: Num -> Bool)`](#isinf) - [`func j0(x: Num -> Num)`](#j0) - [`func j1(x: Num -> Num)`](#j1) @@ -662,6 +663,32 @@ func isfinite(n: Num -> Bool) --- +### `is_between` +Determines if a number is between two numbers (inclusive). + +```tomo +func is_between(x: Num, low: Num, high: Num -> Bool) +``` + +- `x`: The integer to be checked. +- `low`: The lower bound to check (inclusive). +- `high`: The upper bound to check (inclusive). + +**Returns:** +`yes` if `low <= x and x <= high`, otherwise `no` + +**Example:** +```tomo +>> (7.5).is_between(1, 10) += yes +>> (7.5).is_between(100, 200) += no +>> (7.5).is_between(1, 7.5) += yes +``` + +--- + ### `isinf` Checks if a number is infinite. diff --git a/src/environment.c b/src/environment.c index 927f2e87..f32471a4 100644 --- a/src/environment.c +++ b/src/environment.c @@ -87,6 +87,7 @@ env_t *global_env(void) {"Byte", Type(ByteType), "Byte_t", "Byte$info", TypedList(ns_entry_t, {"max", "Byte$max", "Byte"}, {"hex", "Byte$hex", "func(byte:Byte, uppercase=yes, prefix=no -> Text)"}, + {"is_between", "Byte$is_between", "func(x:Byte,low:Byte,high:Byte -> Bool)"}, {"min", "Byte$min", "Byte"}, {"to", "Byte$to", "func(first:Byte,last:Byte,step:Int8?=none -> func(->Byte?))"}, )}, @@ -102,6 +103,7 @@ env_t *global_env(void) {"format", "Int$format", "func(i:Int, digits=0 -> Text)"}, {"gcd", "Int$gcd", "func(x,y:Int -> Int)"}, {"hex", "Int$hex", "func(i:Int, digits=0, uppercase=yes, prefix=yes -> Text)"}, + {"is_between", "Int$is_between", "func(x:Int,low:Int,high:Int -> Bool)"}, {"is_prime", "Int$is_prime", "func(x:Int,reps=50 -> Bool)"}, {"left_shifted", "Int$left_shifted", "func(x,y:Int -> Int)"}, {"minus", "Int$minus", "func(x,y:Int -> Int)"}, @@ -134,6 +136,7 @@ env_t *global_env(void) {"gcd", "Int64$gcd", "func(x,y:Int64 -> Int64)"}, {"parse", "Int64$parse", "func(text:Text -> Int64?)"}, {"hex", "Int64$hex", "func(i:Int64, digits=0, uppercase=yes, prefix=yes -> Text)"}, + {"is_between", "Int64$is_between", "func(x:Int64,low:Int64,high:Int64 -> Bool)"}, {"max", "Int64$max", "Int64"}, {"min", "Int64$min", "Int64"}, {"modulo", "Int64$modulo", "func(x,y:Int64 -> Int64)"}, @@ -155,6 +158,7 @@ env_t *global_env(void) {"gcd", "Int32$gcd", "func(x,y:Int32 -> Int32)"}, {"parse", "Int32$parse", "func(text:Text -> Int32?)"}, {"hex", "Int32$hex", "func(i:Int32, digits=0, uppercase=yes, prefix=yes -> Text)"}, + {"is_between", "Int32$is_between", "func(x:Int32,low:Int32,high:Int32 -> Bool)"}, {"max", "Int32$max", "Int32"}, {"min", "Int32$min", "Int32"}, {"modulo", "Int32$modulo", "func(x,y:Int32 -> Int32)"}, @@ -176,6 +180,7 @@ env_t *global_env(void) {"gcd", "Int16$gcd", "func(x,y:Int16 -> Int16)"}, {"parse", "Int16$parse", "func(text:Text -> Int16?)"}, {"hex", "Int16$hex", "func(i:Int16, digits=0, uppercase=yes, prefix=yes -> Text)"}, + {"is_between", "Int16$is_between", "func(x:Int16,low:Int16,high:Int16 -> Bool)"}, {"max", "Int16$max", "Int16"}, {"min", "Int16$min", "Int16"}, {"modulo", "Int16$modulo", "func(x,y:Int16 -> Int16)"}, @@ -197,6 +202,7 @@ env_t *global_env(void) {"gcd", "Int8$gcd", "func(x,y:Int8 -> Int8)"}, {"parse", "Int8$parse", "func(text:Text -> Int8?)"}, {"hex", "Int8$hex", "func(i:Int8, digits=0, uppercase=yes, prefix=yes -> Text)"}, + {"is_between", "Int8$is_between", "func(x:Int8,low:Int8,high:Int8 -> Bool)"}, {"max", "Int8$max", "Int8"}, {"min", "Int8$min", "Int8"}, {"modulo", "Int8$modulo", "func(x,y:Int8 -> Int8)"}, @@ -219,6 +225,7 @@ env_t *global_env(void) {"format", "Num$format", "func(n:Num, precision=16 -> Text)"}, {"scientific", "Num$scientific", "func(n:Num,precision=0 -> Text)"}, {"percent", "Num$percent", "func(n:Num,precision=0 -> Text)"}, + {"is_between", "Num$is_between", "func(x:Num,low:Num,high:Num -> Bool)"}, {"isinf", "Num$isinf", "func(n:Num -> Bool)"}, {"isfinite", "Num$isfinite", "func(n:Num -> Bool)"}, {"modulo", "Num$mod", "func(x,y:Num -> Num)"}, @@ -251,6 +258,7 @@ env_t *global_env(void) {"format", "Num32$format", "func(n:Num32, precision=8 -> Text)"}, {"scientific", "Num32$scientific", "func(n:Num32, precision=0 -> Text)"}, {"percent", "Num32$percent", "func(n:Num32,precision=0 -> Text)"}, + {"is_between", "Num32$is_between", "func(x:Num32,low:Num32,high:Num32 -> Bool)"}, {"isinf", "Num32$isinf", "func(n:Num32 -> Bool)"}, {"isfinite", "Num32$isfinite", "func(n:Num32 -> Bool)"}, C(2_SQRTPI), C(E), C(PI_2), C(2_PI), C(1_PI), C(LN10), C(LN2), C(LOG2E), diff --git a/src/stdlib/bytes.h b/src/stdlib/bytes.h index f875e7cb..2794d1d9 100644 --- a/src/stdlib/bytes.h +++ b/src/stdlib/bytes.h @@ -22,6 +22,9 @@ Byte_t Byte$from_int16(int16_t i, bool truncate); Closure_t Byte$to(Byte_t first, Byte_t last, OptionalInt8_t step); MACROLIKE Byte_t Byte$from_int8(int8_t i) { return (Byte_t)i; } MACROLIKE Byte_t Byte$from_bool(bool b) { return (Byte_t)b; } +MACROLIKE CONSTFUNC bool Byte$is_between(const Byte_t x, const Byte_t low, const Byte_t high) { + return low <= x && x <= high; +} extern const Byte_t Byte$min; extern const Byte_t Byte$max; diff --git a/src/stdlib/integers.h b/src/stdlib/integers.h index 8988e7c8..12fec62b 100644 --- a/src/stdlib/integers.h +++ b/src/stdlib/integers.h @@ -33,6 +33,9 @@ Closure_t type_name ## $to(c_type first, c_type last, Optional ## type_name ## _t step); \ Closure_t type_name ## $onward(c_type first, c_type step); \ PUREFUNC Optional ## type_name ## _t type_name ## $parse(Text_t text); \ + MACROLIKE CONSTFUNC bool type_name ## $is_between(const c_type x, const c_type low, const c_type high) { \ + return low <= x && x <= high; \ + } \ MACROLIKE PUREFUNC c_type type_name ## $clamped(c_type x, c_type min, c_type max) { \ return x < min ? min : (x > max ? max : x); \ } \ @@ -93,6 +96,9 @@ Text_t Int$value_as_text(Int_t i); PUREFUNC uint64_t Int$hash(const void *x, const TypeInfo_t *type); PUREFUNC int32_t Int$compare(const void *x, const void *y, const TypeInfo_t *type); PUREFUNC int32_t Int$compare_value(const Int_t x, const Int_t y); +MACROLIKE CONSTFUNC bool Int$is_between(const Int_t x, const Int_t low, const Int_t high) { + return Int$compare_value(low, x) <= 0 && Int$compare_value(x, high) <= 0; +} PUREFUNC bool Int$equal(const void *x, const void *y, const TypeInfo_t *type); PUREFUNC bool Int$equal_value(const Int_t x, const Int_t y); Text_t Int$format(Int_t i, Int_t digits); diff --git a/src/stdlib/nums.h b/src/stdlib/nums.h index f3de9dc5..84d87dd5 100644 --- a/src/stdlib/nums.h +++ b/src/stdlib/nums.h @@ -32,6 +32,9 @@ CONSTFUNC bool Num$isnan(double n); double Num$nan(Text_t tag); CONSTFUNC double Num$mix(double amount, double x, double y); OptionalNum_t Num$parse(Text_t text); +MACROLIKE CONSTFUNC bool Num$is_between(const double x, const double low, const double high) { + return low <= x && x <= high; +} MACROLIKE CONSTFUNC double Num$clamped(double x, double low, double high) { return (x <= low) ? low : (x >= high ? high : x); } @@ -88,6 +91,9 @@ CONSTFUNC bool Num32$isnan(float n); CONSTFUNC float Num32$mix(float amount, float x, float y); OptionalNum32_t Num32$parse(Text_t text); float Num32$nan(Text_t tag); +MACROLIKE CONSTFUNC bool Num32$is_between(const float x, const float low, const float high) { + return low <= x && x <= high; +} MACROLIKE CONSTFUNC float Num32$clamped(float x, float low, float high) { return (x <= low) ? low : (x >= high ? high : x); } diff --git a/test/integers.tm b/test/integers.tm index 72a195b4..202c1e06 100644 --- a/test/integers.tm +++ b/test/integers.tm @@ -136,3 +136,10 @@ func main() >> (4).factorial() = 24 + + >> (3).is_between(1, 5) + = yes + >> (3).is_between(1, 3) + = yes + >> (3).is_between(100, 200) + = no |
