Web中长数字的操作

发布于 2024-12-20 13:13:09 字数 483 浏览 0 评论 0原文

我想编写(不使用现有的)一个可以处理长数字(我的意思是至少几百位数字)的库(或其他东西)。

第一个问题:为其选择一种语言。 Perl/JavaScript/PHP 哪一个更好?

第二个问题:如何实现长数字的运算?我唯一得到的就是像使用数组一样使用它们,例如:

arr1 = (12, 34); //1234
arr2 = (98, 76); //9876
sum = longnumbers_add(arr1, arr2); // +
// 34 + 76 = 110 = 10 -..> 1
// 12 + 98 = 110 = 110 + 1 = 11 -..> 1
//sum == (1, 11, 10);

但它的速度很慢(至少我在 PHP 中的尝试)。也许有一些“移位位”超快速方法?

PS

我知道有 gmp 和其他很酷的库。

任何帮助表示赞赏。

I'd like to write (not to use existing one) a library (or something) that works with long numbers (I mean at least few hundred digits).

The first question: choosing a language for it. Which one is better: Perl/JavaScript/PHP?

The second question: how to implement operations with long numbers? The only thing I get is to work with them as with arrays, e.g.:

arr1 = (12, 34); //1234
arr2 = (98, 76); //9876
sum = longnumbers_add(arr1, arr2); // +
// 34 + 76 = 110 = 10 -..> 1
// 12 + 98 = 110 = 110 + 1 = 11 -..> 1
//sum == (1, 11, 10);

But it worked to slow (at least with my try in PHP). Maybe there's some "shift bits" super-fast method?

P.S.

I know there are gmp and other cool libraries.

Any help is appreciated.

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

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

发布评论

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

评论(3

空袭的梦i 2024-12-27 13:13:09

我为了好玩写了几个大数字库。就语言而言,我会选择您最熟悉的一种或您想了解更多的一种。对我来说,最近使用的是 JavaScript,但在我看来,这些语言都或多或少适合大数。但对于网络来说,很大一部分决定取决于您是想在客户端还是服务器端进行计算。在客户端,JavaScript 几乎是唯一的选择。

我写了几篇博客文章,展示了基本算法以及我如何选择存储它们。请注意,这些仅适用于大整数(因此没有小数点),但基础知识仍然适用。

以下是我的经验中的一些想法:

  • 从概念上讲,这可能是最简单的以 10 为基数存储数字,因为它更熟悉,但它实际上并没有改变太多的实现。不过,较大的基数效率更高(在内存和速度方面)。
  • 将数字保持为“小端”顺序几乎总是更简单,因此最低有效数字在数组中排在第一位(或者如果您使用字符串,则将它们反转存储)。
  • 在它正常工作之前,不要太担心性能。最快的做事方法有时会令人惊讶,所以无论如何你都会出错,而且完成后很容易返回并优化每个操作。
  • 如果您想支持小数,最简单的方法是先编写整数类型,然后在此基础上实现浮点数。基本上,您可以分离出代码来跟踪小数位,从而使算法保持清晰。
  • 您需要决定是否要使用二进制补码(10 的补码,或等效于您选择的任何基数也适用)或符号大小(将符号与数字分开存储)用于处理负数。每种方法在某些方面比较复杂,而在其他方面则比较简单。一般来说,如果大小固定,则 n 补码可能更简单,但对于任意大小的数字,符号量值可能更简单。

I've written a couple big number libraries for fun. As far as the language, I would either choose whichever one you're most comfortable with or one you want to learn more about. For me, that was JavaScript most recently, but in my opinion, none of those languages are any more or less suited to large numbers. For the web though, a large part of the decision comes down to whether you want to do the calculations on the client or server side. On the client side, JavaScript is pretty much the only option.

I wrote a couple blog posts demonstrating the basic algorithms and how I chose to store them. Note that these are for big integers only (so no decimal point), but the basics still apply.

Here are a few thoughts from my experience:

  • It's probably easiest conceptually to store the numbers in base 10 because it's more familiar, but it really doesn't change much of the implementation. Larger bases are more efficient though (in both memory and speed).
  • It's almost always simpler to keep the digits in "little endian" order, so the least significant digit comes first in the array (or if you used strings, store them reversed).
  • Don't worry too much about performance until you get it working. The fastest way to do things can sometimes be surprising so you'll get it wrong anyway, and it's easy to go back and optimize each operation once you're done.
  • If you want to support decimals, it can be easiest to write an integer type first, then implement floating point numbers on top of that. Basically, you can separate out the code to keep track of decimal places so it keeps the algorithms cleaner.
  • You'll need to decide if you want to use two's complement (10's complement, or the equivalent for whatever base you choose also works) or sign-magnitude (where you store the sign separately from the digits) for handling negative numbers. Each method is more complicated in some areas and simpler in others. Generally though, n's-complement is probably simpler if you have a fixed size, but sign-magnitude might be simpler for arbitrarily-sized numbers.
尐籹人 2024-12-27 13:13:09

我不确定您是否想编写一个高级库来处理长数字(例如计算素数或其他东西),或者只是为了学习目的而实现低级运算(加法、乘法)。如果您对前者感兴趣,您应该考虑使用 Python,而不是 JavaScript/PHP/Perl。 Python 在其标准库中内置了对任意大整数的支持和用于精确浮点运算的小数类。

I'm not sure whether you want to write a high-level library for working with long numbers (e.g. for calculating primes or something) or just implement the low-level operations (addition, multiplication) for learning purposes. If you're interested in the former, you should consider Python instead of JavaScript/PHP/Perl. Python has builtin-support for arbitrarily large integers and a decimal class for exact floating point arithmetic in its standard library.

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