Math docs, add spacing to binstr_to_int
This commit is contained in:
parent
a029765cf4
commit
db44dc5a31
@ -22,6 +22,10 @@ endif()
|
|||||||
if(${CMAKE_PROJECT_NAME} STREQUAL flint)
|
if(${CMAKE_PROJECT_NAME} STREQUAL flint)
|
||||||
add_executable(tests tests/tests.c)
|
add_executable(tests tests/tests.c)
|
||||||
target_include_directories(tests PRIVATE include)
|
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()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# lfbinarytree
|
# binarytree
|
||||||
|
|
||||||
Binary tree with standard leaf operations
|
Binary tree with standard leaf operations
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# lfbool
|
# bool
|
||||||
|
|
||||||
Macro representation of truthy values
|
Macro representation of truthy values
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# lfinput
|
# input
|
||||||
|
|
||||||
I/O module to assist with consuming data from files
|
I/O module to assist with consuming data from files
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# lflinkedlist
|
# linkedlist
|
||||||
|
|
||||||
A dual sided linked list structure. Used as the foundation for many of the structures in `libflint`
|
A dual sided linked list structure. Used as the foundation for many of the structures in `libflint`
|
||||||
|
|
||||||
|
60
docs/math.md
60
docs/math.md
@ -1,3 +1,59 @@
|
|||||||
# lfmath
|
# math
|
||||||
|
|
||||||
Coming soon
|
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);
|
||||||
|
```
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
# lfset
|
# set
|
||||||
|
|
||||||
Coming soon
|
Coming soon
|
@ -1,3 +1,3 @@
|
|||||||
# lfstack
|
# stack
|
||||||
|
|
||||||
Coming soon
|
Coming soon
|
@ -1,4 +1,4 @@
|
|||||||
# lfutility
|
# utility
|
||||||
|
|
||||||
Utility code that does not fit anywhere else
|
Utility code that does not fit anywhere else
|
||||||
|
|
||||||
|
@ -29,6 +29,9 @@ int clamp_int(int i, int low, int high) {
|
|||||||
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) {
|
||||||
|
if (s[i] == '_') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (s[i] == '1') {
|
if (s[i] == '1') {
|
||||||
n += m;
|
n += m;
|
||||||
}
|
}
|
||||||
|
@ -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 larger\n", i, j, max_int(i, j));
|
||||||
printf("Between %d and %d, %d is smaller\n", i, j, min_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("Binary: %s\n", s);
|
||||||
printf("Decimal: %d\n", binstr_to_int(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");
|
printf("\nGenerate line from 0,0 to 2,5\n");
|
||||||
size_t sz = 0;
|
size_t sz = 0;
|
||||||
Point *line = bresenham(0, 0, 2, 5, &sz);
|
Point *line = bresenham(0, 0, 2, 5, &sz);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user