uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

sine_cos_taylor.c (1748B)


      1 #include <stdio.h>
      2 #include <math.h>
      3 #include "sine-cos-taylor.h"
      4 
      5 #define PI 3.141592654
      6 #define ACCURACY 0.000001
      7 #define COMPARISON 0.000009
      8 
      9 double input()
     10 {
     11     double xDegrees;
     12     
     13     printf("x (in degrees): ");
     14     scanf("%lf", &xDegrees);
     15 
     16     return xDegrees;
     17 }
     18 
     19 double rads_conv(double xDegrees)
     20 {
     21     return xDegrees * (PI/180.0);
     22 }
     23 
     24 double sine_calc(double xRads)
     25 {
     26     double currentFrac, previousFrac, sineValue = 0;
     27     int exponent = 1, sign = 1;
     28     
     29     currentFrac = power(xRads, exponent) / factorial(exponent);
     30     
     31     do
     32     {        
     33         sineValue += sign * currentFrac;
     34     
     35         exponent += 2;
     36 
     37         previousFrac = currentFrac;
     38         currentFrac = power(xRads, exponent) / factorial(exponent);
     39         
     40         sign *= -1;
     41     } while (fabs(previousFrac - currentFrac) > ACCURACY);
     42 
     43     return sineValue;
     44 }
     45 
     46 double cosine_calc(double xRads)
     47 {
     48     double currentFrac = 1, previousFrac, cosineValue = 0;
     49     int exponent = 0, sign = 1;
     50     
     51     do
     52     {        
     53         cosineValue += sign * currentFrac;
     54     
     55         exponent += 2;
     56 
     57         previousFrac = currentFrac;
     58         currentFrac = power(xRads, exponent) / factorial(exponent);
     59         
     60         sign *= -1;
     61     } while (fabs(previousFrac - currentFrac) > ACCURACY);
     62 
     63     return cosineValue;
     64 }
     65 
     66 double power(double xRads, int exponent)
     67 {
     68     int i;
     69     double value;
     70 
     71     for (i = 0, value = 1; i < exponent; i++)
     72         value *= xRads;
     73 
     74     return value;
     75 }
     76 
     77 double factorial(int exponent)
     78 {
     79     int i;
     80     double fac;
     81 
     82     for (i = 1, fac = 1; i <= exponent; i++)
     83         fac *= i;
     84 
     85     return fac;
     86 }
     87 
     88 int compare(double calcValue, double funcValue)
     89 {
     90     if (fabs(calcValue - funcValue) <= COMPARISON)
     91         return 1;
     92     else
     93         return 0;
     94 }