Thursday, November 5, 2009

Big Integers

The runtime recently underwent its first change that is not 100% compatible with Zend based PHP: arbitrary precision integers.

In Zend PHP, an integer (represented as a long, which is generally 32 bits or 64 bits big, depending on the platform) will overflow to a float. This happens when you specify a large literal integer in the source code, or upon certain arithmetical operations.

We've decided to integrate seamless "bignums" into Roadsend PHP instead. This means you can specify an integer as big as you want (only limited by memory), and that arithmetical operations will yield accurate results even when the numbers get large.

Internally, integers that can fit into a hardware word are still represented this way, for speed. They will convert to a bignum (using GMP library) only when necessary.

Zend PHP 5.x and current 6 has support for this now only through the GMP or BC math extensions. This is awkward since you have to juggle the numbers as resources. But, according to recent developer notes, this same functionality may end up in Zend PHP at some point as well.

So, either we're a little ahead of the curve, or we're diverging slightly. Either way we think this is a win for the language as a whole.