summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2022-09-29 16:42:58 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2022-09-29 16:42:58 -0400
commiteae5d5b8d55f5da69eea990e68497dbc0e74c9f3 (patch)
tree4dc2346c652db653320e27054a69c3aa8aa790af
parente8364e95ee1b38e49af29a2aa1338b3d978411f0 (diff)
Fix sine and cosine by increasing presicion
-rw-r--r--cosine.c10
-rw-r--r--format.h8
-rw-r--r--sine.c10
-rw-r--r--testData.h2
4 files changed, 15 insertions, 15 deletions
diff --git a/cosine.c b/cosine.c
index 58bd189..934ca3b 100644
--- a/cosine.c
+++ b/cosine.c
@@ -11,10 +11,10 @@
#include "format.h"
#include "testData.h"
-float cos(float input, int precision){
- float ttl = 1;
+double cos(double input, int precision){
+ double ttl = 1;
int denom = 1;
- float num;
+ double num;
for (int i=2;i<=(2*precision);i+=2){
num = mpow(input,i);
denom *= i*(i-1);
@@ -25,8 +25,8 @@ float cos(float input, int precision){
}
int main(){
- const int precision = 16;
- const float threshold = 1e-4;
+ const int precision = 20;
+ const double threshold = 1e-4;
for (int i=0; i<COUNT_OF(piValues);i++){
if ( abs(piValues[i][2]-cos(piValues[i][0],precision))>threshold ){
diff --git a/format.h b/format.h
index 3d51c3c..d4d1c8b 100644
--- a/format.h
+++ b/format.h
@@ -21,7 +21,7 @@ const char caps[2][26] = {
'n','o','p','q','r','s','t','u','v','w','x','y','z'}
};
-float mpow(float b, int e){
+double mpow(double b, int e){
if (e==1){
return b;
} else if (e%2==0){
@@ -31,9 +31,9 @@ float mpow(float b, int e){
}
}
-float avg(float arr[]){
+double avg(double arr[]){
int len = COUNT_OF(arr);
- float sum = 0;
+ double sum = 0;
for (int i=0;i<len;i++){
sum += arr[i];
}
@@ -75,6 +75,6 @@ char * trim(char *str){
return nstr;
}
-float abs(float i){
+double abs(double i){
return (i>0) ? i : -i;
}
diff --git a/sine.c b/sine.c
index 204ab41..c483317 100644
--- a/sine.c
+++ b/sine.c
@@ -14,10 +14,10 @@
const int DEBUG = TRUE;
-float sin(float input, int precision){
- float ttl = input;
+double sin(double input, int precision){
+ double ttl = input;
int denom = 1;
- float num;
+ double num;
for (int i=3;i<(2*precision)+2;i+=2) {
num = mpow(input,i);
denom *= i*(i-1);
@@ -27,8 +27,8 @@ float sin(float input, int precision){
}
int main(){
- const int precision = 16;
- const float threshold = 1e-4;
+ const int precision = 20;
+ const double threshold = 1e-4;
for (int i=0; i<COUNT_OF(piValues);i++){
if ( abs(piValues[i][1]-sin(piValues[i][0],precision))>threshold ){
diff --git a/testData.h b/testData.h
index 02b23c5..ba7667f 100644
--- a/testData.h
+++ b/testData.h
@@ -40,7 +40,7 @@ const char *wrd[] = {
* - Negative values
* - Values surpassing 2π
*/
-const float piValues[20][3] = {
+const double piValues[20][3] = {
// test-value sin cos
{ 0.000000, 0.000000, 1.000000}, // 0°
{ 0.523599, 0.500000, 0.866025}, // 30°