环境(例如 Ruby)如何处理大量整数?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.
请参阅 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.
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.
基本上,它可以归结为长加法/乘法/除法/减法。可以从那里完成很多优化(废话),因此不建议您自己进行优化。我建议您查看 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.