PHP中的任意精度数学 [英] Arbitrary-Precision Math in PHP

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

问题描述

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

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.

第二,PHP中的BCMath库和GMP库有什么区别?我听说有人声称GMP的API是 fresher,但是idk。更好吗?

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?

最后一个问题是BCMath / GMP采用哪种数字。显然,它采用字符串形式的普通整数(例如 5.34),但是我已经看到了将BCMath函数直接与代表常规整数的八位字节串一起使用的实现(例如 \x12\x23\x45\x67 ),我听说它被称为 bigint,但是Google仍然没有为我提供任何帮助。

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.

推荐答案

任意精度数学到底是什么?

任意精度算术又称为大数数学,它介绍了一种对数字位数进行算术运算的方法。仅受可用内存量的限制。这与主机系统的CPU / ALU提供的固定精度算术 不符,其中所表示数字的最大大小/精度是寄存器位数的一个因素。

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.

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

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.

BCMath和BCMath之间的区别是什么GMP库

最明显的区别是GMP适用于[任意精度] 整数值,而BCMath允许[任意精度] 十进制 /类似浮点型的值。

都不是很难学习的API,但是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)

一个人选择另一个库的选择通常由预期用途(或给定平台的可用性)驱动。在深入研究MP应用程序之前,大多数库都可以满足要求并且通常是等效的(当然,在其类内,即,如果需要浮点数,请避免使用仅整数的库)。

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).

BCMath / GMP需要什么类型的数字?

与大多数任意精度数学包一样,这两个库使用字符串作为API ,即代表它们的输入和输出数值。

内部...某些软件包,例如GMP,都有自己的数字表示形式。这种结构的特定之处通常是在最小化存储需求和允许快速计算之间进行折衷(包括将这种结构串行化/反序列化到/来自文本文件的结构。)

示例问题中的 \x12\x23\x45\x67 被称为 BCD ,即二进制编码的十进制。它允许每个字节存储2个十进制数字,并且有时由任意精度算术库使用。

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.

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

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