4

I see that the parameters could be both double enter image description here

but for codes like

   printf("%lf\n",pow(-32,0.2));
   return 0;

the output is
enter image description here
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.

Community
  • 1
  • 1
HgMs
  • 272
  • 2
  • 10
  • 1
    `pow(1.0, n)` is `1` for all `n`. `pow(-1.0, n)` would be `1` if `n` is an even integer and `-1` if `n` is an odd integer. What would you expect to be the result of `pow(-1.0, 0.5)`? **Hint:** The result would need to be a complex number, and complex numbers cannot be represented by a floating point. – Klas Lindbäck Nov 20 '14 at 09:51
  • How is `0.2` odd? After dividing it by 2 you are left with a remainder, which is the definition of 'odd', but by that reckoning *all* fractions are odd. A statement which can be "true", "false" (if so, please provide a counter-example), or "undefined" as the reasoning for 'even' and 'odd' numbers relies on natural numbers exclusively. – Jongware Nov 20 '14 at 09:52
  • @Klas Lindbäck I can throw an error for that case. It's not the point of my problem . – HgMs Nov 20 '14 at 10:03
  • i think 0.2 is considered odd in this instance in as much as it is an 'odd' root, i.e. the fifth root. 0.5 would be considered even, i.e. the 2nd (square) root. the point being odd roots don't result in a complex no. – bph Nov 20 '14 at 11:23

3 Answers3

4

Here is some math behind the problem:

We have pow(x,y) where x < 0 and y isn't integer.

First, let y = n+f where n is integer and 0<f<1. (Obtained by n = floor(y); f=y-n;)

Using the laws of exponents:

pow(x,y) = pow(x,n) * pow(x,f)

pow(x, f) (x<0) is real if y is a rational number a/b (written on smallest form, where a and b have no common prime divisors) with an odd denominator (that is, if b is odd).

In these cases, the result can be obtaineed as:

pow(-x,y) if a is even and -pow(-x,y) if a is odd

In all other cases with fractions, the result is not real.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
3

roots of negative numbers

You are looking at complex numbers, here is a primer with examples you can use implement your own function - look at this SO question; or you can switch to c++ and use <complex> to create your own library callable from c.

Alternative, based on the comments:

Would it not work if you do:

long double mypow(long double v, long double p)
{
    int sign=1;
    long double r;
    if (v<0)
    {
        sign = (-1);
        v *= sign;
    }
    r = pow(v, p);
    return (r*sign);
}
Community
  • 1
  • 1
slashmais
  • 7,069
  • 9
  • 54
  • 80
  • 2
    I think OP wants to support cases where the actual result is a real number. For the example in OP's question, `pow(-32,0.2)` should yield -2, so there is no need for complex numbers here. – barak manos Nov 20 '14 at 10:57
  • Your solution will give `mypow(-2, 2) == -4` which is wrong. – interjay Feb 05 '23 at 11:55
1

Raising a negative number to a non-integral power typically results in a number hat isn't expressible as a single float. In this specific case (-32 ** 0.2) it just so happens that the answer is -2 (-2 ** 5 is -32), but in the general case you would need to have a pow function that returns a complex number.

Vatine
  • 20,782
  • 4
  • 54
  • 70
  • What i hope is if it does have a float root then return this root, if only complex roots exist then throw an error or return null – HgMs Nov 21 '14 at 01:46
  • If this is really what you want, yoiu're probably better off with a `float nth_root(float n, int nth)`. – Vatine Nov 22 '14 at 13:16