0.699 x 100 = 69.89999999999999?

发布于 2024-09-03 08:58:24 字数 552 浏览 6 评论 0原文

可能的重复:
为什么 99.99 / 100 = 0.9998999999999999
处理浮点数的精度问题

我已经在 php 和 javascript 中看到这个问题。我有这个数字:float 0.699

如果我这样做: 0.699 x 100 = 69.89999999999999

为什么?

编辑

round(0.699 x 10, 2): float 69.90000000000001

Possible Duplicates:
Why does 99.99 / 100 = 0.9998999999999999
Dealing with accuracy problems in floating-point numbers

I've seen this issue in php and javascript. I have this number: float 0.699

if I do this:
0.699 x 100 = 69.89999999999999

why?

edit

round(0.699 x 10, 2): float 69.90000000000001

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

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

发布评论

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

评论(6

稀香 2024-09-10 08:58:24

浮点运算并不精确。

请参阅维基百科上的浮点以更深入地讨论该问题。

Floating point arithmetic is not exact.

See Floating point on Wikipedia for a deeper discussion of the problem.

岁月染过的梦 2024-09-10 08:58:24

这就是过去对我的帮助。这与事物如何以二进制表示有很大关系。基本上,长话短说,二进制中没有所有大数实数的确切数字。

下面的链接将为您更详细地描述这一点。

每个计算机科学家应该了解的浮点运算知识

This is what has helped me in the past. It has a lot to do with how things are represented in binary. Basically long story short in binary there isn't an exact number for all real numbers of large numbers.

The link below will describe that in more detail for you.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

随遇而安 2024-09-10 08:58:24

这在任何语言中都会发生。与计算机上的其他所有内容一样,浮点数也以二进制形式存储。数字 0.699 虽然可以精确地用十进制表示,但可能是二进制的重复小数,因此无法以精确的精度存储。

查看维基百科条目了解浮点数的存储方式以及发生这种情况的原因。

This will happen in any language. Floats, like everything else on a computer, are stored as binary. The number 0.699, while representable exactly in decimal, is probably a repeating decimal in binary, so it can't be stored to exact precision.

Check out the wikipedia entry for how floats are stored, and why this happens.

傻比既视感 2024-09-10 08:58:24

JavaScript 数字是浮点

查看完整的 JavaScript 编号参考。摘抄:

Javascript 中的所有数字都是 64 位(8
字节)浮点数
产生 5e-324 的有效范围
(负)至 1.7976931348623157e+308
(正)在写这篇文章时
被写入(这最终可能
以后改成128位为64位
位处理器变得司空见惯
ECMA 标准不断发展)。

整数被认为是可靠的
(没有句点或指数的数字
表示法)到 15 位数字 (9e15) 1
考虑浮点数
只尽可能可靠,不
更多的!这是一个特别重要的
货币的概念
操纵为 0.06 + 0.01 解析
改为 0.06999999999999999 而不是
0.07。

Javascript numbers are floating point.

Take a look at The complete javascript number reference. Excerpt:

All numbers in Javascript are 64bit (8
bytes) floating point numbers which
yields an effective range of 5e-324
(negative) to 1.7976931348623157e+308
(positive) at the time this article
was written (this may eventually
change to 128 bits in the future as 64
bit processors become commonplace and
the ECMA standards evolve).

Integers are considered reliable
(numbers without a period or exponent
notation) to 15 digits (9e15) 1.
Floating point numbers are considered
only as reliable as possible and no
more! This is an especially important
concept to understand for currency
manipulation as 0.06 + 0.01 resolves
to 0.06999999999999999 instead of
0.07.

走野 2024-09-10 08:58:24

查看浮点,特别是有关 IEEE 754 和可表示数字的部分。

Take a look at Floating Point, specifically the section on IEEE 754 and representable numbers.

夏花。依旧 2024-09-10 08:58:24

这种行为可以用许多编程语言重现,包括 C++ 和汇编语言。原因是FPU 使用浮点格式。您可以在此处阅读详细信息:

http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_14/CH14-1.html#HEADING1-19

一般规则:永远不要期望浮动的确切结果- 点操作。永远不要比较两个浮点数,使用区间,例如:不要测试 f1 == f2,而是使用 f1 > (f2-e)且f1< ( f2 + e ), e 是一些小值。

This behavior can be reproduced in many programming languages, including C++ and Assembly. The reason is floating point format using by FPU. You can read details here:

http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_14/CH14-1.html#HEADING1-19

General rule: never expect exact result of floating-point operations. Never compare two floating point numbers, use interval, for example: instead of testing f1 == f2, use f1 > (f2 -e) and f1 < ( f2 + e ), e is some small value.

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