Algorithm - Dealing with large power of 10 numbers

Created by
Tuesday, August 23, 2016

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.

 

// 10^3 = power1, 10^4 = power 2, 10^5 = power 3 etc.

public void AddNumber(int value, int power) 
{
    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, NumberControl c2)
{
    //set new maxpower to largest of the two
    int mMaxp = c1.mPower;
    if (c2.mPower > mMaxp) mMaxp = c2.mPower;

    NumberControl nc = new NumberControl(mMaxp);

    for(int i = 0;i < c1.NumberContainer.Count;i++)
    {
        nc.AddNumber(c1.NumberContainer[i], i);
    }

    for (int i = 0; i < c2.NumberContainer.Count; i++)
    {
        nc.AddNumber(c2.NumberContainer[i], i);
    }

    return nc;
}

Here is how I add numbers. Thinking about this really got me wondering about how others would choose to solve it instead.

Number container is just a list.

I'm planning on profile how well it handles a few different scenarios, for example if you have a list that goes to the power of 100 and have say 80 "generators" which adds a number at different power levels each second how will it will hold out compared to if I approximate those that are smaller than a certain range from the biggest number.

Reading the numbers is done by specifying how many decimals I want shown and then just walk backwards in the list and adding to a string so unless I want full precision it also works fairly well.

Tags:
algorithms
c#
3
Rating
1 2 3 4 5

Save

Saved

Report

Reported
Reasons
Cancel
0 comments