From db44dc5a31f5d3f8ab0f982052ae2e1707bbca5f Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Wed, 12 Jul 2023 11:45:30 -0700 Subject: [PATCH] Math docs, add spacing to binstr_to_int --- CMakeLists.txt | 8 +++++-- docs/binarytree.md | 2 +- docs/bool.md | 2 +- docs/input.md | 2 +- docs/linkedlist.md | 2 +- docs/math.md | 60 ++++++++++++++++++++++++++++++++++++++++++++-- docs/set.md | 2 +- docs/stack.md | 2 +- docs/utility.md | 2 +- src/math.c | 3 +++ tests/tests.c | 6 ++++- 11 files changed, 79 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 05060ae..5afca48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,10 @@ endif() if(${CMAKE_PROJECT_NAME} STREQUAL flint) add_executable(tests tests/tests.c) target_include_directories(tests PRIVATE include) - target_link_libraries(tests flint bsd) -endif() + if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") + target_link_libraries(tests flint) + else() + target_link_libraries(tests flint bsd) + endif() +endif() diff --git a/docs/binarytree.md b/docs/binarytree.md index 001cf37..330959e 100644 --- a/docs/binarytree.md +++ b/docs/binarytree.md @@ -1,4 +1,4 @@ -# lfbinarytree +# binarytree Binary tree with standard leaf operations diff --git a/docs/bool.md b/docs/bool.md index 8a8dd61..84a7989 100644 --- a/docs/bool.md +++ b/docs/bool.md @@ -1,4 +1,4 @@ -# lfbool +# bool Macro representation of truthy values diff --git a/docs/input.md b/docs/input.md index c8d350a..e2a6997 100644 --- a/docs/input.md +++ b/docs/input.md @@ -1,4 +1,4 @@ -# lfinput +# input I/O module to assist with consuming data from files diff --git a/docs/linkedlist.md b/docs/linkedlist.md index 5a8d072..58c82b7 100644 --- a/docs/linkedlist.md +++ b/docs/linkedlist.md @@ -1,4 +1,4 @@ -# lflinkedlist +# linkedlist A dual sided linked list structure. Used as the foundation for many of the structures in `libflint` diff --git a/docs/math.md b/docs/math.md index f816e20..48f1146 100644 --- a/docs/math.md +++ b/docs/math.md @@ -1,3 +1,59 @@ -# lfmath +# math -Coming soon \ No newline at end of file +General math functions + +## Functions + +## max_int + +Return the maximum integer between `int a` and `int b` + +```c +int max_int(int a, int b); +``` + +## min_int + +Return the minimum integer between `int a` and `int b` + +```c +int min_int(int a, int b); +``` + +## clamp_int + +Clamps an integer between a high and low value + +```c +int clamp_int(int i, int low, int high); +``` + +## binstr_to_int + +Converts a string representing a binary number into an integer. Supports underscores as spacing + +```c +int binstr_to_int(const char *s); + +/* Usage */ +int a = binstr_to_int("10011101"); +int b = binstr_to_int("1001_1101_0010_1011"); +``` + +## bresenham + +Uses bresenham's line algorithim to generate a line in 2D space. +Returns a pointer to an array of `Point`. +The `sz` parameter holds the size of the array. + +```c +Point *bresenham(int x0, int y0, int x1, int y1, size_t *sz); +``` + +## bresenham_p + +Works the same as `bresenham()` but uses the `Point` struct instead of `int` + +```c +Point *bresenham_p(Point p1, Point p2, size_t *sz); +``` diff --git a/docs/set.md b/docs/set.md index 8d68b40..a1e3e95 100644 --- a/docs/set.md +++ b/docs/set.md @@ -1,3 +1,3 @@ -# lfset +# set Coming soon \ No newline at end of file diff --git a/docs/stack.md b/docs/stack.md index ccb61d1..76a5d01 100644 --- a/docs/stack.md +++ b/docs/stack.md @@ -1,3 +1,3 @@ -# lfstack +# stack Coming soon \ No newline at end of file diff --git a/docs/utility.md b/docs/utility.md index 1dc0fef..b4a2890 100644 --- a/docs/utility.md +++ b/docs/utility.md @@ -1,4 +1,4 @@ -# lfutility +# utility Utility code that does not fit anywhere else diff --git a/src/math.c b/src/math.c index 8d3a98c..fd5bb58 100644 --- a/src/math.c +++ b/src/math.c @@ -29,6 +29,9 @@ int clamp_int(int i, int low, int high) { int binstr_to_int(const char *s) { int n = 0, m = 1; for (int i = (int) strlen(s) - 1; i >= 0; --i) { + if (s[i] == '_') { + continue; + } if (s[i] == '1') { n += m; } diff --git a/tests/tests.c b/tests/tests.c index c463b26..77b5a7a 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -161,10 +161,14 @@ void test_math() { printf("Between %d and %d, %d is larger\n", i, j, max_int(i, j)); printf("Between %d and %d, %d is smaller\n", i, j, min_int(i, j)); - char *s = "1010110"; + char *s = "10101101"; printf("Binary: %s\n", s); printf("Decimal: %d\n", binstr_to_int(s)); + char *s2 = "1010_1101"; + printf("Binary: %s\n", s2); + printf("Decimal: %d\n", binstr_to_int(s2)); + printf("\nGenerate line from 0,0 to 2,5\n"); size_t sz = 0; Point *line = bresenham(0, 0, 2, 5, &sz);