views:

101

answers:

3

Possible Duplicate:
Working with large numbers in PHP.

I run a completely useless Facebook app. I'm having a problem with PHP's support for integers. Basically, users give themselves ridiculous numbers of points. The current "king" has 102,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,002,557,529,927 points.

PHP does not seem to play well with large integers. When someone tries to add more than a certain amount of points it will fail because PHP treats those numbers as infinite.

Is there some math library for working with ridiculously large numbers? Should I treat the numbers as strings and write my own?

We're talking numbers which are 2^20 digits in length or longer. They don't need to be accurate (any errors are chalked up to the low quality of the app in general) nor does it need to be high performance. I just need something which allows much longer numbers.

(For those of you who are curious, we store our numbers in the cloud, so storage cost isn't a huge issue.)

+3  A: 

PHP uses floats, or signed 32-bit ints to store numbers. Clearly this is not enough. You can use arbitrary precision arithmetic to store these large numbers. See the PHP book on BC Math for more information:

http://php.net/manual/en/book.bc.php

Delan Azabani
+2  A: 

Two PHP libraries for working with large numbers are BC Math and GMP.

Peter Ajtai
GMP was limiting me to 10^50 when I added numbers. I could store whatever I wanted, but it would cap at 10^50 if I tried to add. I'll try BC Math.
Tyler Menezes
A: 

There is a GMP extension for PHP.

GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface.

http://gmplib.org/

Kendall Hopkins