Das wäre dann wohl effizienter, werde ich probieren. Danke dir!
Ich hatte das damals so gemacht.
[CODE lang="java" title="StartTaylor"]public class StartTaylor {
private final static double PRECISION_LIMIT = 0.001;
public static void main(String[] args) {
System.out.println(" Taylor Math ");
System.out.println("--------------------------------");
for (int angle = 0; angle <= 90; angle += 10) {
double cosTaylor = cos(angle);
double cosMath = Math.cos(Math.toRadians(angle));
System.out.println(String.format("cos(%d)\t %f\t %f", angle, cosTaylor, cosMath));
}
}
/**
* Calculates cosine of an angle using Taylor series limited by precision.
*
* cos(x) = 1 - x^2/fac(2) + x^4/fac(4) - x^6/fac(6) ...
*
* @param angle
* Angle in degree
* @return double
*/
public static double cos(double angle) {
angle = Math.toRadians(angle % 360); // transform degree to radian
double cosine = 1;
int sign = -1;
double add = 0;
int i = 0;
do {
add = getAddCos(angle, i);
cosine += sign * add;
sign = -sign;
i++;
} while (add > PRECISION_LIMIT);
return cosine;
}
public static long fac(int n) {
long val = 1;
for (int i = 2; i <= n; i++)
val *= i;
return val;
}
private static double getAddCos(double x, int n) {
int pot = 2 * (n + 1);
return Math.pow(x, pot) / fac(pot);
}
}[/CODE]