2

I'm trying to write a power function in c without calling pow().

double power (double X, int Y)
{
int i;
double value = 1;
 for (i = 0; i < Y; i++)
 value *= X;
 return value;
}

My question is, is there any reason you can see that this function would not work properly with any given test values? I am trying to cover all input possibilities.

-Thanks

asked Oct 21, 2013 at 3:45
2
  • It will be quite slow and quite imprecise. Commented Oct 21, 2013 at 3:47
  • I changed value to a double, thanks Commented Oct 21, 2013 at 4:18

2 Answers 2

13

This function is inadequate for several reasons:

  • It's buggy. Notice that value is declared as an int rather than a double, which means that if you try to compute power(1.5, 1), you'll get back 1 rather than 1.5. In fact, it will be wrong on almost all inputs.

  • It doesn't handle negative exponents. Try computing power(2, -1). The correct answer is 0.5, but your function (after fixing the int bug noted above) will return 1 rather than 0.5. You can fix this pretty easily (you could, for example, compute power(2, 1) and then take the reciprocal), but it's troublesome as currently written.

  • It's slow. Most exponentiation, when the power is an integer, is computed using an algorithm called exponentiation by squaring , which is considerably faster than your code. Exponentiation by squaring will do Θ(log Y) multiplications, compared to the Θ(Y) multiplications your code makes. It will take exponentially longer for your function to complete.

  • It doesn't handle fractional exponents. Try computing power(1.5, 1.5). You'll get the wrong answer because the exponent is an int, not a double. Correcting this isn't easy; search around on Stack Overflow for other questions on how to implement this properly.

  • It reinvents the wheel. At a fundamental level, you should ask yourself why you're rewriting a function provided to you by the language's math libraries. This can introduce bugs or inefficiencies into the program (see the earlier bullet points) and at the end of the day you haven't increased the functionality.

Hope this helps!

answered Oct 21, 2013 at 3:52

6 Comments

and it doesn't handle overflows!!
@nitish712- I think that's a consequence of the int bug I mentioned in the first part. Once that's accounted for, I think the overflow issue will resolve.
I looked at the exponentiation by squaring, is there a way to do this from "scratch" without calling any functions? Thanks, by the way, for the input
@user28374- Absolutely! There's a very simple recursive formulation of the algorithm that's described on the Wikipedia page.
So where it calls exp-by-squaring how would I type that portion as code, as well as where it shows x squared? Thanks
|
0

Your function should be like this, it will run slower than pow() which runs in O(log Y):

#include<math.h>
#define ABS(x) ((x<0)?(-x):(x))
double power (double X, int Y)
{
 int i;
 double value = 1;
 if (Y == 0)
 {
 return 1.0;
 }
 else if (X == 0)
 {
 return 0.0;
 }
 for (i = 0; i < ABS(Y); i++)
 {
 value *= X;
 if (value == NAN
 || value == INFINITY
 || (X > 0 && (value*X) < value)
 || (X < 0 && (value*X) > value))
 {
 return NAN;
 }
 }
 if (Y < 0) return (1.0/value);
 else return value;
}
answered Oct 21, 2013 at 7:14

1 Comment

That is not how you check for floating point overflow. I don't know what the standard says about it, but my system (OS X, x86-64, Apple LLVM GCC 4.2.1) returns infinity. It also always detects "overflow" if X is less than 1.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.