SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    426
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Dealing with larger numbers (esp. 32 bit)

    I've only recently realised that ints are capped in PHP and if you're on a 32 bit system they are quite limited. So, I have a few questions regarding numbers in PHP:

    1. If an int becomes too big, am I right in saying PHP converts it to a float? This is from the PHP manual:

    Code:
    $million = 1000000;
    $large_number =  50000000000000 * $million;
    var_dump($large_number);                     // float(5.0E+19)
    Can floats be bigger than ints because they use shorthand versions like 5.0E+19? If so, what is the biggest float possible on a 32 bit system?

    2. If you had a BIGINT field in MySQL and you try inserting a variable that PHP has converted to something like 5.0E+19, is it clever enough to convert it to the full number?

    3. Given the limitation on ints, is casting as (int) not a good idea unless you know the number will be less than PHP_INT_SIZE? Is it better to use regex to remove non-0-9 and treat it as a string?

    4. If you need to perform arithmetic on large numbers, do you need to rely on a third party lib?

    Thanks.

  2. #2
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    The problem with casting to int or even float is that you lose precision:

    PHP Code:
    $million 1000000;
    $large_number =  50000000000000 $million;
    var_dump($large_number);                     // float(5.0E+19)
    $large_number $large_number 1;
    var_dump($large_number);                     // float(5.0E+19)

    var_dump($large_number === $large_number+1); // bool(true) 
    Clearly, that last line should be false.

    Generally, you're better off storing large numbers as strings in PHP. I've never needed it so I'll let someone else answer the best way to perform arithmetic on them.

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    426
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomB View Post
    I've never needed it…
    Me neither but I like to get my head round these things. Thanks for your example. I would love to hear from someone regarding the MySQL question. It seems MySQL can handle bigger numbers better but PHP may cause you to lose data.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DrQuincy View Post
    1. If an int becomes too big, am I right in saying PHP converts it to a float? This is from the PHP manual:

    Code:
    $million = 1000000;
    $large_number =  50000000000000 * $million;
    var_dump($large_number);                     // float(5.0E+19)
    Can floats be bigger than ints because they use shorthand versions like 5.0E+19? If so, what is the biggest float possible on a 32 bit system?
    Yes, big numbers will be converted to floats but some precision can be lost.

    Quote Originally Posted by DrQuincy View Post
    2. If you had a BIGINT field in MySQL and you try inserting a variable that PHP has converted to something like 5.0E+19, is it clever enough to convert it to the full number?
    Yes, MySQL will convert 5.0E+19 to the full number. The problem is this number might not be the one you want if you lost precision earlier due to PHP conversion to float and its limited bit depth. Keep large numbers as strings and use the extensions logic_earth mentioned to perform arithmetics on them.

    BTW, you can use DECIMAL in MySQL to store even greater number than BIGINT allows.

  6. #6
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    426
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks, all very useful to know.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Usually once you get to numbers that big the loss of precision is irrelevant. The number will have sufficient digits on the front that are accurate that you will not need to care about what the remaining digits are. With 5.000000000000000e+19 does it really matter whether the number is 1000 bigger or smaller than the exact value? In most cases the answer would be NO.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    426
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, I guess in the real world it is rare you'd need the precision. I've been using PHP for over 10 years and never had the problem. I was just interested in how it worked.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •