I would like to know how to execute the Pow method with two BigInteger types in C#.

HighSky46 1 Reputation point
2022-05-11T17:33:53.543+00:00

First of all, I used a translator because I am not good at English, so I would appreciate it if you could understand that English can be weird.
It's a question.
I'm making a game out of Unity, and the type of money in the game is made out of Big Integer.
However, since only the value of the BigInteger's Power method is set to BigInteger and the component is set to Int32, OverFlow occurred during implementation.
Can you tell me how to solve this problem?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
{count} votes

1 answer

Sort by: Most helpful
  1. HighSky46 1 Reputation point
    2022-05-12T08:30:13.897+00:00

    I solved it like this.

    I don't know if this solution is efficient.

    public static BigInteger Pow(BigInteger Base, BigInteger exp)
    {
    BigInteger Result = BigInteger.One;
    while(exp > 0)
    {
    if(!exp.IsEven)
    {
    Result = BigInteger.Multiply(Result, Base);
    }
    Base *= Base;
    exp /= 2;
    }
    return Result;
    }


    0 comments No comments