.net c#计算负数的幂

Calculate power of a negative number
2021-02-23
  •  译文(汉语)
  •  原文(英语)

我是C#的新手,由于Math.Pow(x,y)返回一个负数的NaN(读:不是负幂),所以我想知道如何更有效地计算结果.我现在正在写的内容是一个基本的for循环,但是我想知道它是否可以以更短的方式实现(也许使用Linq)?

for(int i=0;i<power;i++)
       {
           result = result * num;
       }
速聊1:
您是说它返回NaN类似Math.pow(-2, 5)?
速聊2:
使用Math.Pow(Math.Abs​​(x,y)),然后应用正确的符号.Math.Pow拒绝否定x,因为如果y不是整数,则"应用正确的符号"是不可能的.
速聊3:
您发布的循环将以负数返回1(假设结果初始化为1),该负数不如NaN正确.如果您只想编写一种更有效的幂算法,可以使用分治法,请参阅链接(这是一个C程序,但翻译起来C#应该很简单).
速聊4:
我本来要给雷蒙德的评论作为答案,但他击败了我.校正后,应为Math.Pow(Math.Abs​​(x),y​​).然后,如果y为奇数,则以相反的方式否定结果,结果为正.并且y必须是整数值.
速聊5:
如果功率为负,则返回1,不是这种情况.无论如何,代码是相当不错的,我想我可以翻译一下.
解决过程1

Math.Pow(x,y)返回NaN负数.

这是因为没有为负基定义任意幂:

http://en.wikipedia.org/wiki/指数化

当基数b为正实数时,可以通过指数函数为所有实数甚至复数指数n定义b到n

现在,如果幂是整数(如您的情况),则定义了负数的幂以得到真实的结果,但Pow不会为您计算出来.您应该做的是:

  • 假设你想知道 Pow(-2, 5)
  • 给出Pow问题Pow(2, 5).
  • 看一下指数;如果它是偶数,那么您将得到正确的答案.如果是奇数,则答案是否定的.

因此,在这种情况下,Pow(2, 5)返回32.指数5为奇数,因此将其设为-32,就可以了.

速聊1:
我不明白 Pow(-2, 5)不会返回-32- dotnetfiddle.net/ks8ObK
速聊2:
可能导致该问题的一件事是,如果您的指数不是整数,但是非常接近整数,以至于您不会注意到它不是整数.double a = -2, b = 4.999999999999999; Console.WriteLine("{0}^{1} = {2}", a, b, Math.Pow(a, b));打印"-2 ^ 5 = NaN".
解决过程2

在Pow(双倍基数,双倍幂)中,当基数<0 并且分数(例如:power = 0.5)时,将生成NaN,因为它可以在复杂域而不是在实际域中计算.所以可能是你需要检查的基础动力POW使用前功能.在这种情况下,您应该返回所需的值作为未定义的值(而不是NaN).

波纹管函数执行此操作,并返回0而不是NaN:

// pow with desired Nan
public double Pow(double x,double y){
    double MyNaN = 0; // or any desired value for Nan
    double result = MyNaN; 

    if (Math.Floor (y) != y) { // if y is fractional number
        if (x < 0)  // if x is negative number
            return result;
    }

    result = Math.Pow (x, y);
    return result;

}

I relatively new to C# and since Math.Pow(x,y) returns NaN for a negative number(read: not negative power), I want to know how to calculate result more efficiently. What I am doing write now is a basic for loop, but I want to know if it can be implemented in shorter way (using Linq maybe)?

for(int i=0;i<power;i++)
       {
           result = result * num;
       }
Talk1:
You're saying that it returns NaN for something like Math.pow(-2, 5)?
Talk2:
Use Math.Pow(Math.Abs(x, y)) and then apply the correct sign. Math.Pow rejects negative x because "applying the correct sign" is impossible if y is not an integer.
Talk3:
The loop you posted will return 1 (assuming result is initialized at 1) with a negative number, which is not as correct as NaN. If you are just interested in writing a more efficient power algorithm, you can use a divide and conquer method, see this link (it's a C program, but translating to C# should be simple).
Talk4:
I was going to give Raymond's comment as the answer, but he beat me to it. Once correction, it should be Math.Pow(Math.Abs(x), y). Then if y is odd you negate the result other wise the result is positive. And y must be an integer value.
Talk5:
It will return 1 if power is negative which not the case. Anyhow the code is pretty good, I guess I can translate it..
Solutions1

Math.Pow(x,y) returns NaN for a negative number.

That's because arbitrary exponentiation is not defined for a negative base:

http://en.wikipedia.org/wiki/Exponentiation

when the base b is a positive real number, b to the n can be defined for all real and even complex exponents n via the exponential function

Now, exponentiation of negative numbers to get a real result is defined if the power is an integer, as in your case, but Pow will not calculate it for you. What you should do is:

  • Suppose you want to know Pow(-2, 5)
  • Give Pow the problem Pow(2, 5) instead.
  • Look at the exponent; if it is an even number then you've got the right answer. If it is an odd number, then make the answer negative.

So in this case, Pow(2, 5) returns 32. The exponent 5 is odd, so make it -32, and you're done.

Talk1:
I don't understand. Pow(-2, 5) does return -32 - dotnetfiddle.net/ks8ObK
Talk2:
One thing that might cause that is if your exponent isn't an integer, but is so close to an integer that you don't notice that it's not. double a = -2, b = 4.999999999999999; Console.WriteLine("{0}^{1} = {2}", a, b, Math.Pow(a, b)); prints "-2^5 = NaN".
Solutions2

in Pow(double base, double power), when base < 0 and power is fractional ( ex: power=0.5) , then NaN will be generated, because it is calculable in complex domain not in real domain. so may be you need to check the base and power of pow function before using them. in this case you should return your desired value as undefined value (instead of NaN).

the bellow function performs this operation, and returns 0 instead of NaN:

// pow with desired Nan
public double Pow(double x,double y){
    double MyNaN = 0; // or any desired value for Nan
    double result = MyNaN; 

    if (Math.Floor (y) != y) { // if y is fractional number
        if (x < 0)  // if x is negative number
            return result;
    }

    result = Math.Pow (x, y);
    return result;

}
转载于:https://stackoverflow.com/questions/23712834/calculate-power-of-a-negative-number

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入