diff options
author | Benjamin Chausse <benjamin@chausse.xyz> | 2022-09-27 16:41:07 -0400 |
---|---|---|
committer | Benjamin Chausse <benjamin@chausse.xyz> | 2022-09-27 16:41:07 -0400 |
commit | c56472ceb5d46af1db39b1ac2cd822baffa3ec30 (patch) | |
tree | b59e122f0647c5d94384533ae7f57bac48c26f80 | |
parent | 65e31597fa6bef7ba8263eeb97094d3a829a3ad6 (diff) |
Semi functional Sine function
-rw-r--r-- | sine.c | 50 |
1 files changed, 50 insertions, 0 deletions
@@ -0,0 +1,50 @@ +/* sine.h + * ---------------------------------------------------------------------------- + * "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" + +const int DEBUG = TRUE; + +float sin(float input, int precision){ + float ttl = input; + int denom = 1; + float num; + 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; + } + 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][1]-sin(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][1], + sin(piValues[i][0],precision)); + return 1; + } else { + printf("%f IS ALL GOOD!\n",piValues[i][0]); + } + } + + return 0; +} |