简体   繁体   English

如何在java中计算2 n次的近似自然对数?

[英]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.指令是: 1. 通过将系列中的 n 项相加来计算 2 的 ln。 a) You can approximate the natural logarithm of 2 with a series. a) 你可以用一个级数来近似 2 的自然对数。 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.你使用的术语越多,你就越接近 2 的自然对数。 b) ln 2 = 1 - 1/2 + 1/3 - 1/4 + 1/5 - ... 1/nc) 你需要使用 For 循环来解决这个问题 d) 对于这个程序,你将只传递 1 个参数,即 n 的值。 e) You will need to figure out how to change the sign for each consecutive term. e) 您需要弄清楚如何为每个连续的术语更改符号。 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.起初我认为我们需要找到不同数字的 ln,但后来知道它是输入 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相反,它打印出来的只是 1.0 1.0 1.0 1.0 1.0

Can anyone help point out what I am doing wrong?谁能帮忙指出我做错了什么?

public class aprox_log {公共类 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 .要找到具有n项的交替调和级数的总和,只需遍历从 1 到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.在 Java 中,运算符/以给定数字的精度计算结果。 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.特别是,如果我们将两个int ,结果将表示为int ,如果结果不能准确表示,则向 0 舍入。 That is, 1 / 2 is 0 .也就是说, 1 / 20

If you need floating point accuracy, you should use floating point numbers, for instance by declaring i to be of type double .如果您需要浮点精度,则应使用浮点数,例如将i声明为double类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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