bcmath PHP 库的精度是多少?

发布于 2024-08-22 02:50:35 字数 552 浏览 4 评论 0原文

我正在编写一个 PHP 库,它有一个 Number 类,该类使用 bcmath 扩展 用于任意精度。

我有两个问题:

  1. 与使用内置 int 和 float 类型相比,bcmath 慢多少?

  2. 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:

  1. How much slower is bcmath compared to using the built-in int and float types?

  2. 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 技术交流群。

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

发布评论

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

评论(1

浴红衣 2024-08-29 02:50:35

我将决定您需要支持的数字范围。内置值将比任何需要计算和与其他格式转换的值更快。

在任何系统上,内置整数在 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/

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