Equation editor is not working, but here is a given series The sum from 1 to n o
ID: 3615864 • Letter: E
Question
Equation editor is not working, but here is a given seriesThe sum from 1 to n of: (i)^-i
Below is a java method that computes the sum of the series.
public double solve(int n) {
double answer = 0;
for(int i=1; i<=n; i++) {
answer +=Math.pow(Math.sqrt(i), -i);
}
return answer;
}
HOWEVER this method does not return a result that is completelyaccurate. I know that it is a problem with floating point numbers,but:
a) What is the exact source of the problem?
(Is it math.pow and math.sqrt??)
b) How can I correct the code to return accurate results
Thanks in advance
Explanation / Answer
Math.sqrt is the exact source of problem... please check in which variable you are getting the value ofthe answer is it a double type or not?Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.