PHP中任意大整数的算法 [英] Arithmetic with Arbitrarily Large Integers in PHP

查看:218
本文介绍了PHP中任意大整数的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,因此PHP不是处理任意大整数的最佳语言,因为它本身只支持32位有符号整数。我想要做的是创建一个可以表示任意大二进制数的类,并且能够对其中两个执行简单的算术运算(加/减/乘/除)。

Ok, so PHP isn't the best language to be dealing with arbitrarily large integers in, considering that it only natively supports 32-bit signed integers. What I'm trying to do though is create a class that could represent an arbitrarily large binary number and be able to perform simple arithmetic operations on two of them (add/subtract/multiply/divide).

我的目标是处理128位整数。

My target is dealing with 128-bit integers.

我正在研究几种方法,以及我看到的问题。任何关于你会选择什么以及如何进行评论的输入或评论将不胜感激。

There's a couple of approaches I'm looking at, and problems I see with them. Any input or commentary on what you would choose and how you might go about it would be greatly appreciated.

方法#1:创建128 -bit整数类,在内部将整数存储为四个32位整数。这种方法的唯一问题是,我不确定在操作两个操作数的单个块时如何处理溢出/下溢问题。

Approach #1: Create a 128-bit integer class that stores its integer internally as four 32-bit integers. The only problem with this approach is that I'm not sure how to go about handling overflow/underflow issues when manipulating individual chunks of the two operands.

方法#2:使用bcmath扩展,因为它看起来像它旨在解决的问题。我唯一担心采用这种方法是bcmath扩展的比例设置,因为我的128位整数中不会有任何舍入错误;他们必须准确。我还担心能够最终将bcmath函数的结果转换为二进制字符串(我稍后需要将其转换为某些mcrypt加密函数)。

Approach #2: Use the bcmath extension, as this looks like something it was designed to tackle. My only worry in taking this approach is the scale setting of the bcmath extension, because there can't be any rounding errors in my 128-bit integers; they must be precise. I'm also worried about being able to eventually convert the result of the bcmath functions into a binary string (which I'll later need to shove into some mcrypt encryption functions).

方法#3:将数字存储为二进制字符串(可能是LSB优先)。从理论上讲,我应该能够以这种方式存储任意大小的整数。我所要做的就是编写四个基本算术函数,对两个二进制字符串执行add / sub / mult / div并生成二进制字符串结果。这正是我需要交给mcrypt的格式,所以这是一个额外的加号。这是我认为目前最有希望的方法,但我得到的一个问题是PHP没有提供任何方法来操纵各个位(我知道)。我相信我必须把它分解成字节大小的块(没有双关语意),此时我对方法#1处理溢出/下溢的问题适用。

Approach #3: Store the numbers as binary strings (probably LSB first). Theoretically I should be able to store integers of any arbitrary size this way. All I would have to do is write the four basic arithmetic functions to perform add/sub/mult/div on two binary strings and produce a binary string result. This is exactly the format I need to hand over to mcrypt as well, so that's an added plus. This is the approach I think has the most promise at the moment, but the one sticking point I've got is that PHP doesn't offer me any way to manipulate the individual bits (that I know of). I believe I'd have to break it up into byte-sized chunks (no pun intended), at which point my questions about handling overflow/underflow from Approach #1 apply.

推荐答案

PHP GMP扩展将更好。作为额外的奖励,您可以使用它进行十进制到二进制转换,如下所示:

The PHP GMP extension will be better for this. As an added bonus, you can use it to do your decimal-to-binary conversion, like so:

gmp_strval(gmp_init($n, 10), 2);

这篇关于PHP中任意大整数的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆