0
\$\begingroup\$

So, I want to create a new game. I have problems with how to store large numbers in the game and how to output them. I have written a small pseudo code that contains everything I need to play the game (Did I forget anything?).

# represent  our number
struct BigNumber {
    double base; # (or mantissa)
    double exponent; 
}


# create new number 
function NewBigNumber(double b, double exp) {
    return BigNumber{
        base = b;
        exponent = exp;
    }   
}


# now if we have to BigNumbers we want add operation (+)
# and other operations (-), (*), (/) 
# +
function adding(BigNumber n, BigNumber m){
    # if numbers have same exponent part
    if (n.exponent = m.exponent) {
        # adding base here
        double new_base = n.base + m.base;

        #create new BigNumber with new base
        return NewBigNumber(new_base, n.exponent);


    } else {
        #?????????
    }
}


# - 
function subtraction(BigNumber n, BigNumber m){

}

# *
function multiplication(BigNumber n, BigNumber m){

}

# /
function division(BigNumber n, BigNumber m){

}

As you can see, I will use Scientific notation

I have a problem with the addition operation (and other), what if the exponent part of numbers are different?

Next I have this code.

# If we have achievement systems
# we need function equals of two numbers
function equals(BigNumber n, BigNumber m){
    if (n.base = m.base && n.exponent = m.exponent){
        return True;
    } else {
         return false;
    }
}

And finally, I need the function to output numbers nicely.

suffices = [
    'K', 'M', 'B', 'T', 'Qa', 'Qi', 'Sx', 'Sp', 'Oc', 'No', 'Dc', 'Ud',
    'Dd', 'Td', 'Qad', 'Qid', 'Sxd', 'Spd', 'Od', 'Nd', 'V', 'Uv', 'Dv',
    'Tv', 'Qav', 'Qiv', 'Sxv', 'Spv', 'Ov', 'Nv', 'Tt'
];
# now we need "good view numbers" function
# for exapmle 
# 1000 - 1000
# 1000000 - 1Million
function(BigNumber n) {
    #????????
}
\$\endgroup\$
5
  • 2
    \$\begingroup\$ Have you considered to just use an existing big number library? \$\endgroup\$
    – Philipp
    Commented Feb 5, 2020 at 11:35
  • \$\begingroup\$ I have seen plenty of these game that just use doubles. And yes, the precision is lost, and you get beyond safe integers. By that time it is usually not a problem because the increments are very high too. If you do not want that, I'd side with Philipp, just use a third party big number library. What language is this? I do not know if there an open source, ready for production, big number library for your language ... but there are for others, worst case scenario you port one. \$\endgroup\$
    – Theraot
    Commented Feb 5, 2020 at 13:57
  • \$\begingroup\$ These libraries contain a lot of unnecessary things that I don't need (for example, a sine function for a large number). The code in them is very large, I would like someone to explain to me on my fingers what functions are missing in my code and how to add these numbers. \$\endgroup\$ Commented Feb 5, 2020 at 16:16
  • \$\begingroup\$ @12345654321 How are we supposed to know what functions your game needs when we don't know anything about your game? For example, how should we know that your game does not need a sine function for big numbers? \$\endgroup\$
    – Philipp
    Commented Feb 7, 2020 at 14:27
  • \$\begingroup\$ I write prototype my game. But now it use just simple double/ I want to replace double to scientific notation \$\endgroup\$ Commented Feb 7, 2020 at 16:37

1 Answer 1

1
\$\begingroup\$

This code appears to be JavaScript. Since ECMA-262, JavaScript has a native BigInt class.

  • It works with all the basic math operators +, -, *, /, % and ** (division will round to 0).
  • It also works with the comparison operators like ==, >, <= etc.
  • It can be expressed as a literal by using the suffix n. (const ONE_NONILLION = 1000000000000000000000000000000n).

Using this class will likely give you better performance with a much smaller code footprint and most importantly less bugs than inventing your own.

Unfortunately this class is not yet supported on Edge and Safari. If you want to support these browsers, then you will need a polyfill.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .