I am implementing a method berechneCosinus(float x, int ordnung) in Java that should approximate the cosine of x using the Taylor series up to the specified order.
My questions:
Can I optimize the computation inside the method, without changing the signatures, in order to exactly match the given test results?
Here is what I have so far:
public class Taylor {
public float berechneFak(float x) {
if (x <= 1) return 1.0f;
int n = Math.round(x);
float fak = 1.0f;
for (int i = 2; i <= n; i++) {
fak *= i;
}
return fak;
}
public float berechneCosinus(float x, int ordnung) {
float pi = 3.14159265f;
float zweiPi = 2.0f * pi;
x = x % zweiPi;
if (x > pi) x -= zweiPi;
else if (x < -pi) x += zweiPi;
if (ordnung == 0) return 0.0f;
int anzahlTerme = (ordnung + 1) / 2;
float summe = 0.0f;
float term = 1.0f;
for (int i = 0; i < anzahlTerme; i++) {
int n = i * 2;
if (i > 0) {
term *= -x * x / ((n - 1) * n);
}
summe += term;
}
return summe;
}
}
This is the assignment:
public class Taylor {
public float berechneFak(float x) {
// insert code here!
// drandenken: Vorlesung!
return 0.0f;
}
public float berechneCosinus(float x, int ordnung) {
float acc = 0.0f;
// Ihr Code hierher!
return acc;
}
}
Test case:
Testfälle
Expected / received result:
Erwartet / Erhaltenes Ergebnis
I implemented the cosine calculation method using the Taylor series and normalized x to the interval [−π,π]. However, the results slightly differ from the expected values in some test cases. Since I’m not allowed to change the method signatures and can only work with floats, I’m looking for tips on how to improve the accuracy.
Here is a minimal reproducible example:
public class Taylor {
public float berechneCosinus(float x, int ordnung) {
if (ordnung == 0) return 0.0f;
float sum = 0.0f;
float term = 1.0f;
for (int i = 0; i < (ordnung + 1) / 2; i++) {
int n = 2 * i;
if (i > 0)
term *= -x * x / ((n - 1) * n);
sum += term;
}
return sum;
}
public static void main(String[] args) {
Taylor t = new Taylor();
float x = 3.14159265f; // approx. PI
System.out.printf("%.6f\n", t.berechneCosinus(x, 13));
System.out.printf("%.6f\n", t.berechneCosinus(x, 14));
}
}
The actual output is:
-0.999900
-0.999900
The expected output should be:
-0.999899
-0.999899
I'm aware of the limitations with floating point; the problem is that the tests check for exactly -0.999899. I'm wondering if it's possible to restructure the calculation to get the expected result. Nonetheless I'm going to ask my prof. Could be also possible that there is an error/mistake in the testing code.
Solution
Thanks for all the comments and suggestions; I appreciate all the efforts you guys made so far. I asked my prof today and he gave me a hint so I got the answer now.
I can't say why we should do it like the way he wants it but here is the final code:
public class Taylor {
public float berechneFak(float x) {
float produkt = 1.0f;
for (int i = 2; i <= x; i++) {
produkt *= i;
}
return produkt;
}
public float berechneCosinus(float x, int ordnung) {
float acc = 0.0f;
float a = 1.0f;
for (int i = 0; i < ordnung; i += 2) {
acc += (a / berechneFak(i)) * Math.pow(x, i);
a *= (-1);
}
return acc;
}
public static void main(String[] args) {
Taylor t = new Taylor();
float x = 3.14159265f;
for (int ordnung = 0; ordnung < 15; ordnung++) {
System.out.printf("%.6f%n", t.berechneCosinus(x, ordnung));
}
}
}