From e8364e95ee1b38e49af29a2aa1338b3d978411f0 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Thu, 29 Sep 2022 16:39:09 -0400 Subject: Trigonometry somewhat works --- .gitignore | 1 + cosine.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ format.h | 19 +++++++++++++------ sine.c | 2 +- testData.h | 16 ++++++++-------- tools/gen-trig-test.c | 24 +++++++++++------------- 6 files changed, 82 insertions(+), 28 deletions(-) create mode 100644 cosine.c diff --git a/.gitignore b/.gitignore index 782cf6d..7145c97 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.o +*.out *.rej *.orig tags diff --git a/cosine.c b/cosine.c new file mode 100644 index 0000000..58bd189 --- /dev/null +++ b/cosine.c @@ -0,0 +1,48 @@ +/* cosine.c + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * wrote this file. As long as you retain this notice + * you can do whatever you want with this stuff. If we meet some day, and you + * think this stuff is worth it, you can buy me a beer in return. + * Benjamin Chausse + * ---------------------------------------------------------------------------- + */ +#include +#include "format.h" +#include "testData.h" + +float cos(float input, int precision){ + float ttl = 1; + int denom = 1; + float num; + for (int i=2;i<=(2*precision);i+=2){ + num = mpow(input,i); + denom *= i*(i-1); + // ttl += num/denom; + ttl += ( (i/2)%2 == 0 ) ? num/denom : -num/denom; + } + return ttl; +} + +int main(){ + const int precision = 16; + const float threshold = 1e-4; + + for (int i=0; ithreshold ){ + printf("ERROR: \n" + "Deviation between calculated value and expected value\n" + "surpasses threshold of %f for number %f\n" + "Expected: %f but got: %f\n", + threshold, + piValues[i][0], + piValues[i][2], + cos(piValues[i][0],precision)); + return 1; + } else { + printf("%f IS ALL GOOD!\n",piValues[i][0]); + } + } + + return 0; +} diff --git a/format.h b/format.h index d0cf95a..3d51c3c 100644 --- a/format.h +++ b/format.h @@ -10,6 +10,7 @@ #define TRUE 1 #define FALSE 0 + #define COUNT_OF( arr) (sizeof(arr)/sizeof(0[arr])) // Conversion table between uppercase and lowercase @@ -20,12 +21,14 @@ const char caps[2][26] = { 'n','o','p','q','r','s','t','u','v','w','x','y','z'} }; -int pow(int b, int e){ - int result =1; - for (int i = 0; i0) ? i : -i; +} diff --git a/sine.c b/sine.c index 871acc0..204ab41 100644 --- a/sine.c +++ b/sine.c @@ -21,7 +21,7 @@ float sin(float input, int precision){ for (int i=3;i<(2*precision)+2;i+=2) { num = mpow(input,i); denom *= i*(i-1); - ttl += ((i-1)/2 %2 == 1) ? -1*num/denom : num/denom; + ttl += ((i-1)/2 %2 == 1) ? -num/denom : num/denom; } return ttl; } diff --git a/testData.h b/testData.h index 9bf5e08..02b23c5 100644 --- a/testData.h +++ b/testData.h @@ -17,7 +17,7 @@ // - Trailing spaces -> sis, heightened, minim // - Spaces before the words -> sis, unholly, pop // - Non latin alphabet (greek & russian) -char *wrd[] = { +const char *wrd[] = { "abba", "betrothed", "deified", "elevation", "genesis", "kayak", "murdum", " pop", "relinquish", "stats", "thirst", "abhorrent", "accentuated", "agoraphobia", " aibohphobia", "anemia", "bIb", "bittersweet", "bob", @@ -40,7 +40,7 @@ char *wrd[] = { * - Negative values * - Values surpassing 2π */ -float piValues[20][3] = { +const float piValues[20][3] = { // test-value sin cos { 0.000000, 0.000000, 1.000000}, // 0° { 0.523599, 0.500000, 0.866025}, // 30° @@ -72,37 +72,37 @@ float piValues[20][3] = { * - Identity matrix * */ -int mtrxA[3][3] = { +const int mtrxA[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; -int mtrxB[3][3] = { +const int mtrxB[3][3] = { {3, 1, 4}, {1, 5, 9}, {2, 6, 5} }; -int mtrxNULL[3][3] = { +const int mtrxNULL[3][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }; -int mtrxID[3][3] = { +const int mtrxID[3][3] = { {1, 0, 0}, {0, 1, 0}, {0, 0, 1} }; -int mtrxWide[3][5] = { +const int mtrxWide[3][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15} }; -int mtrxTall[5][3] = { +const int mtrxTall[5][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, diff --git a/tools/gen-trig-test.c b/tools/gen-trig-test.c index 365a90d..54b39bf 100644 --- a/tools/gen-trig-test.c +++ b/tools/gen-trig-test.c @@ -11,25 +11,24 @@ #include #include -/* This code was used to generate floating numbers found in testData.h - * It's purpose is to generate a list of expected outputs for the sine - * and cosine function with a given number. It uses the math.h library +/* This code was used to generate double precision floating point numbers found + * in testData.h It's purpose is to generate a list of expected outputs for the + * sine and cosine function with a given number. It uses the math.h library * which cannot be used for the assignment and which is therefore a great * subject to test and compare with. * - * The generated [][3]int list is organized as follows: - * { original_value, expected_sine, expected_cosine } + * The generated [][3]int list is organized as follows: { original_value, + * expected_sine, expected_cosine } * - * Values chosen here test the following edge cases, - * - Common trigonometric circle identities (0°,30°,45°,60°,90°, etc...) - * - Negative values - * - Values surpassing 2π + * Values chosen here test the following edge cases, - Common trigonometric + * circle identities (0°,30°,45°,60°,90°, etc...) - Negative values - Values + * surpassing 2π */ int main(){ // list of number {{{ - float lst[20] = { + double lst[20] = { 0, // 0° M_PI/6, // 30° M_PI/4, // 45° @@ -56,17 +55,16 @@ int main(){ printf("int piValues[][] = {\n"); int max = (sizeof(lst)/sizeof(lst[0]))-1; - printf("%d",max); for (int i=0; i