Math.Pow
seems to be not working correctly for big results. Probably that is because it uses double
for calculations (How is Math.Pow() implemented in .NET Framework?).
For example:
public static void Main()
{
Console.WriteLine((long)Math.Pow(17, 13));
Console.WriteLine(Pow(17, 13));
}
public static long Pow(int num, int pow)
{
long answer = 1;
for (int i = 0; i < pow; i++)
{
answer *= num;
}
return answer;
}
The result from the above code is:
9904578032905936
9904578032905937
Are there any built-in .NET alternatives for using power of a number without errors?