Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

C# solution for big numbers

Discussion in 'Scripting' started by startas, Jun 18, 2015.

  1. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    I'm looking for a solution to big numbers. My game will have big numbers, i.e., 10^99, and it will be float like numbers, with precision of 1 place after comma. What library or something else i could use to make such numbers?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,909
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    7,155
    Decimal has range up to 7.9e+28, or 79,228,162,514,264,337,593,543,950,335. And gets about 28 to 29 sig values (your link being the source of this info). OP needs up to 10^99 in range, far more than Decimal.

    Double has a range up to 1.07e+308, but a much short sig value range of about 15 or 16 digits. Which is more than the 10^99 needed by OP, but I don't know if it has the sig value range OP needs.

    OP, what sort of sig value range are you going to need???

    If 15 or so sig values is enough for you, double should do you fine.

    If you need perfect sig value representation across the full 10^99 range, you're going to need a 'BigNumber' library of some sort.

    .Net/Mono has a BigInteger in the newest versions of .Net/mono, but Unity doesn't have compatability with those newer version.

    There are 3rd party options as well:
    https://bignumber.codeplex.com/

    Do note, BigNumber types are usually very slow as they support arbitrarily large significant ranges (rather than packed into some constant bit depth like decimal, double, float, etc). This means arithmetic operations with them can be extraordinarily slow relative to what you'd expect from types like Decimal/double/float/etc.

    Only use them for values you truly need that level of precision with.

    What is your game doing that it'd need this level of precision though? Values with that level of significance are usually only needed in things like scientific calculations and the sort (and even then 100 digits of significance is seldom needed). In a game the player seldom would care about the difference between 10^99 and 10^99 + 1. The human mind can barely comprehend a number that size... you're literally 1 degree short of a googol. There's not even that many atoms in the observable universe (estimated to be around 10^80).
     
    Last edited: Jun 19, 2015
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    6,498
    I'm curious what the actual application of a 10^99 value is in a game.
     
    lordofduct likes this.
  5. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    Its going to be same idle genre game like adventure capitalist, where you buy buildings, upgrades and stuff and they give you income. I will not need full number, i will be using only first 9 digits of the number and 1-2 digits after the comma.
     
  6. Who-am-I

    Who-am-I

    Joined:
    Mar 29, 2014
    Posts:
    72
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    7,155
    By comma, I'm assuming you mean the separator for the fractional part of the value?
    I'm assuming you're not from the US or Britain... maybe part of Canada or somewhere not even predominantly English speaking?
    I'm asking, because in the states and UK, the comma is used for something else.

    If you DO mean the comma as in the decimal mark, then consider the following.

    Double has plenty of range on it (it goes to 10^308) for what you need. It also has the sig value range for the upper part of your number (15, you said you need 9). BUT Once your number climbs above approximately 10^13, you're not going to be able to store the digits for the 1 or 2 values after the comma/decimal mark, as they'll be outside of the sig value range.

    What will those 2 fractional digits be used for? What impact should they have on any arithmetic? Could you store that independent of the whole number value? You'd have a 'high' double, and a 'low' double/float (since you only need 2 digits sig in the low you could use a float).
     
  8. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    Yes, i'm from europe. I am using double right now, but i'm just not sure, if it will be enough. It works ok for now, just some problems, like if i have double value 999.999.999.999.999, then all is ok when i convert it to string, but if i add one 9 to total 9.999.999.999.999.999, and it gets rounded to 10.000.000.000.000.000 - 9.999 quadrillinions gets rounded up to 10 quadrillions, anyone know how to format integer part of double value to string ? I'm using right now double value; string val = Math.Floor (value).ToString ("F1");
     
unityunity