I have left out something here or done something wrong??
Background:
We are supposed to implement a class ExpApproximator. It uses the power series and keep adding values until a summand (term) is less than a certain threshold. At each step, you need to compute the new term and add it to the total. Update these terms as follows: term = term * x / n;
public class ExpApproximator
{
public static double exp(int x)
{
final double epsilon = 0.01;
double term = 1;
double sum = 1;
double n = 1;
while(term > epsilon)
{
term *= x/n++;
sum += term;
}
return sum;
}
public static void main(String[] args)
{
System.out.println("e^x: " + exp(Integer.parseInt(args[0])));
System.out.println("e^x: " + Math.pow(Math.E,Integer.parseInt(args[0])));
}
}