a == b,指数表示法。八度怪异?

发布于 2024-12-29 09:56:08 字数 291 浏览 2 评论 0原文

如果我将其输入八度音阶:

a = 8*10^-5;
b = 8.0e-005;
a==b

我会得到答案:

ans = 1

但是如果我将其输入八度音阶:

a = 3*10^-5;
b = 3.0e-005;
a==b

我会得到这个答案

ans = 0

我是否遗漏了什么?这不是测试平等的正确方法吗?

If I type this into octave:

a = 8*10^-5;
b = 8.0e-005;
a==b

I get the answer:

ans = 1

But if I type this into octave:

a = 3*10^-5;
b = 3.0e-005;
a==b

I get this answer

ans = 0

Am I missing something? Is this not the right way to test for equality?

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

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

发布评论

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

评论(2

み零 2025-01-05 09:56:08

这是涉及浮点数的计算期间精度损失的结果,例如在 Java 中 3 * Math.pow(10, -5) 会给你“2.9999999999999997E-5”,它不再匹配b 在你的第二个例子中。这与浮点值在表达式求值过程中的表示和演变方式有关。对于更详细的观点,您可以在谷歌上搜索“精度损失”后缀以及您用来评估这些表达式的上下文/环境。希望这有帮助。

This is a result of loss of precision during computations involving floating point numbers, for example in Java 3 * Math.pow(10, -5) would give you '2.9999999999999997E-5' which no longer matches b in your second example. This is linked to how floating point values are represented and evolve during the evaluation of an expression. For more elaborate perspectives, you can google 'loss of precision' suffixing that with the context / environment you are using to evaluate these expressions. Hope this helps.

゛时过境迁 2025-01-05 09:56:08

这是计算机系统中浮点数表示方式的常见问题。

许多十进制“四舍五入”的数字在计算机使用的二进制表示形式中变成了重复分数,并且它们必须在某个点被截断,从而造成非常轻微的精度损失。如果两个浮点数的得出方式不同,则必须将它们与一点点斜率进行比较,例如 abs(ab) abs(ab) abs(ab) abs(ab) abs(ab) < EPSILON 其中 EPSILON 是一个适当的常数,如 1e-12。

This is a common problem with the way floating point numbers are represented in computer systems.

Many numbers which are 'round' in decimal become repeating fractions in the binary representation used by the computer, and they have to be truncated at some point, for a very slight loss of precision. If two floating point numbers are arrived at differently, you have to compare them with a little bit of slop, e.g. abs(a-b) < EPSILON where EPSILON is an appropriate constant like 1e-12.

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