IT干货网

.net之C# Math.Pow() 坏了

langtianya 2025年05月04日 编程设计 115 0

不,这(据我所知)不涉及整数除法或浮点舍入问题。

我的确切代码是:

    static void Main(string[] args) 
    { 
        double power = (double)1.0 / (double)7.0; 
        double expBase = -128.0; 
        System.Console.WriteLine("sanity check: expected: -128 ^ 0.142857142857143 = -2.    actual: " + expBase + " ^ " + power + " = " + Math.Pow(expBase, power)); 
        System.Console.ReadLine(); 
    } 

输出是:
sanity check: expected: -128 ^ 0.142857142857143 = -2. actual: -128 ^ 0.14285 7142857143 = NaN
此代码的目标框架是(根据解决方案属性).NET Framework 4.0 Client Profile。

奇怪的是,我在网络上的任何地方都没有发现任何提及。我在这里吃疯狂的药丸吗!?

请您参考如下方法:

似乎完全符合规定;来自 the Math.Pow() remarks sectionPow(x,y) ;

Parameters
x < 0 but not NegativeInfinity; y is not an integer, NegativeInfinity, or PositiveInfinity.

Result
NaN


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!