环境(例如 Ruby)如何处理大量整数?

发布于 2024-08-07 14:55:05 字数 203 浏览 11 评论 0原文

我的 Ruby (MRI) 整数拒绝溢出。我注意到类从 fixnum 更改为 bignum,但我想知道这是如何建模的以及 ruby​​ 使用什么样的过程对这些大整数执行算术。我在 SCHEME 以及其他环境中都看到过这种行为。

我问这个问题是因为我想在 C 程序中实现类似的东西,并且想知道 bignum + bignum 如何简化为原始操作。

有什么指点吗?

My integers in Ruby (MRI) refuse to overflow. I've noticed the class change from fixnum to bignum but I'm wondering how this is modeled and what sort of process ruby uses to perform arithmetic on these massive integers. I've seen this behaviour in SCHEME as well as other environments.

I ask because I'd like to implement something similar in a C program and would like to know how bignum + bignum reduces to primitive operations.

Any pointers?

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

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

发布评论

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

评论(4

原来是傀儡 2024-08-14 14:55:05

Python 也这样做。

基本上,它不是将数字视为自然适合硬件架构的一串位(例如 32 位),而是将数字视为一串 32 位数字,然后实现所有算术运算以处理来自 32 位的进位- 位数字到另一个。随着数字变长,这还涉及分配额外的 32 位数字。这比看起来容易。

例如,99 * 99 小于 100 * 100(即 10,000),因此人们可能会假设两个 2 位数字相乘将产生不超过 4 位数字的结果。当每个数字都是 32 位字时,同样的情况也适用。

您可能想尝试在 Ruby 中实现此功能,只是为了好玩,使用某种允许固定二进制数量的类型。我相信 FixNum 类会起作用。

Python also does this.

Basically, instead of treating a number as a string of bits that naturally fits the hardware architecture, (32 bits for instance) it treats a number as a string of 32-bit digits, then implements all the arithmetic operations to handle carries from one 32-bit digit to another. That also involves allocating additional 32 bit digits as the number grows longer. This is easier than it seems.

For instance, 99 * 99 is less that 100 * 100 which is 10,000, therefore one might assume that multiplying two 2-digit numbers will produce a result no longer than 4 digits. Same thing applies when each digit is a 32-bit word.

You might want to try implementing this in Ruby, just for fun, using some type that allows fixed binary quantities. I believe the FixNum class would work.

恋竹姑娘 2024-08-14 14:55:05

请参阅 Numerical Recipes for C 书中的第 20.6 节: http://www.nrbook.com/ a/bookcpdf.php

这是任意精度数学的一个很好的实现。如果您想变得更奇特,您可以创建一个 C++ 类来重载运算符,然后实现这些函数。或者您可以直接致电他们。

Look at section 20.6 in the Numerical Recipes for C book: http://www.nrbook.com/a/bookcpdf.php

It's a great implementation of arbitrary precision math. If you wanted to get fancy you'd make a C++ class that overloads the operators and then implements these functions. Or you can just call them directly.

空宴 2024-08-14 14:55:05

Erlang 也这样做。您可以查看 erl_interface 模块中的源代码(C 语言)。

Erlang does this as well. You can take a look at the source code (in C) in the erl_interface module.

亣腦蒛氧 2024-08-14 14:55:05

基本上,它可以归结为长加法/乘法/除法/减法。可以从那里完成很多优化(废话),因此不建议您自己进行优化。我建议您查看 GMP(gnu 多精度)项目,该项目静态或动态链接到您的应用程序。它并不难使用,但有一些 C++ 和其他包装器可以让您更简单地使用它。如果您正在处理浮点运算,请使用 MPFR,它可以正确处理舍入。

Basically, it boils down to long-addition/multiplication/division/subtraction. There are a lot of optimizations that can be done from there (duh), so rolling your own isn't really recommended. I'd recommend checking out the GMP (gnu multi-precision) project, which static or dynamic links into your app. It's not hard to use, but there's a few C++ and other wrappers for it that let you work with it more simply. If you're doing floating point stuff, get MPFR, which handles rounding properly.

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