SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Large Numbers

    hi everybody,

    I've been a long-time reader but not ever joined, but now I'd need your help if possible.

    I run an online game that deals with very large numbers, when the player gets to over approximately a trillion and they do something that requires that money to be used it sends 1 to the database and they lose all their money.

    I am fairly new to programming, and I am even worse at maths. I kind of know what is doing but Do not know how to prevent it. The number ends up something like 1.35e which the database takes as 1.

    please help I am begging somebody.

  2. #2
    Passionate Web Developer Egyptechno's Avatar
    Join Date
    Jan 2004
    Location
    Dubai
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the problem might be :

    1- it's the php limits . . and then you must try to depened on asp.net .. can carry more at same time

    2- the way you programmed your game .. then you need to reprogramme it

  3. #3
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can give it a shot with BCMath Arbitrary Precision Mathematics Functions that support "numbers of any size and precision, represented as strings".

    Regards

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need to choose appropriate type for the column where numbers are stored. See overview of numeric types. If the numbers are even larger than those mentioned there, store them as strings (varchar).

  5. #5
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just store the number as a string. Unless you actually plan doing calculations on it in SQL this should be fine.

  6. #6
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this column type is set to bigint said that is not the problem that will handle and number way bigger than what we are trying to handle,

    I do not have the bcmath in the PHP.ini on the server we are running 4.3.11 I didn't think it was needed but I did try it last night, however I may have implemented it wrong in any advice welcome.

    the only calculations I do on the SQL side and there are a lot is basically take the number and add the new number in the SQL query. so this would not be a solution.

    I did read somewhere about a 2 billion limit with 32-bit programming but I may be completely wrong.

  7. #7
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    er so use a string (sorry stereofrog for repeating you.. skipped the end of your post! ).

  8. #8
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Will Kelly
    er so use a string (sorry stereofrog for repeating you.. skipped the end of your post! ).

    will it work if alot queries are example money=money+$addmoney in mysql querie.

  9. #9
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dbevfat
    Or you can give it a shot with BCMath Arbitrary Precision Mathematics Functions that support "numbers of any size and precision, represented as strings".

    Regards
    in the server php.ini

    [bcmath]
    bcmath.scale = 0 ; number of decimal digits for all bcmath functions

  10. #10
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stevember
    will it work if alot queries are example money=money+$addmoney in mysql querie.
    ok sorry misread your last post then. can you move this into the php script? Then you would be fine.

  11. #11
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so php sees 1.35e okay as a figure.

    so if php adds 1000 to 1.3e+13 this way

    Code:
    $old=1.3e+13;
    $new=1000;
    
    $new=$old+$add;
    
    insert money=$new
    it will write into database 13000000001000 or 1.3e+13?

  12. #12
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try
    PHP Code:
    echo sprintf('%u'$new); 
    also, what's wrong with BCMath?

    Regards

  13. #13
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dbevfat
    try
    PHP Code:
    echo sprintf('%u'$new); 
    also, what's wrong with BCMath?

    Regards
    [bcmath]
    bcmath.scale = 0 ; number of decimal digits for all bcmath functions

    thats what in the server php.ini should i add something else?

  14. #14
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's ok. The thing with BCMath is that you don't do additions like $a = $b + $c, but use rather $a = bcadd($b, $c). All three variables are strings, so you don't get scientific representation of large integers anymore.

    It's all explained here - a link that I already posted.

    What does that sprintf show?


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
  •