c# - 是否有用于 long 类型的函数 Math.Pow(A,n) ?

标签 c# math pow

我正在测试一个小的 C# 程序片段:

        short min_short = (short)(int)-Math.Pow(2,15);   
        short max_short = (short)(int)(Math.Pow(2, 15) - 1);
        Console.WriteLine("The min of short is:{0};\tThe max of short is:{1}", min_short, max_short);

        int min_int = (int)-Math.Pow(2, 31);
        int max_int = (int)(Math.Pow(2, 31) - 1);
        Console.WriteLine("The min of int is:{0};\tThe max of int is:{1}", min_int, max_int);

        uint min_uint = 0;
        uint max_uint = (uint)(Math.Pow(2, 32) - 1);
        Console.WriteLine("The min of uint is:{0};\tThe max of uint is:{1}", min_uint, max_uint);

        long min_long = (long)-Math.Pow(2, 63);
        long max_long = (long)(Math.Pow(2, 63) - 1);
        Console.WriteLine("The min of long is:{0};\tThe max of long is:{1}", min_long, max_long);

        ulong min_ulong = 0;
        ulong max_ulong = (ulong)(Math.Pow(2, 64) - 1);
        Console.WriteLine("The min of ulong is:{0};\tThe max of ulong is:{1}", min_ulong, max_ulong);

输出是:

The min of ushort is:0; The max of ushort is:65535
The min of short is:-32768;     The max of short is:32767
The min of int is:-2147483648;  The max of int is:2147483647
The min of uint is:0;   The max of uint is:4294967295
The min of long is:-9223372036854775808;The max of long is:-9223372036854775808
The min of ulong is:0;  The max of ulong is:0

我怀疑是Math.Pow()函数的错误导致的,返回的是double类型。

public static double Pow(
    double x,
    double y
)

所以,我的问题是: long 类型是否有类似的 Math 函数? 如何更正上面程序片段中的错误。 非常感谢!

最佳答案

您已达到 Math.Pow 限制。 您需要使用 System.Numerics.BigInteger.Pow

关于c# - 是否有用于 long 类型的函数 Math.Pow(A,n) ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39181444/

相关文章:

java - 将数字提高到分数幂java

c# - 为什么 JIT 编译器不删除未使用的变量?

c# - 通过 IOptions 配置 EF Core 上下文

java - 数学 - 映射数字java

security - 为 elgamal 寻找生成器

python - 如果 y 为负数,为什么 x**y%z 不能像 pow(x, y, z) 一样工作?

mysql - POW() 还是 POWER() 更好?

c# - 如何将结构从非托管 C++ 程序传递到 C# 程序?

c# - 防止 TestInitialize 为一个 TestMethod 方法运行

algorithm - 是否可以使用 32 位平方根的函数来帮助计算 64 位平方根?