summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2022-09-29 16:39:09 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2022-09-29 16:39:09 -0400
commite8364e95ee1b38e49af29a2aa1338b3d978411f0 (patch)
tree042a81f77fa1ef7e7cfca831307cc76486e5731d
parentc56472ceb5d46af1db39b1ac2cd822baffa3ec30 (diff)
Trigonometry somewhat works
-rw-r--r--.gitignore1
-rw-r--r--cosine.c48
-rw-r--r--format.h19
-rw-r--r--sine.c2
-rw-r--r--testData.h16
-rw-r--r--tools/gen-trig-test.c24
6 files changed, 82 insertions, 28 deletions
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):
+ * <benjamin@chausse.xyz> 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 <stdio.h>
+#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; i<COUNT_OF(piValues);i++){
+ if ( abs(piValues[i][2]-cos(piValues[i][0],precision))>threshold ){
+ 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; i<e; i++){
- result *= b;
+float mpow(float b, int e){
+ if (e==1){
+ return b;
+ } else if (e%2==0){
+ return mpow(b*b,e/2);
+ } else {
+ return b * mpow(b*b, e/2);
}
- return result;
}
float avg(float arr[]){
@@ -51,8 +54,8 @@ char * lower(char *str){
return str;
}
+// Count the total number of spaces to define the new str size
char * trim(char *str){
- // Count the total number of spaces to define the new str size
int spc =0;
for (int i=0; i<COUNT_OF(str); i++){
if (str[i] == ' '){
@@ -71,3 +74,7 @@ char * trim(char *str){
nstr[COUNT_OF(nstr)-1] = '\0';
return nstr;
}
+
+float abs(float i){
+ return (i>0) ? 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 <stdio.h>
#include <stdlib.h>
-/* 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<max; i++) {
printf(" {%f, %f, %f},\n",
lst[i],
sin(lst[i]),
cos(lst[i]));
};
- printf(" {%f, %f, %f}\n}",
+ printf(" {%f, %f, %f}\n}\n",
lst[max],
sin(lst[max]),
cos(lst[max]));
- return 0
+ return 0;
}