-4

I Want to Calculate x^y.

x is -0.726354 and y is 0.954.

So, I Using pow Function. but return infinity value.

how to calculate when x value is negative.

Please Answer to me.

Thanks.

user1320165
  • 331
  • 1
  • 3
  • 8

2 Answers2

6

Powers of negative numbers with non-integer exponent are generally complex numbers. The pow function returns a double which is an approximation of a real number. Therefore this cannot work.

If you are only interested in the absolute value, you can rewrite it as:

x^y = (-x)^y * (-1)^y

You can calculate the left term using pow because -x is positive. The right term has an absolute value of 1.

By the way, the powfunction does not return an "infinity" value but a value that indicates a domain error.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
  • First. Thanks for Corret to me. I will find about indicates a domain error. and Is this equation( (-1)^y ) Always return 1??? – user1320165 May 17 '16 at 07:53
  • @user1320165: The value of (-1)^y is not really 1 but a complex number with an absolute value of 1. In the complex plane, these are values on the unit circle. – Frank Puffer May 17 '16 at 08:05
  • According to your answer. 1 is an approximate value. I understand it. Thank you. – user1320165 May 17 '16 at 08:19
  • @user1320165: No, not an approximate value but an absolute value. An example: Both -1 and 1 have the same absolute value of 1. With complex numbers, you have not only these two cases but an infinite number of them. – Frank Puffer May 17 '16 at 08:26
  • Is you explained this?? if x < 0 case x^y = |x|^y * (-1)^y. I don't know about complex numbers.... so I can't understand it. – user1320165 May 17 '16 at 08:43
  • @user1320165: Without some basic knowledge of complex numbers, you won't be able to understand you original question. In fact, a variant of this question is what made Gauss come up with complex numbers in the 19th century. – Frank Puffer May 17 '16 at 08:55
  • I understand what you means. I will Learn Complex number. Thank you. – user1320165 May 17 '16 at 08:59
3

pow for floating point arguments is probably implemented as exp (y log x) (this is, by the way, remarkably clever since log and exp in particular can be evaluated very quickly on modern chipsets). Clearly that does not work for non-positive x, hence your output.

The standard specifies that pow is only defined for a positive x if x and y are floating point. http://en.cppreference.com/w/cpp/numeric/math/pow is a good reference.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thanks. I tried to exp(y ln x) but failed. because ln(-0.7) returned indicates a domain error. According to reference written in pow(-1, ±∞). ±∞ means any number?? even decimal?? – user1320165 May 17 '16 at 08:11
  • You do understand that -0.726354 raised to 0.954 does not have a real solution? (Rather a family of complex numbers)? – Bathsheba May 17 '16 at 08:19
  • Yup. That answer has a clever decomposition. But note carefully what the answerer means by the *absolute* value. – Bathsheba May 17 '16 at 08:31
  • @user1320165 it means any infinity: positive or negative – Cubbi May 27 '16 at 13:00