creating a floating point power function in c

211 Views Asked by At

So here's the current implementation of my power function.

pow(x, n) int x, n;
{
    int r = 1;
    int i;

    for (i = 0; i < n; i++)
        r *= x;

    return (r);
}

The problem is how it only works for whole numbers & doesn't work with floats like pow(4, .5).

Of course, I've already tried changing everything to double & I know how a standard library function exists. Also I've seen Floating point exponentiation without power-function, but none of the solutions were working nor what I wanted.

Here is the version, where I used doubles.

double pow(x, n)
double x, n;
{
    double r = 1.;
    int i;

    for (i = 0; i < n; i++)
        r *= x;

    return (r);
}

It returns 1., when I use it as pow(4., .5).

0

There are 0 best solutions below