通用编程:十进制数、浮点数

发布于 2024-08-01 20:04:14 字数 416 浏览 11 评论 0原文

我可能完全错了,我对此一无所知,但我对编程语言中的十进制数字数据类型有疑问。 我知道浮点数并不完全精确,因为它们以带有幂或其他东西的二进制形式存储,但我一直想知道为什么十进制数字数据类型不仅仅存储数字,就好像没有小数一样,所以计算如下如果没有小数,则在后面加上。 就像在这种情况下:

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 技术交流群。

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

发布评论

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

评论(3

浅忆 2024-08-08 20:04:14

这正是他们所做的。 浮点数以指数形式存储。 假设我们正在使用基于十进制的计算机,因此我不必将所有这些数字更改为二进制。

您正在将 2.159 * 3.507 相乘,但实际上 2.159 存储为 2159 * 10^-33.507 > 存储为 3507 * 10^-3。 由于我们正在使用基于十进制的系统,因此假设为 10,因此我们只需存储 -3 而无需存储 10 ,如下所示:2159,-33507,-3-3 是“浮点”的位置:随着点向左移动,浮点减小(.3507 存储为 3507,-4< /code>),并且随着点向右移动,浮点数增加(35.07 存储为 3507,-2)。

当将两者相乘时,十进制数(或二进制计算机上的二进制数)是唯一相乘的东西。 浮点被添加!所以在幕后发生的事情是:

2.159 * 3.507
2159,-3 * 3507,-3
2159 * 3507,-3 + -3
7571613,-6

7571613,-6 只是 7571613 * 10^-6 (记住我们可以假设 10 因为我们正在使用十进制计算机),这与 7.571613 相同。

当然,浮点数不一定是-3,它可以是任何适合存储的东西:

21590 * .3507
2159,1 * 3507,-4
2159 * 3507,1 + -4
7571613,-3
7571.613

当然,大多数计算机不以十进制存储东西,所以实际的数字全部都是二进制,浮点数类似于 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 actuality 2.159 is stored as 2159 * 10^-3 and 3.507 is stored as 3507 * 10^-3. Since we're working on a decimal-based system, the 10 is assumed, so we only really have to store -3 without the 10, like this: 2159,-3 or 3507,-3. The -3 is the location of the "floating point": as the point moves left the floating point decreases (.3507 is stored as 3507,-4) and as the point moves right the floating point increases (35.07 is stored as 3507,-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:

2.159 * 3.507
2159,-3 * 3507,-3
2159 * 3507,-3 + -3
7571613,-6

7571613,-6 is just 7571613 * 10^-6 (remember we can assume the 10 because we're working on a decimal computer) which is the same as 7.571613.

Of course, the floating point doesn't have to be -3, it could be anything that fits into the storage:

21590 * .3507
2159,1 * 3507,-4
2159 * 3507,1 + -4
7571613,-3
7571.613

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 than 10^-3 -> -3. But you get the idea.

美人迟暮 2024-08-08 20:04:14

定点运算有很多实现。 然而,使用定点存储,我们经常很快就会用完小数位。 它非常适合货币交易,因为我们知道我们不会存储/关心任何无理数。

此外,对于许多其他事情,定点运算根本不值得花费这些开销。 浮点运算速度要快得多。

阅读内容:

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:

离不开的别离 2024-08-08 20:04:14

这称为“定点算术”人们一直在这样做。

请参阅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

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