计算对数

发布于 2024-11-01 09:22:23 字数 485 浏览 7 评论 0原文

我正在尝试编写一个方法,该方法接受基数k和值n到小数点后两位,然后计算n的对数基数k,而不使用任何Java的Math.log 方法。这是我到目前为止所得到的:

public static double log(double k, double n) {
    double value = 0.0;

    for(double i = 1; i > .001; i /= 10) {
        while(!(Math.pow(k, value) >= n )) {
            value += i;
        }
    }

    return value;
}

当我尝试计算 5.0625 的以 4 为底的对数时,问题出现了,它返回 2.0,但应该返回 1.5。

我不知道为什么这不起作用。任何帮助表示赞赏。

不,这不是作业,这是我为了好玩而试图解决的问题集的一部分。

I'm trying to write a method that takes in a base k and a value n to 2 decimal places, then computes the log base k of n without using any of Java's Math.log methods. Here's what I have so far:

public static double log(double k, double n) {
    double value = 0.0;

    for(double i = 1; i > .001; i /= 10) {
        while(!(Math.pow(k, value) >= n )) {
            value += i;
        }
    }

    return value;
}

The problem comes up when I try computing log base 4 of 5.0625, which returns 2.0, but should return 1.5.

I have no idea why this isn't working. Any help is appreciated.

No this is not homework, it's part of a problem set that I'm trying to solve for fun.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吃兔兔 2024-11-08 09:22:23

您添加金额 i 的次数太频繁了。因此,您很快就会达到比实际值大的值,并且 while 循环将永远不会再次进入。

从该值中减去一次 i 就可以了:

for(double i = 1; i > .001; i /= 10) {
    while(!(Math.pow(k, value) > n )) {
        value += i;
    }
    value -= i;
}

You're adding the amount i once too ofter. Thus you'll quite soon reach a value larger than the actual value and the while loop will never be entered again.

Subtract i once from the value and you'll be fine:

for(double i = 1; i > .001; i /= 10) {
    while(!(Math.pow(k, value) > n )) {
        value += i;
    }
    value -= i;
}
浮生未歇 2024-11-08 09:22:23

单步执行纸上的代码:

Iteration: i=1 value = 0.0, calculated power = 1
Iteration: i=1 value = 1.0, calculated power = 4
Iteration: i=1 value = 2.0, calculated power = 16

现在,您的值是 2.0。但代码中的任何一点都无法向另一个方向纠正。您需要检查过冲和下冲情况。

Step through the code on paper:

Iteration: i=1 value = 0.0, calculated power = 1
Iteration: i=1 value = 1.0, calculated power = 4
Iteration: i=1 value = 2.0, calculated power = 16

Now at this point, your value is 2.0. But at no point in the code to you have a way to correct back in the other direction. You need to check for both overshoot and undershoot cases.

晨敛清荷 2024-11-08 09:22:23

这个循环

    while(!(Math.pow(k, value) >= n )) {
        value += i;
    }

走得太远了。它仅在超过正确值后停止。所以在计算个位的时候,1不够,所以就到2.0,而后面的所有测试都表明至少是够了,所以就到此结束。

This loop

    while(!(Math.pow(k, value) >= n )) {
        value += i;
    }

goes too far. It only stops after the correct value has been surpassed. So when calculating the ones place, 1 isn't enough, so it goes to 2.0, and all subsequent tests show that it is at least enough, so that's where it ends.

谈场末日恋爱 2024-11-08 09:22:23

手工计算日志,多有趣啊!我建议将其写在纸上,然后使用监视变量逐步执行代码或在每一步输出每个变量。然后检查此方法,看看它是否与您正在做的事情相符: 链接

Calculating logs by hand, what fun! I suggest doing it out on paper, then stepping through your code with watch variables or outputting each variable at each step. Then check this method out and see if it lines up with what you're doing: Link

岁月静好 2024-11-08 09:22:23

您可以随时查看:

https://stackoverflow.com/a/2073928/251767

它提供了一种算法,可以计算任意基数的任意数字的对数。这是对有关使用 BigDecimal 类型计算日志的问题的回答,但它可以很容易地适应任何浮点类型。

由于它使用平方和除以二,而不是使用多次调用 Math.pow(),因此它应该很快收敛并使用更少的 CPU 资源。

You could always look at:

https://stackoverflow.com/a/2073928/251767

It provides an algorithm which will compute a log of any number in any base. It's a response to a question about calculating logs with BigDecimal types, but it could be adapted, pretty easily, to any floating-point type.

Since it uses squaring and dividing by two, instead of using multiple calls to Math.pow(), it should converge pretty quickly and use less CPU resources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文