/**
* @(#)precision1.java
*
* precision1 application
*
* @author
* @version 1.00 2013/11/15
*/
public class precision1 {
public static void main(String[] args) {
// TODO, add your application code
int x = 4;
double increment = 1 ;
double coefficient = 1;
double currentValue = 1;
double precision = 0.01;
for( int i = 1;increment>precision ;i++)
{
currentValue= Math.pow(x,i)*coefficient+currentValue;
increment= Math.abs (Math.pow(x,i)*coefficient);
coefficient = coefficient * (1/(i+1));
System.out.println("Current result: " + currentValue);
}
}
}
The question is: If number 2 is given, it should ask an integer x from user, and a precision. It should calculate approximate value of e to the power x by using the formula given below, by using user inputted x. Your program stops calculation whenever the increment is less than given precision value. It should also be written as a method, not directly in your main method.
and the output should be:
current result: 5.0
current result: 13.0
current result: 23.666666666666664
current result: 34.33333333333333
current result: 42.86666666666666
current result: 48.55555555555555
current result: 51.8063492063492
current result: 53.43174603174603
current result: 54.15414462081129
current result: 54.44310405643739
current result: 54.54818021484688
current result: 54.583205600983376
current result: 54.59398264287153
current result: 54.59706179769672
Result is 54.59706179769672
and output of my code is:
Current result: 5.0
Current result: 5.0
Regardless of asking input parts why my code does not work?
Quickly running it through a debugger - (1/(i+1))
is integer division, causing coefficient = coefficient * (1/(i+1));
to evaluate to 0 and thus increment= Math.abs (Math.pow(x,i)*coefficient);
to evaluate to 0
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments