How much precision for a bcmath PHP library?
Asked Answered
B

1

1

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?

Bragg answered 15/2, 2010 at 7:6 Comment(0)
I
1

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/

Intersect answered 18/2, 2010 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.