我有一个ulong
需要提高到高功率的值,但Math.Pow
没有成功产生正确的输出。
我的 C# 代码:
ulong a = 9123456789;
ulong b = (ulong)Math.Pow(a, 9999);
Console.WriteLine(b);
屏幕的输出为 0。如何执行此计算并获得正确的结果?
是的,可以使用专门的BigInteger
类来计算该数字。ushort
尝试使用宽度为 16 位的 来执行此操作是没有希望的。
using System;
using System.Collections;
using System.Numerics;
public class Test
{
public static void Main(string[] args)
{
BigInteger a = 9123456789;
BigInteger b = BigInteger.Pow(a, 9999);
//Output this number if you're feeling lucky.
Console.WriteLine(b);
}
}
输出
43056151396124937171542222696555900626431494491043369510338912076154406943108
..
8614367506747618876018379290109
顺便说一句,您需要 330,837 位来存储结果。
如果您要处理非常大的数字,请查找BigInteger 。