双重比较问题
我正在用 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 技术交流群。

发布评论
评论(4)
震级=2.28e20 不大于magMax=1.31e28。 E后面的整数是指数。 magMax 实际上是 1.31 * 10^28(1 和 28 个零)
http://en.wikipedia.org/wiki/Scientific_notation
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
数字以科学记数法书写,这里有十进制表示细分
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.