Aproximarea Cordic

class cordicAproximation{

private int precision;

private double theta;

public cordicAproximation(double theta, int precision){

this.theta=theta;

this.precision=precision;

}

public double getK(){

double result=1;

for(int i=0;i<precision;i++){

result=result*Math.sqrt(1+Math.pow(2,(-2*i)));

}

return result;

}

public double getCos(){

double x=1/getK();

double y=0;

double z=theta;

for(int i=0;i<precision;i++){

if(z>=0){

double tmp=x;

x=x-(Math.pow(2,-i)*y);

y=y+(Math.pow(2,-i)*tmp);

z=z-Math.atan(Math.pow(2,-i));

}

else{

double tmp=x;

x=x+(Math.pow(2,-i)*y);

y=y-(Math.pow(2,-i)*tmp);

z=z+Math.atan(Math.pow(2,-i));

}

}

return x;

}

public double libraryCos(){

return Math.cos(theta);

}

public double getError(){

return Math.abs(getCos()-libraryCos());

}

public String toString(){

return "Cordic cos: "+getCos()+"\nLibrary cos: "+libraryCos() +"\nError: "+getError();

}

}

public class Main {

public static void main(String args[]) {

cordicAproximation cordic=new cordicAproximation(Math.PI/6,5);

System.out.println(cordic);

}

}