简体   繁体   English

bcmath PHP库有多少精度?

[英]How much precision for a bcmath PHP library?

I'm writing a PHP library that has a Number class that uses the bcmath extension for arbitrary precision. 我正在编写一个PHP库,该库具有一个Number类,该类使用bcmath扩展来实现任意精度。

I have two questions: 我有两个问题:

  1. How much slower is bcmath compared to using the built-in int and float types? 与使用内置的int和float类型相比,bcmath的速度要慢多少?

  2. bcmath has an optional scale argument (that defaults to 3 digits). bcmath有一个可选的scale参数 (默认为3位数)。 For an general purpose Number class that anyone could use, what would be a good level of precision? 对于任何人都可以使用的通用Number类,什么级别的精确度会很高? How do languages like Perl (that have arbitrary precision numbers) deal with scale? 像Perl这样的语言(具有任意精度数字)如何处理小数位数?

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. 内置整数在任何系统上都可以使用,直到32位为止,某些系统支持64位值。 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. 您可以通过检查常量PHP_INT_MAX的值来检查系统所支持的内容,然后确定是否要承担数学库的开销。 For systems with 32 bit integers, anything above 32 bits will be converted to a float automatically. 对于具有32位整数的系统,高于32位的所有内容都会自动转换为浮点数。 This isn't an issue unless you are using built in functions for things like round, printf, modulus etc. 除非您将内置函数用于倒圆,printf,模数等,否则这不是问题。

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/ 我对使用模数来划分进入我的网站的流量以及在sprintf中使用%d格式化整数感到吃惊: http : //af-design.com/blog/2009/10/28/php-64-bit-整数模数/

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

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