Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Dealing with arbitrarily large power of 10 numbers

Dealing with arbitrarily large power of 10 numbers

Scheduled Pinned Locked Moved Algorithms
tutorialdatabasegame-dev
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 11683251
    wrote on last edited by
    #1

    If you are familiar with idle game or incremental games like Adventure Capitalist or Clicker Heroes you know that after a while you are dealing with very large numbers. If you are not aware basically you earn points in these games which you invest so that you can earn more points even quicker, rinse and repeat. I wrote a small game of this style myself but didn't leave the range that a double couldn't handle but it did get me thinking on how to deal with extraordinarily large numbers. Just to play around I figured a quick and easy way was to just keep a list with ints, each index represents a higher power and you can easily go higher if needed. index zero keeps numbers between 0-999 and index 1 then starts at 10^3. So index 5 would be ^7 etc. Except for numbers 0-999 if you want to increase the number I have a function where you specify what number 1-9 you want to add and to which power. If you add say 9^5 to 3^5 it sorts this out by becoming 2^5 and 1^6. So far I haven't accounted for subtraction yet in my little project but that should be fairly easy in a similar manner. I think this shouldn't be too computationally heavy unless if used to run addition between several large lists. Should I want to use this in a game I think it could also be easily adapted to use some approximation to reduce unnecessary work. For example if you have an entity in the game which adds 10^15 points per second and one that adds 10^3 and you show 5 decimals instead of doing all the background computations of adding those smaller numbers every second you just approximate how many seconds before they add as much so that it shows and then go from there if I made myself clear.

    public void AddNumber(int value, int power) // 10^3 = power1, 10^4 = power 2, 10^5 = power 3 etc.
    {
    if (power == 0)
    {
    NumberContainer[0] += value;
    while (NumberContainer[0] >= 1000)
    {
    NumberContainer[0] -= 1000;
    AddNumber(1, 1);
    }
    } else if(power < mPower)
    {
    NumberContainer[power] += value;
    while( NumberContainer[power] >= 10)
    {
    NumberContainer[power] -= 10;
    AddNumber(1, power + 1);
    }
    }else
    {
    //power too large
    }
    }

        public static NumberControl operator +(NumberControl c1, NumberContr
    
    Richard DeemingR L K M D 5 Replies Last reply
    0
    • M Member 11683251

      If you are familiar with idle game or incremental games like Adventure Capitalist or Clicker Heroes you know that after a while you are dealing with very large numbers. If you are not aware basically you earn points in these games which you invest so that you can earn more points even quicker, rinse and repeat. I wrote a small game of this style myself but didn't leave the range that a double couldn't handle but it did get me thinking on how to deal with extraordinarily large numbers. Just to play around I figured a quick and easy way was to just keep a list with ints, each index represents a higher power and you can easily go higher if needed. index zero keeps numbers between 0-999 and index 1 then starts at 10^3. So index 5 would be ^7 etc. Except for numbers 0-999 if you want to increase the number I have a function where you specify what number 1-9 you want to add and to which power. If you add say 9^5 to 3^5 it sorts this out by becoming 2^5 and 1^6. So far I haven't accounted for subtraction yet in my little project but that should be fairly easy in a similar manner. I think this shouldn't be too computationally heavy unless if used to run addition between several large lists. Should I want to use this in a game I think it could also be easily adapted to use some approximation to reduce unnecessary work. For example if you have an entity in the game which adds 10^15 points per second and one that adds 10^3 and you show 5 decimals instead of doing all the background computations of adding those smaller numbers every second you just approximate how many seconds before they add as much so that it shows and then go from there if I made myself clear.

      public void AddNumber(int value, int power) // 10^3 = power1, 10^4 = power 2, 10^5 = power 3 etc.
      {
      if (power == 0)
      {
      NumberContainer[0] += value;
      while (NumberContainer[0] >= 1000)
      {
      NumberContainer[0] -= 1000;
      AddNumber(1, 1);
      }
      } else if(power < mPower)
      {
      NumberContainer[power] += value;
      while( NumberContainer[power] >= 10)
      {
      NumberContainer[power] -= 10;
      AddNumber(1, power + 1);
      }
      }else
      {
      //power too large
      }
      }

          public static NumberControl operator +(NumberControl c1, NumberContr
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      This forum is for asking questions. What you've got there looks more like a Tip. :) Submit a new Article - CodeProject[^] You might also want to see how your code compares to the built-in BigInteger structure[^]. Eric Lippert did a series of blog posts[^] covering how you could implement integer operations without using the built-in number types, which is also worth a read.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • M Member 11683251

        If you are familiar with idle game or incremental games like Adventure Capitalist or Clicker Heroes you know that after a while you are dealing with very large numbers. If you are not aware basically you earn points in these games which you invest so that you can earn more points even quicker, rinse and repeat. I wrote a small game of this style myself but didn't leave the range that a double couldn't handle but it did get me thinking on how to deal with extraordinarily large numbers. Just to play around I figured a quick and easy way was to just keep a list with ints, each index represents a higher power and you can easily go higher if needed. index zero keeps numbers between 0-999 and index 1 then starts at 10^3. So index 5 would be ^7 etc. Except for numbers 0-999 if you want to increase the number I have a function where you specify what number 1-9 you want to add and to which power. If you add say 9^5 to 3^5 it sorts this out by becoming 2^5 and 1^6. So far I haven't accounted for subtraction yet in my little project but that should be fairly easy in a similar manner. I think this shouldn't be too computationally heavy unless if used to run addition between several large lists. Should I want to use this in a game I think it could also be easily adapted to use some approximation to reduce unnecessary work. For example if you have an entity in the game which adds 10^15 points per second and one that adds 10^3 and you show 5 decimals instead of doing all the background computations of adding those smaller numbers every second you just approximate how many seconds before they add as much so that it shows and then go from there if I made myself clear.

        public void AddNumber(int value, int power) // 10^3 = power1, 10^4 = power 2, 10^5 = power 3 etc.
        {
        if (power == 0)
        {
        NumberContainer[0] += value;
        while (NumberContainer[0] >= 1000)
        {
        NumberContainer[0] -= 1000;
        AddNumber(1, 1);
        }
        } else if(power < mPower)
        {
        NumberContainer[power] += value;
        while( NumberContainer[power] >= 10)
        {
        NumberContainer[power] -= 10;
        AddNumber(1, power + 1);
        }
        }else
        {
        //power too large
        }
        }

            public static NumberControl operator +(NumberControl c1, NumberContr
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Member 11683251 wrote:

        Thinking about this really got me wondering about how others would choose to solve it instead.

        My solution here[^]. Not the most efficient way :)

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          This forum is for asking questions. What you've got there looks more like a Tip. :) Submit a new Article - CodeProject[^] You might also want to see how your code compares to the built-in BigInteger structure[^]. Eric Lippert did a series of blog posts[^] covering how you could implement integer operations without using the built-in number types, which is also worth a read.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          M Offline
          M Offline
          Member 11683251
          wrote on last edited by
          #4

          Maybe but then it would need a bit more polish and I'm not sure it would fit, maybe a small blog post and carry with it some test results and discussion around it. When I started thinking about how to solve this I had a nagging suspicion that there already was something readily available but I couldn't figure out what search terms to use to find it so I turned it in to code instead. That blog post was interesting I admire how some people can go so in-depth in a subject.

          1 Reply Last reply
          0
          • M Member 11683251

            If you are familiar with idle game or incremental games like Adventure Capitalist or Clicker Heroes you know that after a while you are dealing with very large numbers. If you are not aware basically you earn points in these games which you invest so that you can earn more points even quicker, rinse and repeat. I wrote a small game of this style myself but didn't leave the range that a double couldn't handle but it did get me thinking on how to deal with extraordinarily large numbers. Just to play around I figured a quick and easy way was to just keep a list with ints, each index represents a higher power and you can easily go higher if needed. index zero keeps numbers between 0-999 and index 1 then starts at 10^3. So index 5 would be ^7 etc. Except for numbers 0-999 if you want to increase the number I have a function where you specify what number 1-9 you want to add and to which power. If you add say 9^5 to 3^5 it sorts this out by becoming 2^5 and 1^6. So far I haven't accounted for subtraction yet in my little project but that should be fairly easy in a similar manner. I think this shouldn't be too computationally heavy unless if used to run addition between several large lists. Should I want to use this in a game I think it could also be easily adapted to use some approximation to reduce unnecessary work. For example if you have an entity in the game which adds 10^15 points per second and one that adds 10^3 and you show 5 decimals instead of doing all the background computations of adding those smaller numbers every second you just approximate how many seconds before they add as much so that it shows and then go from there if I made myself clear.

            public void AddNumber(int value, int power) // 10^3 = power1, 10^4 = power 2, 10^5 = power 3 etc.
            {
            if (power == 0)
            {
            NumberContainer[0] += value;
            while (NumberContainer[0] >= 1000)
            {
            NumberContainer[0] -= 1000;
            AddNumber(1, 1);
            }
            } else if(power < mPower)
            {
            NumberContainer[power] += value;
            while( NumberContainer[power] >= 10)
            {
            NumberContainer[power] -= 10;
            AddNumber(1, power + 1);
            }
            }else
            {
            //power too large
            }
            }

                public static NumberControl operator +(NumberControl c1, NumberContr
            
            K Offline
            K Offline
            Kenneth Haugland
            wrote on last edited by
            #5

            I'd recommend using logarithms for it, and I think that all major games with leveling have the same logarithmic progression implemented in one form or another: Weber–Fechner law - Wikipedia, the free encyclopedia[^]

            1 Reply Last reply
            0
            • M Member 11683251

              If you are familiar with idle game or incremental games like Adventure Capitalist or Clicker Heroes you know that after a while you are dealing with very large numbers. If you are not aware basically you earn points in these games which you invest so that you can earn more points even quicker, rinse and repeat. I wrote a small game of this style myself but didn't leave the range that a double couldn't handle but it did get me thinking on how to deal with extraordinarily large numbers. Just to play around I figured a quick and easy way was to just keep a list with ints, each index represents a higher power and you can easily go higher if needed. index zero keeps numbers between 0-999 and index 1 then starts at 10^3. So index 5 would be ^7 etc. Except for numbers 0-999 if you want to increase the number I have a function where you specify what number 1-9 you want to add and to which power. If you add say 9^5 to 3^5 it sorts this out by becoming 2^5 and 1^6. So far I haven't accounted for subtraction yet in my little project but that should be fairly easy in a similar manner. I think this shouldn't be too computationally heavy unless if used to run addition between several large lists. Should I want to use this in a game I think it could also be easily adapted to use some approximation to reduce unnecessary work. For example if you have an entity in the game which adds 10^15 points per second and one that adds 10^3 and you show 5 decimals instead of doing all the background computations of adding those smaller numbers every second you just approximate how many seconds before they add as much so that it shows and then go from there if I made myself clear.

              public void AddNumber(int value, int power) // 10^3 = power1, 10^4 = power 2, 10^5 = power 3 etc.
              {
              if (power == 0)
              {
              NumberContainer[0] += value;
              while (NumberContainer[0] >= 1000)
              {
              NumberContainer[0] -= 1000;
              AddNumber(1, 1);
              }
              } else if(power < mPower)
              {
              NumberContainer[power] += value;
              while( NumberContainer[power] >= 10)
              {
              NumberContainer[power] -= 10;
              AddNumber(1, power + 1);
              }
              }else
              {
              //power too large
              }
              }

                  public static NumberControl operator +(NumberControl c1, NumberContr
              
              M Offline
              M Offline
              Mark io
              wrote on last edited by
              #6

              It may be a good idea to create a struct or class I'll call it LN which has a private vector with intergers VEC in the range of 0 to 9. The class has functions for Algebraic operations (+,-,*,/) to add two LN together. These functions must: - determine the sizes of both VEC and resize accordingly. - iterate though both VEC and execute the operation digit by digit. Since vectors are dynamically allocated, you have virtually no cap on how many digits the number has (accept for RAM). You'll have to replace all score related constant values in your code with LN's I'm not sure how your game mechanics are designed, but if you have many hundreds of entities which add some constant value per second, it might be a good idea to have a single variable X which is added to the game score S every second, instead of iterating through all entities and adding the constant value to S. That is, when an entity is created, it adds it value per second constant E to X and when the entity is destroyed it subtracts E from X. In every game step you then add X to S. This therefore only requires a single addition operation per game step and of course an addition operation when creating or destroying an entity.

              M 1 Reply Last reply
              0
              • M Mark io

                It may be a good idea to create a struct or class I'll call it LN which has a private vector with intergers VEC in the range of 0 to 9. The class has functions for Algebraic operations (+,-,*,/) to add two LN together. These functions must: - determine the sizes of both VEC and resize accordingly. - iterate though both VEC and execute the operation digit by digit. Since vectors are dynamically allocated, you have virtually no cap on how many digits the number has (accept for RAM). You'll have to replace all score related constant values in your code with LN's I'm not sure how your game mechanics are designed, but if you have many hundreds of entities which add some constant value per second, it might be a good idea to have a single variable X which is added to the game score S every second, instead of iterating through all entities and adding the constant value to S. That is, when an entity is created, it adds it value per second constant E to X and when the entity is destroyed it subtracts E from X. In every game step you then add X to S. This therefore only requires a single addition operation per game step and of course an addition operation when creating or destroying an entity.

                M Offline
                M Offline
                Member 11683251
                wrote on last edited by
                #7

                Concerning the situation that might arise if you have hundreds of entities I figured that I could just do some approximations. The most important parts would be those entities that's closest to the current largest number in the bank so to speak. If you have one entity that adds say 10^70 you it I could just go over everything below say 10^60 to calculate what to add per second for all those entities instead of doing 60 different calculations. So far I'm not working on creating a game but this kinda grew from curiosity but I did some testing and 100 different entities, one for each level up to 10^100 was just a few ms. No graphics or anything else but even if I kept increasing it would be a while before there would be a performance hit. For a mobile app it might me more important but my home computer doesn't have a high end cpu so I was quite happy with the current performance. There is a game called clicker heroes and the highest I ever came in that game was in the span of 10^130. There are so many of these games out there and they mainly target mobile with in game purchases which costs exorbitant amounts of money. While I see the allure of these games where the amount of in game points you accumulate while you are idle and can just pop in every once in a while to spend and increase the gathering rate I find it hard to comprehend how people can justify dropping 100$ > for in game credits. I'll see if I make an implementation of your suggestion of vectors if I got the time but it's always interesting to compare different solutions. If I do I'll make a comparison with one of the string based methods too.

                1 Reply Last reply
                0
                • M Member 11683251

                  If you are familiar with idle game or incremental games like Adventure Capitalist or Clicker Heroes you know that after a while you are dealing with very large numbers. If you are not aware basically you earn points in these games which you invest so that you can earn more points even quicker, rinse and repeat. I wrote a small game of this style myself but didn't leave the range that a double couldn't handle but it did get me thinking on how to deal with extraordinarily large numbers. Just to play around I figured a quick and easy way was to just keep a list with ints, each index represents a higher power and you can easily go higher if needed. index zero keeps numbers between 0-999 and index 1 then starts at 10^3. So index 5 would be ^7 etc. Except for numbers 0-999 if you want to increase the number I have a function where you specify what number 1-9 you want to add and to which power. If you add say 9^5 to 3^5 it sorts this out by becoming 2^5 and 1^6. So far I haven't accounted for subtraction yet in my little project but that should be fairly easy in a similar manner. I think this shouldn't be too computationally heavy unless if used to run addition between several large lists. Should I want to use this in a game I think it could also be easily adapted to use some approximation to reduce unnecessary work. For example if you have an entity in the game which adds 10^15 points per second and one that adds 10^3 and you show 5 decimals instead of doing all the background computations of adding those smaller numbers every second you just approximate how many seconds before they add as much so that it shows and then go from there if I made myself clear.

                  public void AddNumber(int value, int power) // 10^3 = power1, 10^4 = power 2, 10^5 = power 3 etc.
                  {
                  if (power == 0)
                  {
                  NumberContainer[0] += value;
                  while (NumberContainer[0] >= 1000)
                  {
                  NumberContainer[0] -= 1000;
                  AddNumber(1, 1);
                  }
                  } else if(power < mPower)
                  {
                  NumberContainer[power] += value;
                  while( NumberContainer[power] >= 10)
                  {
                  NumberContainer[power] -= 10;
                  AddNumber(1, power + 1);
                  }
                  }else
                  {
                  //power too large
                  }
                  }

                      public static NumberControl operator +(NumberControl c1, NumberContr
                  
                  D Offline
                  D Offline
                  Daniel Pfeffer
                  wrote on last edited by
                  #8

                  It sounds like you need arbitrary-precision numbers. With C-1999 or C++2011, you are guaranteed to have a 64-bit built-in type. If you limit yourself to "digits" containing 9 decimal digits (they fit into 32 bits), you can perform the four basic arithmetic operations with "digits" that are easily converted to decimal notation. The following article (printed in Dr. Dobb's Journal in August 1992; includes source code) should get you started: Multiple-Precision Arithmetic in C | Dr Dobb's[^]

                  If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups