TEXTBOOK SOLUTIONS FOR

A Comprehensive Introduction to Object-Oriented Programming with Java 0th Edition

182 step-by-step solutions

Solved by publishers, professors & experts

iOS, Android, & web

81% of students said using Chegg better prepared them for exams

2013 Chegg Homework Help Survey

PROBLEM

Chapter:Problem:

show all steps

You can compute sin x and cos x by using the following power series:

Write a program that evaluates sin x and cos x by using the power series. Use the double data type, and increase the number of terms in the series until the overflow occurs. You can check if the overflow occurs by comparing the value against Double. POSITIVE_INFINITY. Compare the results you obtain to the values returned by the sin and cos methods of the Math class.

SAMPLE SOLUTION

JavaScript Not Detected

JavaScript is required to view textbook solutions.

Step 1 of 1

class Exercise6_06 {

private static final String LEADING_SPACES =
"
";

private static final int
DEFAULT_MAX_TERM = 10;

public static void main (String[] args) {

// Change the value
of x to test a different number

final double x =
0.8;

double
cosX, sinX;

double
numerator, denominator;

int sinMaxTerm, cosMaxTerm, sign;

System.out.println("Sin x and Cos x using Power Series");

sinMaxTerm =
DEFAULT_MAX_TERM;

/*SIN*/ //Compute sin x up to the limit of the 'double'
data type

do
{

numerator = x;

denominator = 1;

sinX =
0;

sign =
1;

for (int i = 1; i <= sinMaxTerm; i++ ) {

sinX += sign * numerator / denominator;

//get sin x up to the i'th
term

//get the numerator and denominator

//of the (i+1)st term

//sign switches between +1 and -1

numerator *= x *
x;

denominator *= (2*i) * (2*i+1);

sign *=
-1;

}

sinMaxTerm++;

} while (
denominator != Double.POSITIVE_INFINITY ); //See Note below

/*COS*/ //Compute cox x up to the limit of the 'double' data
type

cosMaxTerm =
DEFAULT_MAX_TERM;

do {

numerator = 1;

denominator = 1;

cosX =
0;

sign =
1;

for (int i = 1; i <= cosMaxTerm; i++ ) {

//get cos x up to the i'th term

//get the numerator and denominator

//of the (i+1)st term

//sign switches between +1 and -1

cosX += sign * numerator /
denominator;

numerator *= x * x;

denominator *= (2*i-1) * (2*i);

sign *=
-1;

}

cosMaxTerm++;

} while ( denominator
!= Double.POSITIVE_INFINITY ); //See Note below

//Display the
result

System.out.println(
"Input: x --
" + x );

System.out.println();

System.out.println("Output: " );

System.out.println();

System.out.println("
Max term -- " + (sinMaxTerm-1)

+ "th term");

System.out.println(LEADING_SPACES + "Power Series Sine:
" + sinX );

System.out.println(LEADING_SPACES + "Math Class
Sine: "

+ Math.sin(x) );

System.out.println();

System.out.println("
Max term -- " + (cosMaxTerm-1)

+ "th term");

System.out.println(LEADING_SPACES + "Power Series Cosine: " + cosX
);

System.out.println(LEADING_SPACES + "Math Class Cosine:
"

+ Math.cos(x) );

}

// NOTE:

// When the value of x is too big (or too
small in case of negative x),

// the power series routine is not accurate.
When the value for x is

// small <= 30, then 'denominator' becomes
overflow before the 'numerator'