From 1d4ad135a43747041afb58f3cce586ceab94e39a Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Thu, 29 Sep 2022 16:57:25 -0400 Subject: clean up code --- cosine.c | 11 +++++++---- format.h | 6 +++--- sine.c | 10 ++++++---- tools/gen-trig-test.c | 19 ++++++++++--------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/cosine.c b/cosine.c index 28996f0..85e062a 100644 --- a/cosine.c +++ b/cosine.c @@ -11,14 +11,15 @@ #include "format.h" #include "testData.h" +const int DEBUG = FALSE; + double cos(double input, int precision){ double ttl = 1; int denom = 1; double num; for (int i=2;i<=(2*precision);i+=2){ - num = mpow(input,i); + num = pow(input,i); denom *= i*(i-1); - // ttl += num/denom; ttl += ( (i/2)%2 == 0 ) ? num/denom : -num/denom; } return ttl; @@ -40,9 +41,11 @@ int main(){ cos(piValues[i][0],precision)); return 1; } else { - printf("%f IS ALL GOOD!\n",piValues[i][0]); + if (DEBUG){ + printf("%f IS ALL GOOD!\n",piValues[i][0]); + } } } - + printf("All checks have passed!\n"); return 0; } diff --git a/format.h b/format.h index d4d1c8b..519876c 100644 --- a/format.h +++ b/format.h @@ -21,13 +21,13 @@ const char caps[2][26] = { 'n','o','p','q','r','s','t','u','v','w','x','y','z'} }; -double mpow(double b, int e){ +double pow(double b, int e){ if (e==1){ return b; } else if (e%2==0){ - return mpow(b*b,e/2); + return pow(b*b,e/2); } else { - return b * mpow(b*b, e/2); + return b * pow(b*b, e/2); } } diff --git a/sine.c b/sine.c index a6f0053..f221b5b 100644 --- a/sine.c +++ b/sine.c @@ -12,14 +12,14 @@ #include "format.h" #include "testData.h" -const int DEBUG = TRUE; +const int DEBUG = FALSE; double sin(double input, int precision){ double ttl = input; int denom = 1; double num; for (int i=3;i<(2*precision)+2;i+=2) { - num = mpow(input,i); + num = pow(input,i); denom *= i*(i-1); ttl += ((i-1)/2 %2 == 1) ? -num/denom : num/denom; } @@ -42,9 +42,11 @@ int main(){ sin(piValues[i][0],precision)); return 1; } else { - printf("%f IS ALL GOOD!\n",piValues[i][0]); + if (DEBUG){ + printf("%f IS ALL GOOD!\n",piValues[i][0]); + } } } - + printf("All checks have passed!\n"); return 0; } diff --git a/tools/gen-trig-test.c b/tools/gen-trig-test.c index 54b39bf..66a82ec 100644 --- a/tools/gen-trig-test.c +++ b/tools/gen-trig-test.c @@ -11,24 +11,25 @@ #include #include -/* 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. +/* This code was used to generate 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 } * - * 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 {{{ - double lst[20] = { +const double lst[20] = { 0, // 0° M_PI/6, // 30° M_PI/4, // 45° -- cgit v1.2.3