使用类似整数的数据类型实现小数/任意算术

发布于 2024-11-13 09:15:02 字数 620 浏览 4 评论 0原文

这可能之前已经被问过,但我找不到任何相关的内容。

通过任意/固定数量的整数的专门类或结构来实现一种任意/十进制算术是否可能/性能良好?

让我澄清一下;浮点数(据我理解),使用尾数、指数,也许还有符号位。它们能够代表 1/2、1/4、1/8 等……但不能代表 1/10。

我的建议是这样的:让数字用普通的 intint64_t 或一系列这些来表示,只保留完全不超过 10 的幂的值被整数的范围覆盖(因此对于 32 位 int,限制为 10^8-1)。这只是为了让计算变得容易。再加上一个指数,就可以知道应该乘以 10 的多少次方才能得到实数。

例如:125.36 将由等于 12536 的 int 和等于 -2 的 10 次方表示。这个数字实际上需要尽可能多的int,以及一堆特殊函数。

另一个例子:4,294,967,297(即 2^32+1)需要两个 int,一个等于 4,另一个等于 294,967,297,十的幂等于 0。

这好吗?想法,为了更简单/更快的算术而丢弃所有这些内存?我还没有完全想到溢出部分,也许计算 int32_t+int32_t=int64_t 可以很好地解决这个问题?

This probably has probably been asked before, but I couldn't find anything relevant.

Would it be possible/performant to implement a kind of arbitrary/decimal arithmatic through a specialized class or struct of an arbitrary/fixed amount of integers?

Let me clarify; floats (as I understand them), use a mantissa, exponent and perhaps a sign bit. They are capable of representing 1/2, 1/4, 1/8, etc... but not, say 1/10.

What I propose is something like this: let the number be represented by a plain int or int64_t or a series of these, keeping only the values up to a power of ten completely covered by the range of the integer (so for a 32-bit int, the limit would be 10^8-1). This only to keep calculation easy. Add to that an exponent, that will say what power of ten it should be multiplied to get the real number.

An example: 125.36 would be represented by an int equal to 12536 and a power of ten equal to -2. This number would in effect take as much as too ints, and a bunch of special functions.

Another example: 4,294,967,297 (which is 2^32+1) would take two ints, one equal to 4, the other equal to 294,967,297, and a power of ten equal to 0.

Is this a good idea, throwing away all this memory for a simpler/faster arithmetic? I haven't quite thought out the overflow part, perhaps calculating int32_t+int32_t=int64_t would solve that beautifully?

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

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

发布评论

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

评论(2

拒绝两难 2024-11-20 09:15:02

您正在考虑二进制算术的精度限制。我认为用二进制精度换取十进制精度不会给你带来任何好处。

你是对的,二进制可以表示1/2和1/4,但不能表示1/10,而十进制可以。但两者都不能代表1/3。所以精度总是会受到一些限制。 (对于小数计算,它可能是隐藏的,因为我们在现实生活中获取的大多数数据都是小数格式,这意味着大多数现实生活数据都会四舍五入到任意小数位。)

也就是说,您可以查看 python Decimal package (如果 python 是你的语言),它实现了你似乎想要的行为:计算十进制数学我们在学校“根据需要”精确学习的方式。

You are pondering on the precision limitations of binary arithmetic. I think that trading binary precision for decimal precision won't bring you anywhere.

You are right, binary can represent 1/2 and 1/4, but not 1/10, which decimal can. But both of them cannot represent 1/3. So there always will be some limitation of precision. (It may be hidden for decimal calculations, since we get most data in real life in decimal format, that means that most real life data are rounded to any number decimal places.)

That said, you can look at python Decimal package (if python is your language), which implemented the behaviour, that you seem to intend: calculate decimal math the way we learned at school with "as needed" precision.

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