diff --git a/README.md b/README.md index a643fab..b74e492 100644 --- a/README.md +++ b/README.md @@ -327,6 +327,9 @@ instructions. The program will "throw darts at a target," calculating an approximation of PI by tracking how many darts "hit the target" versus the total number of darts "thrown". +The [SINE](./projects/SINE/README.md) project stresses floating point +math and functions. + The [SNOW](./projects/snow/README.md) project uses 1970's era tech to animate a simple particle system. This project demonstrates a reasonable design process of breaking down complex problems into simpler parts. diff --git a/README.pdf b/README.pdf index 97797d3..b8bf785 100644 Binary files a/README.pdf and b/README.pdf differ diff --git a/projects/README.md b/projects/README.md index 41bac84..ea709ee 100644 --- a/projects/README.md +++ b/projects/README.md @@ -8,5 +8,6 @@ Here are a number of challenges for you to flex your new found knowledge. | [FIRST](./first_project/README.md) | A good first project. | | [FIZZBUZZ](../section_1/fizzbuzz/) | The interview question. A video is also provided. | | [PI](./PI/README.md) | Compute an approximation of PI using stochastic (random) methods. | +| [SINE](./SINE/README.md) | Stresses functions and floating point math. | | [SNOW](./snow/README.md) | A fun little animation. | | [WALKIES](./walkies/README.md) | A fun little animation. | diff --git a/projects/README.pdf b/projects/README.pdf index 63a9444..d8776ee 100644 Binary files a/projects/README.pdf and b/projects/README.pdf differ diff --git a/projects/SINE/README.md b/projects/SINE/README.md new file mode 100644 index 0000000..887be52 --- /dev/null +++ b/projects/SINE/README.md @@ -0,0 +1,77 @@ +# Compute Sine using Taylor Series + +## Overview + +This project stresses the use of floating point instructions to create +a program that computes the sine of an angle given to you in degrees +on the command line. + +## Taylor Series + +The sine of an angle given in radians can be found using the Taylor +Series: + +```text +sin x = x - x^3/3! + x^5/5! - x^7/7! ... +``` + +Notice each term flips from addition to subtraction. + +Notice each term is based on the odd integers starting at 1. + +## Command line + +You are to accept two arguments on the command line. `getopt` is not +being used here to concentrate on the floating point math. Both +arguments are therefore required. + +* The angle in degrees whose sine you wish to calculate. Take this to + be a double. + +* The number of terms to evaluate. The number of terms must lie between + 1 and 10 inclusive. + +## C version + +To assist your efforts, [here](./c_version.c) is a version of this +project written in C. + +## Errors to stderr + +Error messages must be sent to `stderr`. + +If you are using the convergence macros to allow your program to build +on both Apple Silicon Mac OS and Linux, note the special casing needed +to deal with `stderr`. If this is you, compile the C version on Mac OS +with the `-S` compiler option to see the generated assembly language and +search for `stderr`. + +## Sample executions + +```text +SINE % ./a.out 0 8 +The sine of 0.00 degrees is 0.000000 in radians. +SINE % ./a.out 90 8 +The sine of 90.00 degrees is 1.000000 in radians. +SINE % ./a.out 180 8 +The sine of 180.00 degrees is -0.000001 in radians. +SINE % ./a.out 180 82 +Number of terms is out of range. +SINE % ./a.out 180 -10 +Number of terms is out of range. +SINE % echo $? +1 +``` + +## CSC3510 + +The following applies to Carthage College CSC3510 students. + +### Work rules + +Work is to be done solo. + +### What to hand in + +Just the .S file. **Your name must be at the top of the file.** + diff --git a/projects/SINE/README.pdf b/projects/SINE/README.pdf new file mode 100644 index 0000000..eba1271 Binary files /dev/null and b/projects/SINE/README.pdf differ diff --git a/projects/SINE/c_version.c b/projects/SINE/c_version.c new file mode 100644 index 0000000..9c47c14 --- /dev/null +++ b/projects/SINE/c_version.c @@ -0,0 +1,67 @@ +#include +#include + +double pi = 3.14159265359; + +double D2R(double d) { + return d * pi / 180.0; +} + +long Factorial(int n) { + long retval = 1; + + if (n > 0) { + while (n > 1) { + retval = retval * n--; + } + } + return retval; +} + +double IntegerPower(double b, int e) { + double retval = 1.0; + if (e > 0) { + while (e > 0) { + retval = retval * b; + e--; + } + } + return retval; +} + + +int main(int argc, char ** argv) { + double sin = 0.0; + + if (argc != 3) { + fprintf(stderr, "Two numerical arguments must be given.\n"); + return 1; + } + + double angle = atof(argv[1]); + int terms = atoi(argv[2]); + + if (terms < 1 || terms > 10) { + fprintf(stderr, "Number of terms is out of range.\n"); + return 1; + } + + double r_angle = D2R(angle); + + for (int term = 0, base = 1; term < terms; term++, base += 2) { + double toggle = (term & 1) ? -1.0 : 1.0; + + sin += toggle * + IntegerPower(r_angle, base) / Factorial(base); + /* + if (toggle > 0) { + printf("adding %d p/b intermediate: %f\n", base, sin); + } else { + printf("subtracting %d p/b intermediate: %f\n", base, sin); + } + */ + } + printf("The sine of %.2f degrees is %f in radians.\n", angle, sin); + + return 0; +} \ No newline at end of file