bcmath PHP 库的精度是多少?
我正在编写一个 PHP 库,它有一个 Number 类,该类使用 bcmath
扩展 用于任意精度。
我有两个问题:
与使用内置 int 和 float 类型相比,bcmath 慢多少?
bcmath
有一个可选的scale 参数(默认为 3 位数字)。对于任何人都可以使用的通用 Number 类,什么水平的精度才是好的?像 Perl 这样的语言(具有任意精度数字)如何处理比例?
I'm writing a PHP library that has a Number class that uses the bcmath
extension for arbitrary precision.
I have two questions:
How much slower is bcmath compared to using the built-in int and float types?
bcmath
has an optional scale argument (that defaults to 3 digits). For an general purpose Number class that anyone could use, what would be a good level of precision? How do languages like Perl (that have arbitrary precision numbers) deal with scale?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将决定您需要支持的数字范围。内置值将比任何需要计算和与其他格式转换的值更快。
在任何系统上,内置整数在 32 位之前都很好,某些系统支持 64 位值。您可以通过检查常量 PHP_INT_MAX 的值来检查您的系统支持什么,并确定之后是否要承担数学库的开销。对于 32 位整数的系统,任何高于 32 位的数据都会自动转换为浮点数。这不是问题,除非您使用诸如 round、printf、modulus 等内置函数。
我对此很感兴趣,使用模数来划分进入我的网站的流量以及在 sprintf 中使用 %d 格式化整数: < a href="http://af-design.com/blog/2009/10/28/php-64-bit-integer-modulus-almost/" rel="nofollow noreferrer">http://af-design. com/blog/2009/10/28/php-64-bit-integer-modulus-almost/
I would decide what range of numbers you need to support. The built in values will be faster than any value that requires calculation and conversion to/from some other format.
Built in integers are good until 32 bits on any system, some systems support 64 bit values. You can check what your system supports by checking the value of the constant PHP_INT_MAX and determine if you want to carry the overhead of the math library after that. For systems with 32 bit integers, anything above 32 bits will be converted to a float automatically. This isn't an issue unless you are using built in functions for things like round, printf, modulus etc.
I was bit by this using modulus to divide traffic coming to my site as well as with formatting integers using %d in sprintf: http://af-design.com/blog/2009/10/28/php-64-bit-integer-modulus-almost/