简体   繁体   English

PHP中的任意精度数学

[英]Arbitrary-Precision Math in PHP

I'm currently trying to figure out how to work with arbitrary-precision numbers in PHP. 我目前正试图弄清楚如何使用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? 其次,PHP中的BCMath和GMP库有什么区别? I've heard claims that GMP's API is "fresher", but idk. 我听说GMP的API是“更新鲜”,但是我很高兴。 Is one better? 一个更好吗?

And my final question would be what type of numbers BCMath/GMP takes. 我最后的问题是BCMath / GMP需要什么类型的数字。 Obviously it takes normal integers in string form (eg "5.34"), but I've seen implementations where BCMath functions have been used directly with octet strings representing regular integers (eg "\\x12\\x23\\x45\\x67"), which I've heard as being called "bigint", but again Google has yielded nothing for me. 显然它需要字符串形式的正常整数(例如“5.34”),但我已经看到了BCMath函数直接用于表示常规整数的八位字节字符串的实现(例如“\\ x12 \\ x23 \\ x45 \\ x67”),其中我听说被称为“bigint”,但谷歌再也没有给我带来任何好处。

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. 任意精度算术又称“bignum数学”,介绍了一种对数字执行算术运算的方法,其数字位数仅受可用存储量的限制。 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. 这与主机系统的CPU / ALU提供的固定精度算法不同 ,并且所表示的数字的最大大小/精度是这些硬件处理器的寄存器的位数的因子。

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. 任意精度算术都比较慢,有点浪费存储,需要专门的库,如GMP或BCMath。

what are the differences between the BCMath and GMP libraries BCMath和GMP库之间有什么区别
The most salient difference is that GMP works on [arbitrary precision] integer values, whereby BCMath allows [arbitrary precision] decimal / float-like values. 最显着的区别是GMP在[任意精度] 数值上工作,因此BCMath允许[任意精度] 十进制 /浮点数值。
Neither API is hard to learn, but BCMath may be a bit more intuitive (in addition to support float-like values) 这两种API都难以学习,但BCMath可能更直观(除了支持浮点数值)

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, ie avoid integer-only library if you need floating point numbers). 在你深入研究MP应用程序之前,大多数库都符合要求并且通常是等效的(当然在它的类中,即如果你需要浮点数,则避免使用整数库)。

what type of numbers BCMath/GMP takes? BCMath / GMP采用什么类型的数字?
As with most arbitrary precision math packages, these two libraries use strings for their API, ie to represent their input and output numeric values. 与大多数任意精度数学包一样,这两个库使用字符串作为其API,即表示其输入和输出数值。
Internally... Some packages like GMP have their own representation for the numbers. 内部......像GMP这样的一些软件包有自己的数字表示。 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 ie Binary Coded Decimal. 问题中的示例"\\x12\\x23\\x45\\x67"称为BCD,即二进制编码的十进制。 It allows storing 2 decimal digits per byte and is sometimes used by Arbitrary Precision Arithmetic libraries. 它允许每个字节存储2个十进制数字,有时由任意精度算术库使用。

GMP is a ton faster BCMath, although BCMath can be made faster using OpenSSL. 尽管使用OpenSSL可以使BCMath更快,但GMP速度更快。 Here's a benchmark comparing the various techniques: 这是比较各种技术的基准:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM