Search Unity

C# solution for big numbers

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

Thread Status:
Not open for further replies.
  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:
    8,297
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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
    Bunny83 likes this.
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
    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:
    73
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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");
     
  9. Arduan

    Arduan

    Joined:
    Mar 3, 2022
    Posts:
    1
    If you want to work with incredibly large numbers look here...

    https://github.com/Arduan77/Miki-Calculator/tree/Update20220303

    I am not a professional programmer i write for myself, sometimes, so sorry for unprofessional use of c# but the program works. I will be grateful for any advice and correction. I use this calculator to generate 32-character passwords from numbers that are around 58 digits long. Since the program adds numbers in the string format, you can perform calculations on numbers with the maximum length of the string variable. The program uses long lists for the calculation, so it is possible to calculate on larger numbers, possibly 18x the maximum capacity of the list.
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Bunny83, NixTown, LilGames and 2 others like this.
  11. FuturPlanet

    FuturPlanet

    Joined:
    Sep 27, 2021
    Posts:
    1
  12. TT_B01

    TT_B01

    Joined:
    Jun 4, 2021
    Posts:
    1
    Hello everyone, How can I divide 2 variables of type BigInteger between each other and return a float?
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Please don't necropost. Please create your own thread to ask your own questions.

    Thanks.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.