DIY Trigonometry Functions in C

  The DIY Trigonometry Functions project isn't a practical tool or utility, it's just an educational pursuit to understand how computers handle trigonometry functions. In practice, embedded systems use something called CORDIC, which uses matrix translations to avoid more complex hardware functions, however for this project I will be using Taylor Series as it's simpler and more fun to explore. This isn't meant to be well structured or production-level code or anything, just exploring an idea for the sake of it.

  There are two separate folders, cos, and sin; handling their functions respectively. With using a Taylor series, you can achieve a very high degree of accuracy with just a few terms, for values close to zero, however sine and cosine waves are perfect circles, so we can divide their wave into a section and translate the result to the accurate portion of the taylor series; There are two decisions to be made here to balance reasonable accuracy and computational cost, how many sections to divide the wave into, and how many terms to use? After some experimentation, I found that the "sweet spot" for divisions is dividing the wave in half, which makes the translation super simple, any input above PI is just treated as the difference between itself and PI, with the output sign flipped. As for the number of terms, I found 6 terms to be the best compromise, 4 terms has too low accuracy, while 8 terms is much more computationally expensive with negligible accuracy increases. This applied for both Sine and Cosine. This program uses Linux time.h for timekeeping, and the C standard Math.h library to compare the accuracy of our results from 0 to 2PI with a step size of 0.0000001, the results are

Sine, with a taylor series of 6 terms has an accuracy of 0.000021 Radians.
Cosine, with a taylor series of 6 terms, has an accuracy of 0.0001 Radians

Which, for my purpose, is more than good enough, especially with my PC completing over 62 million of these calculations in under 1.5 seconds.

This project, and the code was heavily inspired by the blog post from Austin Z. Henley,
"Implementing cosine in C from scratch."

Source Code

Find and Replace CLI

  The Find and Replace CLI tool is a tool made using the C Standard Library. The tool works by specifying a filename, a string to replace, and the replacment string.

IE: ./fnr filename.txt "old string" "new string"

The program replaces all instances of the old string with the new string in the entire file. There are few simple catches for invalid quantity of arguments, and inability to find specified files by name.

  How it works: The program works by creating a buffer of the files contents, and then indexing the occurances of the string to be replaced; then a new buffer is created, at the specified size of the old buffer, minus the size of the string to be replaced multiplied by the number of occurances, plus the string to replace multiplied by the number of occurances. Then, within a loop, all characters of the old buffer are copied over to the new buffer, until the next occurance in the index is hit, in which the new string is copied over to the new buffer, the old buffers iterator is incremented by the size of the old string, and the index is incremented by one.

Source Code

Snake Game

  The Snake Game project is a small simple game made in C using the SDL2 library. The game loop goes as follows, you start as a single white cube, consistently moving in a single direction using the arrow keys you can change the direction the snake moves. The objective is to have the snake move towards and "eat" the green apples that spawn. If the snake hits the bounds of the map, or itself, the game is reset.

Source Code