通用编程:十进制数、浮点数
我可能完全错了,我对此一无所知,但我对编程语言中的十进制数字数据类型有疑问。 我知道浮点数并不完全精确,因为它们以带有幂或其他东西的二进制形式存储,但我一直想知道为什么十进制数字数据类型不仅仅存储数字,就好像没有小数一样,所以计算如下如果没有小数,则在后面加上。 就像在这种情况下:
2.159 * 3.507 --> 2159 * 3507 = 7571613
^^^ ^^^
123 456
6 decimals in total... 7571613 -> 7.571613
^^^^^^
654321
所以 2.159 * 3.507 = 7.571613
为什么它不能像这样工作呢?
I'm probably completely wrong, and I don't really know anything about it, but I have a question about decimal number data types in programming languages. I understand that floats aren't completely precise, because they're stored in binary with a power or something, but I always wondered why decimal number data types don't just store a number as if there was no decimal, so do calculations as if there wasn't a decimal, and then add it in after. Like in this situation:
2.159 * 3.507 --> 2159 * 3507 = 7571613
^^^ ^^^
123 456
6 decimals in total... 7571613 -> 7.571613
^^^^^^
654321
so 2.159 * 3.507 = 7.571613
Why can't it just work like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这正是他们所做的。 浮点数以指数形式存储。 假设我们正在使用基于十进制的计算机,因此我不必将所有这些数字更改为二进制。
您正在将
2.159 * 3.507
相乘,但实际上2.159
存储为2159 * 10^-3
和3.507
> 存储为3507 * 10^-3
。 由于我们正在使用基于十进制的系统,因此假设为10
,因此我们只需存储-3
而无需存储10
,如下所示:2159,-3
或3507,-3
。-3
是“浮点”的位置:随着点向左移动,浮点减小(.3507
存储为3507,-4< /code>),并且随着点向右移动,浮点数增加(
35.07
存储为3507,-2
)。当将两者相乘时,十进制数(或二进制计算机上的二进制数)是唯一相乘的东西。 浮点被添加!所以在幕后发生的事情是:
7571613,-6
只是7571613 * 10^-6
(记住我们可以假设10
因为我们正在使用十进制计算机),这与7.571613
相同。当然,浮点数不一定是
-3
,它可以是任何适合存储的东西:当然,大多数计算机不以十进制存储东西,所以实际的数字全部都是二进制,浮点数类似于
2^-9 -> -9
而不是10^-3 -> -3
。 但你明白了。That's exactly what they do. A floating-point number is stored in exponent form. Let's assume that we're working on a decimal-based computer so I don't have to change all these numbers to binary.
You're multiplying
2.159 * 3.507
, but in actuality2.159
is stored as2159 * 10^-3
and3.507
is stored as3507 * 10^-3
. Since we're working on a decimal-based system, the10
is assumed, so we only really have to store-3
without the10
, like this:2159,-3
or3507,-3
. The-3
is the location of the "floating point": as the point moves left the floating point decreases (.3507
is stored as3507,-4
) and as the point moves right the floating point increases (35.07
is stored as3507,-2
).When you multiply the two together, the decimal number (or the binary number on a binary computer) is the only thing that gets multiplied. The floating point gets added! So behind the scenes what happens is:
7571613,-6
is just7571613 * 10^-6
(remember we can assume the10
because we're working on a decimal computer) which is the same as7.571613
.Of course, the floating point doesn't have to be
-3
, it could be anything that fits into the storage:And of course, most computers don't store things in decimal, so the actual numbers would be all in binary, and the floating point would be something like
2^-9 -> -9
rather than10^-3 -> -3
. But you get the idea.定点运算有很多实现。 然而,使用定点存储,我们经常很快就会用完小数位。 它非常适合货币交易,因为我们知道我们不会存储/关心任何无理数。
此外,对于许多其他事情,定点运算根本不值得花费这些开销。 浮点运算速度要快得多。
阅读内容:
There are quite a few implementations of fixed-point arithmetic. However, we often run out of decimal places very, very quickly with fixed-point storage. It's ideal for monetary transactions, where we know that we aren't going to store/care about any irrational numbers.
Additionally, for a lot of other things, fixed-point arithmetic just isn't worth the overhead. Floating point is just a lot faster.
Things to read:
这称为“定点算术”人们一直在这样做。
请参阅http://gameprogrammer.com/4-fixed.html
This is called "fixed-point arithmetic" People do it all the time.
See http://gameprogrammer.com/4-fixed.html