Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- double calculateK(unsigned int n){
- double k = 1.0;
- int i = 0;
- unsigned long power = 1;
- for(; i < n; i++){
- k*=sqrt(1.0 + 1.0/power);
- }
- return k;
- }
- double codrig(double theta, unsigned int n){
- double x = 1.0/calculateK(n);
- double x_old = x;
- double y = 0;
- double z = theta;
- double power = 1.0;
- for(int i = 0; i < n; i++, power/= 2.0){
- if(z>=0){
- x = x - (y * power) ;
- y = y + (x_old * power) ;
- z = z - atan(power);
- }
- else {
- x = x + (y * power) ;
- y = y - (x_old * power) ;
- z = z + atan(power);
- }
- x_old = x;
- }
- return x;
- }
- void printCODRIG(double theta, char* thetaSTR, unsigned int n){
- double cos_cordig = codrig(theta, n);
- double cos_library = cos(theta);
- double error = cos_cordig - cos_library;
- unsigned int noEqualBits = 0;
- if(thetaSTR == NULL) thetaSTR = "theta";
- printf(" n = %2d | cos_cordig(%s) = %+.10f | cos_library(%s) = %+.10f | error = %+.15f | equal_bits = %2d\n",
- n, thetaSTR, cos_cordig, thetaSTR, cos_library, error, noEqualBits);
- }
- int main()
- {
- double theta = 0;
- char* thetaSTR = NULL;
- unsigned int n = 0;
- theta = M_PI/6;
- n = 31;
- thetaSTR = "pi/6";
- printCODRIG(theta, thetaSTR, n);
- return 0;
- }
- //sau aslta varianta
- #include <stdio.h>
- #include <math.h>
- /*double calculateK(unsigned int n){
- double k = 1.0;
- int i = 0;
- unsigned long power = 1;
- for(; i < n; i++){
- k*=sqrt(1.0 + 1.0/power);
- }
- return k;
- }
- double codrig(double theta, unsigned int n){
- double x = 1.0/calculateK(n);
- double x_old = x;
- double y = 0;
- double z = theta;
- double power = 1.0;
- for(int i = 0; i < n; i++, power/= 2.0){
- if(z>=0){
- x = x - (y * power) ;
- y = y + (x_old * power) ;
- z = z - atan(power);
- }
- else {
- x = x + (y * power) ;
- y = y - (x_old * power) ;
- z = z + atan(power);
- }
- x_old = x;
- }
- return x;
- }*/
- double codrig(float theta, unsigned int n){
- double x,y,z,alpha;
- double k = 1;
- for(int i = 0; i < n; i++){
- k*=sqrt(1+pow(2, -2*i));
- }
- printf("k = %f.8\n", k);
- x = 1/k;
- y = 0;
- z = theta;
- for (int i = 0; i< n; i ++){
- alpha = atan(pow(2,-i));
- if(z>0){
- float x_temp = x;
- float y_temp = y;
- x = x - pow(2, -i) * y_temp;
- y = y + pow(2, -i) * x_temp;
- z = z - alpha;
- }
- else {
- float x_temp = x;
- float y_temp = y;
- x = x + pow(2, -i) * y_temp;
- y = y - pow(2, -i) * x_temp;
- z = z + alpha;
- }
- printf("z = %.8f y = %.8f x = %.8f alpha = %.8f\n", z, y, x, alpha);
- }
- return x;
- }
- void printCODRIG(double theta, char* thetaSTR, unsigned int n){
- double cos_cordig = codrig(theta, n);
- double cos_library = cos(theta);
- double error = cos_cordig - cos_library;
- unsigned int noEqualBits = 0;
- if(thetaSTR == NULL) thetaSTR = "theta";
- printf(" n = %2d | cos_cordig(%s) = %+.10f | cos_library(%s) = %+.10f | error = %+.15f | equal_bits = %2d\n",
- n, thetaSTR, cos_cordig, thetaSTR, cos_library, error, noEqualBits);
- }
- int main()
- {
- double theta = 0;
- char* thetaSTR = NULL;
- unsigned int n = 0;
- theta = M_PI/6;
- n = 31;
- thetaSTR = "pi/6";
- printCODRIG(theta, thetaSTR, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement