bcmath PHP库有多少精度? [英] How much precision for a bcmath PHP library?

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

问题描述

我正在编写一个PHP库,该库具有一个使用 bcmath 扩展以获得任意精度。

I'm writing a PHP library that has a Number class that uses the bcmath extension for arbitrary precision.

我有两个问题:


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

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

bcmath 具有可选的 scale参数(默认为3位数字)。对于任何人都可以使用的通用Number类,什么级别的精确度会很高?像Perl这样的语言(具有任意精度数字)如何处理小数位数?

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?


推荐答案

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

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.

在任何系统上,直到32位为止,内置整数都是好的。系统支持64位值。您可以通过检查常量PHP_INT_MAX的值来检查系统所支持的内容,然后确定是否要承担数学库的开销。对于具有32位整数的系统,高于32位的所有内容都会自动转换为浮点数。除非您将内置函数用于舍入,printf,模数等,否则这不是问题。

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.

我对使用模数划分流量的方法感到有些困惑。我的网站以及在sprintf中使用%d格式化整数的方式: http://af-design.com/blog/2009/10/28/php-64-bit-integer-modulus-almost/

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/

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

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