You are here: maximum integer size? « PHP Programming Language « IT news, forums, messages
maximum integer size?

Posted by yawnmoth on 02/16/06 20:57

I'm trying to figure out how big integers in PHP can be and am having
some difficulty.

My first idea was to try something like this:

<?
for ($bits=0; (1<<($bits+1)) > (1<<$bits); $bits++);
echo $bits;
?>

The result was 30. This, however, is incorrect as the following
demonstrates:

<?
$num = 1<<30;

echo "num = $num\r\n";
echo 'num<<1 = '.($num<<1)."\r\n";
echo 'num*2 = '.($num*2)."\r\n";
?>

That my first idea would return the wrong result doesn't surprise me,
though, since bitwise operators in PHP are treated as arithematic ones
and not logical ones.

So I came up with another test.

<?
for ($bits=0; pow(2,$bits+1) > pow(2,$bits); $bits++);
echo $bits;
?>

In Windows XP, I got 1024 back. On Linux 2.6.13.2 and 2.4.28 (as
reported by phpinfo), all I get are timeout errors. So I decided to
make my script more efficient and hopefully faster:

<?
$x = 2;
$y = 1;
for ($bits=1; ($x*=2) > ($y*=2); $bits++);
echo $bits;
?>

On Windows XP, I again got 1024 back, whereas on Linux, I again got
timeout errors.

All in all, this leaves me a little confussed as to how to interpret
the results. Can integers in PHP be of an infinite precission when
running on Linux? If so, what's the point of the BCMath / GMP
functions on Linux?

Also, if this is the case, then how would I go about writting an
infinite precission integer arithmetic library that wasn't OS
dependant? I could make it work on Windows by using the bcmath
functions, since Windows builds of PHP include them by default, and use
PHP's normal integer stuff on Linux (Linux builds of PHP don't include
bcmath by default). But what about all the other platforms for which
PHP is available on? What about Mac OS X / OS/2 / etc? Would it be
safe to assume that if PHP's built-in integer functions didn't support
infinite precission that it'd include support for bcmath?

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация