简体   繁体   English

如何在C#中计算负数与小数幂?

[英]How do you compute negative numbers to fractional powers in C#?

I am trying to compute negative numbers to fractional power in C#. 我正在尝试在C#中计算负数来计算分数幂。

This what I tried to compute the power, 这就是我试图计算的能力,

Math.Pow(-0.5264, 1.11)--> Result NaN
Math.Pow(-0.5264, 2) --> 0.27709696
Math.Pow(0.5264, 1.11) --> Gives me a result. 

When the base is positive and the power is of fractional i do get a value. 当基数为正且幂为分数时,我确实获得了一个值。 But it is failing to give result when base as negative value? 但是,当基数为负值时,无法给出结果吗?

When I tried to find power of a negative base it is returning a complex number format a+bi in matlab. 当我试图找到负数基的幂时,它在Matlab中返回了复数格式a + bi。

I really have no clue how to achieve it in C#. 我真的不知道如何在C#中实现它。

Could someone help me to resolve the issue. 有人可以帮我解决问题。

Math.Pow is behaving correctly according to its specification. Math.Pow根据其规范正确运行。

If you want the complex value, and you don't have a complex math library, it's easy enough to compute it. 如果您想要复杂的值,并且没有复杂的数学库,则计算它很容易。

We know Euler's Identity: 我们知道欧拉的身份:

e ix = cos x + i sin x e ix = cos x + i sin x

and more specifically 更具体地说

e = -1 eiπ = -1

Therefore 因此

0.5264 e = -0.5264 0.5264ËIπ= -0.5264

Therefore 因此

(0.5264) 1.11 e 1.11 i π = (-0.5264) 1.11 (0.5264) 1.11 e 1.11 iπ=(-0.5264) 1.11

Now use Euler's identity again 现在再次使用欧拉的身份

(0.5264) 1.11 (cos(1.11 π) + i sin(1.11 π)) = (-0.5264) 1.11 (0.5264) 1.11 (cos(1.11π)+ i sin(1.11π))=(-0.5264) 1.11

And so the real part of the number that you're looking for is 因此,您要查找的数字的真正部分是

(0.5264) 1.11 cos(1.11 π) (0.5264) 1.11 cos(1.11π)

And the imaginary part is 而虚部是

(0.5264) 1.11 sin(1.11 π) (0.5264) 1.11 sin(1.11π)

And now you have reduced the problem to real-valued functions that you can compute with the ordinary math library. 现在,您已将问题简化为可以使用普通数学库计算的实值函数。

The moral of the story is: solve your math problems by doing mathematics . 故事的寓意是: 通过数学解决数学问题

You can calculate such expression using System.Numerics.Complex.Pow : 您可以使用System.Numerics.Complex.Pow计算此类表达式:

Complex.Pow(-0.5264, 1.11); //(-0,461524987676948, -0,166159219770948)
Complex.Pow(-0.5264, 2).Real; // 0.27709696
Complex.Pow(0.5264, 1.11).Real; // 0.49052441383186784

Usual Math.Pow returns double which is real number not complex. 通常Math.Pow返回double,这是实数,并不复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM