How do you do *integer* exponentiation in C#?

前端 未结 11 1078
迷失自我
迷失自我 2020-11-30 08:26

The built-in Math.Pow() function in .NET raises a double base to a double exponent and returns a double result.

W

11条回答
  •  隐瞒了意图╮
    2020-11-30 08:48

    I cast the result into int, like this:

    double exp = 3.0;
    int result = (int)Math.Pow(2.0, exp);
    

    In this case, there are no rounding errors because base and exponent are integer. The result will be integer too.

提交回复
热议问题