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.
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.
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 pow
function does not return an "infinity" value but a value that indicates a domain error.
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.