You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
1.4 KiB
92 lines
1.4 KiB
4 years ago
|
package sin;
|
||
|
|
||
|
public class Sin {
|
||
|
|
||
|
public static double number;
|
||
|
public static double PI = 3.14159265358979323846;
|
||
|
|
||
|
|
||
|
public Sin() {
|
||
|
|
||
|
}
|
||
|
|
||
|
public static double sin(double num) {
|
||
|
|
||
|
double tmp = Math.toRadians(num);
|
||
|
number=Math.sin(tmp);
|
||
|
|
||
|
return number;
|
||
|
|
||
|
}
|
||
|
|
||
|
public static double cos(double num) {
|
||
|
|
||
|
double tmp = Math.toRadians(num);
|
||
|
|
||
|
return Math.cos(tmp);
|
||
|
|
||
|
}
|
||
|
|
||
|
public static double mysin(double x)
|
||
|
{
|
||
|
while(x>2*PI) x -= 2*PI;
|
||
|
while(x<0) x += 2*PI;
|
||
|
|
||
|
double index, result=0;
|
||
|
int i=1;
|
||
|
do{
|
||
|
index = mypow(x,i) / factorial(i);
|
||
|
result += mypow(-1,i/2) * index;
|
||
|
i += 2;
|
||
|
}
|
||
|
while(index>1e-6);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
public static double factorial(double i) {
|
||
|
|
||
|
if(i==0)
|
||
|
return 1;
|
||
|
else
|
||
|
return i*factorial(i-1);
|
||
|
}
|
||
|
|
||
|
public static double mypow(double x,int i) {
|
||
|
|
||
|
if(i>0)
|
||
|
return x*mypow(x, i-1);
|
||
|
else if(i<0)
|
||
|
return 1/x*mypow(x, i+1);
|
||
|
else
|
||
|
return 1;
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
public static double mycos(double x) {
|
||
|
while(x>2*PI) x -= 2*PI;
|
||
|
while(x<0) x += 2*PI;
|
||
|
|
||
|
double index, result=0;
|
||
|
int i=0;
|
||
|
|
||
|
do{
|
||
|
index = mypow(x,i) / factorial(i);
|
||
|
result += mypow(-1,i/2) * index;
|
||
|
i += 2;
|
||
|
}
|
||
|
while(index>1e-6);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
public static double toRadians(double angdeg) {
|
||
|
return angdeg / 180.0 * PI;
|
||
|
}
|
||
|
public static void main(String[] args) {
|
||
|
|
||
|
System.out.println(mysin(toRadians(30)));
|
||
|
System.out.println(mycos(30));
|
||
|
}
|
||
|
|
||
|
}
|