为什么 (int)(33.46639 * 1000000) 返回 33466389?

发布于 2024-08-24 06:48:23 字数 90 浏览 6 评论 0原文

(int)(33.46639 * 1000000) 返回 33466389

为什么会发生这种情况?

(int)(33.46639 * 1000000) returns 33466389

Why does this happen?

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

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

发布评论

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

评论(7

凉城已无爱 2024-08-31 06:48:23

浮点数学并不完美。 每个程序员都应该了解的内容

浮点运算被许多人认为是深奥的学科。这是相当令人惊讶的,因为浮点在计算机系统中无处不在。几乎每种语言都有浮点数据类型;从个人电脑到超级计算机的计算机都具有浮点加速器;大多数编译器有时会被要求编译浮点算法;事实上,每个操作系统都必须响应浮点异常,例如溢出。本文介绍了对计算机系统设计人员有直接影响的浮点方面的教程。它首先介绍浮点表示和舍入误差的背景,然后讨论 IEEE 浮点标准,最后以大量示例说明计算机构建者如何更好地支持浮点。

...

将无限多个实数压缩为有限数量的位数需要近似表示。虽然整数有无限多个,但在大多数程序中,整数计算的结果可以存储在 32 位中。相反,给定任何固定的位数,大多数实数计算将产生无法使用这么多位数精确表示的数量。因此,浮点计算的结果通常必须进行舍入,以适应其有限表示。这种舍入误差是浮点计算的特征。

Floating point math isn't perfect. What every programmer should know about it.

Floating-point arithmetic is considered an esoteric subject by many people. This is rather surprising because floating-point is ubiquitous in computer systems. Almost every language has a floating-point datatype; computers from PCs to supercomputers have floating-point accelerators; most compilers will be called upon to compile floating-point algorithms from time to time; and virtually every operating system must respond to floating-point exceptions such as overflow. This paper presents a tutorial on those aspects of floating-point that have a direct impact on designers of computer systems. It begins with background on floating-point representation and rounding error, continues with a discussion of the IEEE floating-point standard, and concludes with numerous examples of how computer builders can better support floating-point.

...

Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.

恰似旧人归 2024-08-31 06:48:23

双精度并不精确,因此内部 33.46639 实际上存储为 33.466389

编辑:正如理查德所说,它是浮点数据(以二进制形式存储在有限的位集中),所以它不完全是这样)...

Double precision is not exact, so internally 33.46639 is actually stored as 33.466389

Edit: As Richard said, it's floating point data, (stored in binary in a finite set of bits) so it's not exactly that) ....

雾里花 2024-08-31 06:48:23

那是 1994 年底的除夕夜。英特尔首席执行官安迪·格罗夫 (Andy Grove) 度过了美好的一年,奔腾处理器问世并大受欢迎。于是,他走进一家酒吧,点了一杯双份尊尼获加绿牌威士忌。

调酒师把酒端上来,说道:“先生,20 美元。”

格罗夫把一张二十美元的钞票放在柜台上,看了一会儿,说道:“零钱留着吧。”

http://en.wikipedia.org/wiki/Pentium_FDIV_bug

It was New Years' Eve at the end of 1994. Andy Grove, CEO of Intel, was coming off a great year, what with the Pentium processor coming out and being a big hit. So, he walked into a bar and ordered a double shot of Johnnie Walker Green Label.

The bartender served it up and said, "that will be $20, sir."

Grove put a twenty dollar bill on the counter, looked at it for a moment, and said, "keep the change."

http://en.wikipedia.org/wiki/Pentium_FDIV_bug

娇柔作态 2024-08-31 06:48:23

原因是 33.46639 将表示为略小于该数字的值。

乘以 1000000 将得到 33466389.99999999。

使用 (int) 进行类型转换将仅返回整数部分 (33466389)。

如果您想要“正确”的数字,请在类型转换之前尝试 round() 。

The reason is that 33.46639 will be represented as something slightly less than that number.

Multiplying by 1000000 will give you 33466389.99999999.

Type-casting using (int) will then just return the integer part (33466389).

If you want the "right" number, try round() before type casting.

绿萝 2024-08-31 06:48:23

如果你问为什么它没有变成 33466390,那是因为 double 没有无限精度,并且数字无法用二进制精确表达。

如果将 double 替换为 decimal ((int)(33.46639m * 1000000)),则等于 33466390< /code>,因为小数是以 10 为基数计算的。

If you're asking why it doesn't become 33466390, it's because doubles do not have infinite precision, and the number cannot be expressed exactly in binary.

If you replace the double with a decimal ((int)(33.46639m * 1000000)), it be equal to 33466390, because decimals are calculated in base 10.

泪冰清 2024-08-31 06:48:23

因为33.46639无法用有限个二进制数字精确表示。 33.46639 * 1000000的实际结果是33466389.9999999962747097015380859375。转换会将其截断为 33466389。

Because 33.46639 can't be expressed exactly in a finite number of binary digits. The actual result of 33.46639 * 1000000 is 33466389.9999999962747097015380859375. The cast truncates it to 33466389.

百思不得你姐 2024-08-31 06:48:23

您得到不同结果的原因是您使用“cast

(int)(33.46639 * 1000000) returns 33466389
^^^^^

将结果转换为“int”类型...在相乘时将整数类型向上或向下舍入然后转换为“int”......不要依赖浮点来足够准确......Skeet 在他的网站上发布了精彩的介绍 此处此处...

The reason you got a different result is the fact that you used a 'cast'

(int)(33.46639 * 1000000) returns 33466389
^^^^^

to cast the result to a type of 'int'... which either rounded up or down the integral type when multipled together and then converted to 'int'.... do not rely on floating point to be accurate enough....Skeet posted an excellent introduction on his site here and here...

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