Interessante Frage. Wie Tatarize hoffe ich, dass dies keine Hausaufgabenvermeidung ist.
Dieser Code zeigt die Anzahl der Begriffe an, die benötigt werden, um im Ergebnis für alle Winkel 0 - 90 Grad eine absolute Genauigkeit von +/- 0,000 000 1 zu erhalten.
Der höchste Leistungsbegriff macht einen Unterschied von x^k/k! zu dem Ergebnis. So
x^k/k! < 1/10^7
Hierbei ist x in Radiant so der größte Wert von x ~ 1,57 rads. Dies bedeutet, dass nur eine Serie bis zur Leistung 13 eine Endgültigkeitsdauer von weniger als 0,000 000 1 ergibt.
Leider ist mein PC fortgeschrittenen Alters (32-Bit) und jeder Versuch, 13 zu berechnen! verursacht Überlauf. So passen ich die Horner method ein wenig, vielleicht etwas Effizienz verlieren, aber faktorielles Überlauf zu vermeiden und einen Halt ermöglicht, wenn der Winkel klein ist, oder wenn eine ausreichende Genauigkeit, bevor der Strom gewonnen wird 13.
Sin x = x - x^2(x/3! - x^2(x/5! - x^2(x/7! - . . . - x^2(x/(m-1)!- x^2(x/m!)
wobei m die höchste Leistung, die für die gewünschte absolute Präzision.
Sin x = x + Sum { iTerm(i) * x^2/(i * (i-1)) }
wo
iTerm(0) = x and iTerm(n) = - x^2 * iTerm(n-1)/(i*(i-1)
PS - Warum können wir verwenden nicht Mathe Formatierung außerhalb der Mathematik Stapel von Exchange? Es würde das Schreiben von Gleichungen so viel klarer machen.
public class TrigByPoly
{
// No constructor used.
public static void main(String[] args)
{
double x0 = 0,
x1 = Math.PI/12,
x2 = Math.PI/6,
x3 = Math.PI/4,
x4 = Math.PI/3,
x5 = Math.PI/2;
double sinx0 = SinByPoly(x0),
sinx1 = SinByPoly(x1),
sinx2 = SinByPoly(x2),
sinx3 = SinByPoly(x3),
sinx4 = SinByPoly(x4),
sinx5 = SinByPoly(x5);
System.out.println("Sin(0) to 7 decimal places is : " + sinx0);
System.out.println("Sin(15) to 7 decimal places is : " + sinx1);
System.out.println("Sin(30) to 7 decimal places is : " + sinx2);
System.out.println("Sin(45) to 7 decimal places is : " + sinx3);
System.out.println("Sin(60) to 7 decimal places is : " + sinx4);
System.out.println("Sin(90) to 7 decimal places is : " + sinx5);
}
public static double SinByPoly(double x)
{
int i = 0; // Polynomial order indicator.
double x2 = x * x,
iTerm,
sinx = 0;
if (x < 0.0084) // Limiting angle for Sinx = x to 10^-7 precision.
sinx = x;
else
{
sinx = x;
iTerm = sinx;
i = 3;
do
{
iTerm = - x2 * iTerm/(i * (i - 1));
sinx += iTerm;
i = i + 2;
} while (i < 14 && (iTerm > 0.0000001 || -iTerm > 0.0000001));
}
return sinx;
}
}
OUTPUT
======
Sin(0) to an absolute precision of 1.0E-7 is : 0.0
Sin(15) to an absolute precision of 1.0E-7 is : 0.2588190618109834
Sin(30) to an absolute precision of 1.0E-7 is : 0.4999999918690232
Sin(45) to an absolute precision of 1.0E-7 is : 0.7071067829368671
Sin(60) to an absolute precision of 1.0E-7 is : 0.8660254450997811
Sin(75) to an absolute precision of 1.0E-7 is : 0.9659258210120795
Sin(90) to an absolute precision of 1.0E-7 is : 0.999999943741051
Sie haben eine Endlosschleife, weil Ihre Bedingung nicht das ist, was Sie wollen. Sie versuchen wahrscheinlich, Variablen für zwei verschiedene Zwecke wiederzuverwenden und zu scheitern. Erstellen Sie eine Delta-Variable, in der Sie die in dieser Interaktion vorgenommene Änderung speichern und den Detla-Wert in der Bedingung testen. – HopefullyHelpful