双重比较问题

发布于 01-03 19:16 字数 1193 浏览 4 评论 0原文

我正在用 Java 编写一个音高检测程序,并在 for 循环的每次迭代中比较两个双精度值,以确定样本数组中的最高幅度点。

其代码是:

double magnitude = 0.0;
double maxMag = 0.0;

int j = 0;
    for (int i = 0; i < 2640; i += 2) {
        magnitude = calcMag(sample[i], sample[i + 1])
                * (i < outputLength / 2 ? calcMag(sample[i * 2],
                        sample[i * 2 + 1]) : 1.0)
                * (i < outputLength / 3 ? calcMag(sample[i * 3],
                        sample[i * 3 + 1]) : 1.0)
                * (i < outputLength / 4 ? calcMag(sample[i * 4],
                        sample[i * 4 + 1]) : 1.0)
                * (i < outputLength / 5 ? calcMag(sample[i * 5],
                        sample[i * 5 + 1]) : 1.0);

        if (magnitude > maxMag) {
            maxMag = magnitude;
            peakIndex = j;
        }

        j++;
    }

使用调试器并查看变量,“magnitude”和“maxMag”的比较第一次工作正常(当 maxMag 为零时),但从那时起,它每次都会计算为 false,即使当震级实际上大于 maxMag 时。

示例比较值,其中 (magnitude > maxMag) 为 false:

magnitude = 2.828262485980501E20

maxMag = 1.3167436120685821E28

我一直在研究这个问题,并寻找比较双精度数的潜在问题(我已经尝试过 Double 到无济于事)。所以现在我想知道这里是否有人可以阐明我可能做错了什么?

谢谢

I'm writing a pitch detection program in Java and compare two doubles in each iteration of a for loop in order to determine the highest magnitude point in a sample array.

The code for this is:

double magnitude = 0.0;
double maxMag = 0.0;

int j = 0;
    for (int i = 0; i < 2640; i += 2) {
        magnitude = calcMag(sample[i], sample[i + 1])
                * (i < outputLength / 2 ? calcMag(sample[i * 2],
                        sample[i * 2 + 1]) : 1.0)
                * (i < outputLength / 3 ? calcMag(sample[i * 3],
                        sample[i * 3 + 1]) : 1.0)
                * (i < outputLength / 4 ? calcMag(sample[i * 4],
                        sample[i * 4 + 1]) : 1.0)
                * (i < outputLength / 5 ? calcMag(sample[i * 5],
                        sample[i * 5 + 1]) : 1.0);

        if (magnitude > maxMag) {
            maxMag = magnitude;
            peakIndex = j;
        }

        j++;
    }

Using a debugger and looking at the variables, the comparison of "magnitude" and "maxMag" works correctly the first time (when maxMag is zero), but from that point on it evaluates to false every time, even when magnitude is in fact greater than maxMag.

Example compared values where (magnitude > maxMag) is false:

magnitude = 2.828262485980501E20

maxMag = 1.3167436120685821E28

I've been looking at this for ages and looked around for potential problems with comparing doubles (I've tried the compare methods of Double to no avail). So now I'm wondering if anyone on here could shed some light on what I might be doing wrong?

Thanks

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

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

发布评论

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

评论(4

勿忘初心2025-01-10 19:16:58

数字以科学记数法书写,这里有十进制表示细分

2.828262485980501E20 = 2.828262485980501 * 10^20 = 282826248598050100000

1.3167436120685821E28 = 1.3167436120685821 * 10^28 = 13167436120685821000000000000

所以

2.828262485980501E20 < 1.3167436120685821E28

程序输出正确。

numbers are written in scientific notation here are there decimal representation breakdown

2.828262485980501E20 = 2.828262485980501 * 10^20 = 282826248598050100000

1.3167436120685821E28 = 1.3167436120685821 * 10^28 = 13167436120685821000000000000

So

2.828262485980501E20 < 1.3167436120685821E28

program output is correct.

老子叫无熙2025-01-10 19:16:58

maxMag = 1.3167436120685821E28

IS 大于

magnitude = 2.828262485980501E20

看看“E”后面的指数!

but

maxMag = 1.3167436120685821E28

IS larger than

magnitude = 2.828262485980501E20

Look at the exponents, after the "E"!

眼眸2025-01-10 19:16:58

magnitude 不大于maxMag

幅度 约为 10 的 20 次方。
maxMag 约为 10 的 28 次方。

magnitude is not greater than maxMag.

magnitude is in the order of 10 to the 20th power.
maxMag is in the order of 10 to the 28th power.

待天淡蓝洁白时2025-01-10 19:16:58

震级=2.28e20 不大于magMax=1.31e28。 E后面的整数是指数。 magMax 实际上是 1.31 * 10^28(1 和 28 个零)
http://en.wikipedia.org/wiki/Scientific_notation

magnitude=2.28e20 is NOT greater than magMax=1.31e28. The integer after E is the exponent. magMax is in fact 1.31 * 10^28 (1 and 28 zeros)
http://en.wikipedia.org/wiki/Scientific_notation

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