简体   繁体   中英

How to compute approximate natural log of 2 n times in java?

The instructions were: 1. Compute the ln of 2, by adding up to n terms in the series. a) You can approximate the natural logarithm of 2 with a series. The more terms you use, the closer you get to the natural logarithm of 2. b) ln 2 = 1 - 1/2 + 1/3 - 1/4 + 1/5 - ... 1/nc) You will need to utilize a For Loop to solve this problem d) You will pass only 1 argument, the value of n, for this program. e) You will need to figure out how to change the sign for each consecutive term. This is not difficult, but it will require some thought.

Forgive me if this doesn't sound very good, I am not the best at English. My code so far is this: even though I'm aware that I've done most wrong. I know it is wrong but I can not find out where to start or how to change the sign. I was at first doing this thinking we needed to find the ln of different numbers but later learned it was with input n. EDIT: I believe I know what major part I was doing wrong. I fixed this to the best of my ability. I'm now a bit stuck because I'm playing out the logic in my head and I believe it should work. Instead all it prints out is 1.0 1.0 1.0 1.0 1.0

Can anyone help point out what I am doing wrong?

public class aprox_log {

static double findLog (int n)
{
    double ln = 1;
    for (int i = 1; i <= n; i++)
    {
        for (int k = 0; i <= n; k++) {
            if ((k%2) == 0)
                ln = ln - (1/i);
            ln = ln + (1/i);
        }
    }
return 1 - ln;
}

public static void main(String[] args) {
    //These lines print the results of test inputs.
    System.out.println(findLog(2)); //~0.69314718056
    System.out.println(findLog(5)); //~1.60943791243
    System.out.println(findLog(10)); //~2.30258509299
    System.out.println(findLog(9)); //~2.19722457734
    System.out.println(findLog(1)); //0
}

}

NEW CODE:

    public class aprox_log {

static double findLn (int n)
{
    double ln = 0;
    for (int i = 1; i <= n; i++)
    {
        if (i%2 == 0)
            ln = ln - (1/i);
        ln = ln + (1/i);
    }
return ln;

public static void main(String[] args) {
    //These lines print the results of test inputs.
    System.out.println(findLn(2)); //0.5
    System.out.println(findLn(5)); //0.783333333
    System.out.println(findLn(10)); //0.64563492063
    System.out.println(findLn(9)); //0.74563492063
    System.out.println(findLn(1)); //1
}

}

`

To find the sum of the alternating harmonic series with n terms, just loop over the integers from 1 to n . Then, add the reciprocal of the current number if it is odd and subtract the reciprocal if it is even.

static double findLog(int n){
    double res = 0;
    for(int i = 1; i <= n; i++) res += 1d / (i % 2 == 0 ? -i: i);
    return res;
}

In Java, the operator / calculates a result at the accuracy of the numbers it is given. In particular, if we divide two int , the result will be represented as int , rounding towards 0 if the result can not be represented exactly. That is, 1 / 2 is 0 .

If you need floating point accuracy, you should use floating point numbers, for instance by declaring i to be of type double .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM