I have left out something here or done something wrong??
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])));

11 Years
Discussion Span
Last Post by jwenting

what do you think it should do, and what does it do?

I see at least one error in your code, stemming from the way Java does division and multiplication and what you probably think it does.
You are missing a required cast somewhere, that's all I'll say for now.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.