I see that the parameters could be both double
but for codes like
printf("%lf\n",pow(-32,0.2));
return 0;
the output is
obviously wrong
I have read https://stackoverflow.com/questions/22583740/how-to-implement-a-pow-function-in-python-for-float-as-well-as-negative-numbers but it can't make it for float negative number.
so any suggestion on how to implement a pow function which support negative float number?
I have an idea that for pow(double x,double n), if x
is negative then do pow(-x,n)
, and check n
before return the value.
Take pow(-32,0.2)
for example, first I go for pow(32,0.2)
and get 2
, then I check n
, 0.2 in this case. And I found that 0.2, aka 1/5 , is an "odd" number so i negate the answer then return -2
is this a possible way to solve the problem?
[edit]
I know the answer could be not a single one but i hope i can get an float number as an answer if it does have one.