PHP 中的任意精度数学

发布于 2024-11-27 22:46:58 字数 327 浏览 4 评论 0原文

我目前正在尝试弄清楚如何在 PHP 中使用任意精度数字。所以我想我的第一个问题是任意精度数学到底是什么。我尝试在谷歌上搜索一个好的定义,但由于某种原因,没有人能用足够简单的词语来表达它。

其次,PHP中的BCMath和GMP库有什么区别?我听说过 GMP 的 API“更新鲜”,但我不知道。有一个更好吗?

我的最后一个问题是 BCMath/GMP 采用什么类型的数字。显然它需要字符串形式的普通整数(例如“5.34”),但我见过 BCMath 函数直接与表示常规整数的八位字节字符串一起使用的实现(例如“\x12\x23\x45\x67”),我将其听说被称为“bigint”,但谷歌再次没有为我提供任何结果。

I'm currently trying to figure out how to work with arbitrary-precision numbers in PHP. So I guess my first question would be what exactly is arbitrary-precision math. I tried Googling for a good definition but for some reason nobody can put it in simple enough words.

Second, what are the differences between the BCMath and GMP libraries in PHP? I've heard claims that GMP's API is "fresher", but idk. Is one better?

And my final question would be what type of numbers BCMath/GMP takes. Obviously it takes normal integers in string form (e.g. "5.34"), but I've seen implementations where BCMath functions have been used directly with octet strings representing regular integers (e.g. "\x12\x23\x45\x67"), which I've heard as being called "bigint", but again Google has yielded nothing for me.

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

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

发布评论

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

评论(2

想挽留 2024-12-04 22:46:58

任意精度数学到底是什么?
任意精度算术又名“bignum math”,引入了一种对数字执行算术运算的方法,其位数仅受可用内存量的限制。这与主机系统的 CPU/ALU 提供的固定精度算术不同,其中所表示数字的最大大小/精度是寄存器位数的一个因素这些硬件处理器。

固定精度算法在存储方面速度快、效率高,并且是内置的/通用的。然而,它适用于有限的(如果有时“足够大”)数值范围。任意精度算术速度较慢,有点浪费存储空间,并且需要专门的库,例如 GMP 或 BCMath。

BCMath 和 GMP 库之间有什么区别
最显着的区别是 GMP 适用于[任意精度]整数值,而 BCMath 允许[任意精度]小数/类似浮点数的值。
这两个 API 都不难学,但 BCMath 可能更直观一点(除了支持类似浮点值之外)

人们对特定库的选择通常是由预期用途(或由其可用性决定)给定平台)。在您深入研究 MP 应用程序之前,大多数库都符合要求并且通常是等效的(当然在其类别内,即如果您需要浮点数,请避免仅使用整数库)。

BCMath/GMP 采用什么类型的数字?
与大多数任意精度数学包一样,这两个库使用字符串作为其 API,即表示其输入和输出数值。
在内部...一些软件包(例如 GMP)有自己的数字表示形式。此类结构的具体特征通常是最小化存储需求和允许快速计算之间的折衷(包括将此类结构“序列化/反序列化”到文本文件或从文本文件“序列化/反序列化”。)
问题中的示例 "\x12\x23\x45\x67" 称为 BCD 即二进制编码的十进制。它允许每个字节存储 2 位十进制数字,有时由任意精度算术库使用。

what exactly is arbitrary-precision math?
Arbitrary precision arithmetic aka "bignum math", introduces a way of performing arithmetic operations on numbers which number of digits are only limited by the amount of memory available. This is in departure with the fixed precision arithmetic which is afforded by the CPUs/ALUs of the host systems and where the maximum size/precision of the number represented is a factor of the number of bits of the registers of these hardware processors.

Fixed precision arithmetic is fast, efficient with regards to storage and it is built-in/universally available. It is however applicable to limited (if only sometimes "big enough") numerical ranges. Arbitrary precision arithmetic is slower, somewhat wasteful of the storage and requires specialized libraries such as GMP or BCMath.

what are the differences between the BCMath and GMP libraries
The most salient difference is that GMP works on [arbitrary precision] integer values, whereby BCMath allows [arbitrary precision] decimal / float-like values.
Neither API is hard to learn, but BCMath may be a bit more intuitive (in addition to support float-like values)

One's selection of a particular library over another one is typically driven by the intended use (or by the availability on a given platform). Until you get heavily into MP applications, most library will fit the bill and be generally equivalent (within its class of course, i.e. avoid integer-only library if you need floating point numbers).

what type of numbers BCMath/GMP takes?
As with most arbitrary precision math packages, these two libraries use strings for their API, i.e. to represent their input and output numeric values.
Internally... Some packages like GMP have their own representation for the numbers. The specific of such structures is typically a compromise between minimizing storage requirements and allowing fast computations (including that of "serializing/deserializing" such structures to/from text files.)
The example "\x12\x23\x45\x67" in the question is known as BCD i.e. Binary Coded Decimal. It allows storing 2 decimal digits per byte and is sometimes used by Arbitrary Precision Arithmetic libraries.

小猫一只 2024-12-04 22:46:58

GMP 比 BCMath 快得多,尽管使用 OpenSSL 可以使 BCMath 更快。以下是比较各种技术的基准:

http://phpseclib.sourceforge.net/math/intro。 html

GMP is a ton faster BCMath, although BCMath can be made faster using OpenSSL. Here's a benchmark comparing the various techniques:

http://phpseclib.sourceforge.net/math/intro.html

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