Page 1 of 1
Largest integer that can be handled by php?
Posted: Thu Jun 12, 2008 11:47 am
by agentcris
Hello all,
First up, I am sorry if I am re-posting something. I have searched this forum for nearly an hour now and I haven't found any related topics to the one I am posting now.
Pretty much like the existing URL shortening websites. The reason why I am thinking of coding my own is that I am not ready to trust the presently existing services because of fear of spam.
So now coming to the actual problem,
As you can see, this simple piece of code loops till the actual number becomes zero and keep adding a character from the array validChars. The problem I am encountering is, after the number 2147483647, it doesn't work and it completely gets screwed up. The equivalent alpha-numeric string for that number is 1BCkl2. But for the next number, 2147483647, the equivalent alpha-numeric is BCkl200000000000000000... it goes on.
I am thinking that this is most prolly due to an error in my scripting in PHP. Experienced coders here please help me out as I am really curious about what could possibly be a reason for this script not working beyond that number.
Thanks and Looking forward hear your replies,
A PHP Noobe.

Re: Largest integer that can be handled by php?
Posted: Thu Jun 12, 2008 11:50 am
by agentcris
Sorry,
For the number 2147483647 the alpha-numeric string is 2lkCB1
And after that for the number 2147483648 (many zeroes).......000000000000000000000000000002lkCB
Thanks

Re: Largest integer that can be handled by php?
Posted: Thu Jun 12, 2008 12:02 pm
by Benjamin
–2147483648 to 2147483647 on a 32bit system.
Re: Largest integer that can be handled by php?
Posted: Thu Jun 12, 2008 12:12 pm
by agentcris
Oh, Thanks for that.
Is there a way by which I can improve on this algorithm or something? Maybe support more URLs? Purely of academic interests.
Re: Largest integer that can be handled by php?
Posted: Thu Jun 12, 2008 12:40 pm
by dml
If I understand correctly, you're converting the number base from 10 to 52, using the standard arithmetic method of taking modulo then dividing until you're left with zero. What's probably happening is that 2147483648 is bigger than PHP_INT_MAX, so PHP actually makes it a floating point number, and when you divide it you're left with fractional parts, and those fractional parts make the ==0 test come out false, which leaves you with the big string of zeros at the end.
Working with floats will cause loss of precision and will make any kind of equals comparison invalid. If you want to do integer operations beyond the normal integer bounds, you should use a large integer library. I haven't used such a library myself, but I'm sure someone on the forum can recommend one.
Re: Largest integer that can be handled by php?
Posted: Thu Jun 12, 2008 12:46 pm
by agentcris
Yea, I am converting a base 10 number to base 62. (10 numeric digits + 26 upper-case digits + 26 lower-case digits)
Thanks for that idea dml

, I have heard about large integer libraries, but strange it never struck me. I will research about this possibility.
But if there is anyone who has worked with large integer libraries, please do let me know.
Re: Largest integer that can be handled by php?
Posted: Thu Jun 12, 2008 12:56 pm
by dml
You know, it's interesting that it works in Javascript, since Javascript seems to also convert to floating point when a number is too big to fit in an int. (
http://www.devx.com/webdev/Article/17215/1954)
Re: Largest integer that can be handled by php?
Posted: Thu Jun 12, 2008 1:28 pm
by agentcris
Yeah, That is the most interesting fact and that is the main factor that prompted me to post here

Experienced coders, Please give your thoughts on this strange, but interesting phenomenon.

Re: Largest integer that can be handled by php?
Posted: Thu Jun 12, 2008 2:04 pm
by Weirdan
The interesting fact is that it's modulo operator that breaks the results for numbers over PHP_INT_MAX but close to it. This is because it's defined
on integer operands only, so PHP implicitly converts floating point operand $Num to int, and it's treated as
negative number. Using fmod() instead of % solves this problem (again, for numbers close to PHP_INT_MAX). I wouldn't trust this implementation for very large numbers though, even with fmod(). Using library like bcmath is certainly preferred here.
Code: Select all
<?php
function convertNum2URL($N){
//initialization
$validChars="0123456789"."abcdefghijklmnopqrstuvwxyz"."ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$validCharsLen=strlen($validChars);
$URL="";
$Num=ceil(abs($N));
$inter="";
//URL generation
while(true){
$inter=fmod($Num,$validCharsLen);
$URLdig=$validChars[$inter];
$URL=$URL.$URLdig;
$Num=($Num-$inter)/$validCharsLen;
if($Num==0) break;
}
return $URL;
}
var_dump(convertNum2URL(PHP_INT_MAX+1)); // string(6) "2BCkl2"
var_dump(convertNum2URL(PHP_INT_MAX*PHP_INT_MAX)); // string(11) "0cknAqozFu5"
Re: Largest integer that can be handled by php?
Posted: Thu Jun 12, 2008 2:41 pm
by agentcris
Thanks a lot Weirdan!

I have been breaking my head on this for the past one week
But as dmf noted, its interesting that Javascript modulo operator works fine in this case. A point to note.
Weirdan wrote:
I wouldn't trust this implementation for very large numbers though, even with fmod(). Using library like bcmath is certainly preferred here.
As of now this serves my need. But I will certainly look into the bcmath library on an academic note.