Advertisement
Demetra4

prob_AC_COGRIG_cos

Dec 9th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double calculateK(unsigned int n){
  5.     double k = 1.0;
  6.     int i = 0;
  7.     unsigned long power = 1;
  8.     for(; i < n; i++){
  9.         k*=sqrt(1.0 + 1.0/power);
  10.     }
  11.     return k;
  12. }
  13. double codrig(double theta, unsigned int n){
  14.     double x = 1.0/calculateK(n);
  15.     double x_old = x;
  16.     double y = 0;
  17.     double z = theta;
  18.     double power = 1.0;
  19.  
  20.     for(int i = 0; i < n; i++, power/= 2.0){
  21.         if(z>=0){
  22.             x = x - (y * power) ;
  23.             y = y + (x_old * power) ;
  24.             z = z - atan(power);
  25.         }
  26.         else {
  27.             x = x + (y * power) ;
  28.             y = y - (x_old * power) ;
  29.             z = z + atan(power);
  30.         }
  31.         x_old = x;        
  32.         }
  33.     return x;
  34. }
  35. void printCODRIG(double theta, char* thetaSTR, unsigned int n){
  36.     double cos_cordig = codrig(theta, n);
  37.     double cos_library = cos(theta);
  38.     double error = cos_cordig - cos_library;
  39.     unsigned int noEqualBits = 0;
  40.     if(thetaSTR == NULL) thetaSTR = "theta";
  41.         printf(" n = %2d | cos_cordig(%s) = %+.10f | cos_library(%s) = %+.10f | error = %+.15f | equal_bits = %2d\n",
  42.                             n, thetaSTR, cos_cordig, thetaSTR, cos_library, error, noEqualBits);
  43. }
  44. int main()
  45. {
  46.     double theta = 0;
  47.     char* thetaSTR = NULL;
  48.     unsigned int n = 0;
  49.     theta = M_PI/6;
  50.     n = 31;
  51.     thetaSTR = "pi/6";
  52.     printCODRIG(theta, thetaSTR, n);
  53.  
  54.     return 0;
  55. }
  56. //sau aslta varianta
  57. #include <stdio.h>
  58. #include <math.h>
  59.  
  60. /*double calculateK(unsigned int n){
  61.     double k = 1.0;
  62.     int i = 0;
  63.     unsigned long power = 1;
  64.     for(; i < n; i++){
  65.         k*=sqrt(1.0 + 1.0/power);
  66.     }
  67.     return k;
  68. }
  69. double codrig(double theta, unsigned int n){
  70.     double x = 1.0/calculateK(n);
  71.     double x_old = x;
  72.     double y = 0;
  73.     double z = theta;
  74.     double power = 1.0;
  75.  
  76.     for(int i = 0; i < n; i++, power/= 2.0){
  77.         if(z>=0){
  78.             x = x - (y * power) ;
  79.             y = y + (x_old * power) ;
  80.             z = z - atan(power);
  81.         }
  82.         else {
  83.             x = x + (y * power) ;
  84.             y = y - (x_old * power) ;
  85.             z = z + atan(power);
  86.         }
  87.         x_old = x;        
  88.         }
  89.     return x;
  90. }*/
  91. double codrig(float theta, unsigned int n){
  92.     double x,y,z,alpha;
  93.     double k = 1;
  94.     for(int i = 0; i < n; i++){
  95.         k*=sqrt(1+pow(2, -2*i));
  96.     }
  97.     printf("k = %f.8\n", k);
  98.     x = 1/k;
  99.     y = 0;
  100.     z = theta;
  101.     for (int i = 0; i< n; i ++){
  102.         alpha = atan(pow(2,-i));
  103.         if(z>0){
  104.             float x_temp = x;
  105.             float y_temp = y;
  106.             x = x - pow(2, -i) * y_temp;
  107.             y = y + pow(2, -i) * x_temp;
  108.             z = z - alpha;
  109.         }
  110.         else {
  111.             float x_temp = x;
  112.             float y_temp = y;
  113.             x = x + pow(2, -i) * y_temp;
  114.             y = y - pow(2, -i) * x_temp;
  115.             z = z + alpha;
  116.         }
  117.         printf("z = %.8f  y = %.8f  x = %.8f  alpha = %.8f\n", z, y, x, alpha);
  118.     }
  119.     return x;
  120. }
  121. void printCODRIG(double theta, char* thetaSTR, unsigned int n){
  122.     double cos_cordig = codrig(theta, n);
  123.     double cos_library = cos(theta);
  124.     double error = cos_cordig - cos_library;
  125.     unsigned int noEqualBits = 0;
  126.     if(thetaSTR == NULL) thetaSTR = "theta";
  127.         printf(" n = %2d | cos_cordig(%s) = %+.10f | cos_library(%s) = %+.10f | error = %+.15f | equal_bits = %2d\n",
  128.                             n, thetaSTR, cos_cordig, thetaSTR, cos_library, error, noEqualBits);
  129. }
  130. int main()
  131. {
  132.     double theta = 0;
  133.     char* thetaSTR = NULL;
  134.     unsigned int n = 0;
  135.     theta = M_PI/6;
  136.     n = 31;
  137.     thetaSTR = "pi/6";
  138.     printCODRIG(theta, thetaSTR, n);
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement