Largest integer that can be handled by php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Largest integer that can be handled by php?

Post 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. :)
Last edited by agentcris on Wed Jun 25, 2008 4:08 pm, edited 4 times in total.
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Re: Largest integer that can be handled by php?

Post by agentcris »

Sorry,

For the number 2147483647 the alpha-numeric string is 2lkCB1

And after that for the number 2147483648 (many zeroes).......000000000000000000000000000002lkCB


Thanks :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Largest integer that can be handled by php?

Post by Benjamin »

–2147483648 to 2147483647 on a 32bit system.
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Re: Largest integer that can be handled by php?

Post 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.
dml
Forum Contributor
Posts: 133
Joined: Sat Jan 26, 2008 2:20 pm

Re: Largest integer that can be handled by php?

Post 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.
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Re: Largest integer that can be handled by php?

Post 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.
dml
Forum Contributor
Posts: 133
Joined: Sat Jan 26, 2008 2:20 pm

Re: Largest integer that can be handled by php?

Post 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)
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Re: Largest integer that can be handled by php?

Post 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. :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Largest integer that can be handled by php?

Post 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"
 
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Re: Largest integer that can be handled by php?

Post by agentcris »

Thanks a lot Weirdan! :) I have been breaking my head on this for the past one week :)
dmf wrote: 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)
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.
Post Reply