add clamp_int

This commit is contained in:
Evan Burkey 2022-03-21 14:32:31 -07:00
parent 7c37a7bd28
commit 91e2ac1994
2 changed files with 10 additions and 0 deletions

View File

@ -3,6 +3,7 @@
int max_int(int a, int b); int max_int(int a, int b);
int min_int(int a, int b); int min_int(int a, int b);
int clamp_int(int i, int low, int high);
int binstr_to_int(const char *s); int binstr_to_int(const char *s);
#endif // LIBFLINT_H_MATH #endif // LIBFLINT_H_MATH

View File

@ -16,6 +16,15 @@ int min_int(int a, int b) {
return b; return b;
} }
int clamp_int(int i, int low, int high) {
if (i > high) {
return high;
} else if (i < low) {
return low;
}
return i;
}
int binstr_to_int(const char *s) { int binstr_to_int(const char *s) {
int n = 0, m = 1; int n = 0, m = 1;
for (int i = (int)strlen(s) - 1; i >= 0; --i) { for (int i = (int)strlen(s) - 1; i >= 0; --i) {