java algorithm calculation of precision

Pumpkin
/**
 * @(#)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. the formula

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?

C.B.

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Porting CRC Calculation algorithm from C to Java

From Dev

Delphi wrong double precision calculation

From Dev

Time complexity calculation of algorithm

From Dev

Algorithm for shape calculation (Ellipse)

From Dev

Algorithm Recursive Formula Calculation

From Dev

CRC checksum calculation algorithm

From Dev

Efficient algorithm for matrix calculation

From Dev

CPU statistics calculation algorithm

From Dev

Time complexity calculation for my algorithm

From Dev

Fermat Algorithm for prime factors calculation

From Dev

Java ScheduledExecutorService BAD Precision

From Dev

Loss of precision - Java

From Dev

Loss of precision - Java

From Dev

Loss of precision in java

From Dev

Java double losing precision

From Dev

Gamma in the Baum Welch algorithm and float precision

From Dev

Compiler flags for enhancing the precision of intermediate floating-point calculation

From Dev

Matlab digits precision ruins the calculation. How to avoid it?

From Dev

Why the algorithm fails when increase the number precision? How can we decrease the sensitivity of the algorithm to the number precision?

From Java

Understanding Time complexity calculation for Dijkstra Algorithm

From Dev

Need an algorithm written for delay time calculation

From Dev

trouble in calculation the right algorithm in javascript to draw an array

From Dev

C# Windows form algorithm calculation

From Dev

Java hashcode reverse calculation

From Java

Issue with Time Calculation in Java

From Dev

Java Primitives range calculation

From Dev

Java code for CRC calculation

From Dev

Operations / Calculation in Java

From Dev

Java Calculation Mistake

Related Related

HotTag

Archive