exponents without POW
Author Message exponents without POW

I'm hoping someone might be able to help me with the following
problem.  I need a way of raising a given number (Total) to a given
exponent (Power) without using the POW function.  I've tried the
following loop with success:

while(Count < Power)
{
Total = Total * Total;
Count = Count + 1;
}

where Total is type float and Count and Power are integers.  Am I
missing something obvious?  Any help is appreciated.

Multiply by the original value, not by the current total.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
do not ask me C questions via email; post them instead

Thu, 21 Jun 2001 03:00:00 GMT  exponents without POW
I'm hoping someone might be able to help me with the following
problem.  I need a way of raising a given number (Total) to a given
exponent (Power) without using the POW function.  I've tried the
following loop with success:

while(Count < Power)
{
Total = Total * Total;
Count = Count + 1;
}

where Total is type float and Count and Power are integers.  Am I
missing something obvious?  Any help is appreciated.

Fri, 22 Jun 2001 03:00:00 GMT  exponents without POW

| I'm hoping someone might be able to help me with the following
| problem.  I need a way of raising a given number (Total) to a given
| exponent (Power) without using the POW function.  I've tried the
| following loop with success:
|
| while(Count < Power)
|             {
|                Total = Total * Total;
|                Count = Count + 1;
|             }
|
| where Total is type float and Count and Power are integers.  Am I
| missing something obvious?  Any help is appreciated.

double
myPower( double total, double power )
{

return exp( power * log( total ) );

Quote:
}

you'll have to be careful about negative arguments, btw.

igorR
--
"Ma belle demoiselle, repondit Candide, quand on est amoureux, jaloux
et fouette par l'Inquisition, on ne se connait plus"
Voltaire, "Candide"

Fri, 22 Jun 2001 03:00:00 GMT  exponents without POW

Quote:
> I need a way of raising a given number (Total) to a given
>   exponent (Power) without using the POW function.  I've tried the
>   following loop with success:

>   while(Count < Power)
>           {
>              Total = Total * Total;
>              Count = Count + 1;
>           }

>   where Total is type float and Count and Power are integers.  Am I
>   missing something obvious?  Any help is appreciated.

>Multiply by the original value, not by the current total.

If you don't want to use pow(), you can get this job done faster and slightly
more accurately by specializing the double to int power loop which is contained
in most implementations of pow().

Tim Prince

Fri, 22 Jun 2001 03:00:00 GMT  exponents without POW
: I'm hoping someone might be able to help me with the following
: problem.  I need a way of raising a given number (Total) to a given
: exponent (Power) without using the POW function.  I've tried the
: following loop with success:

: while(Count < Power)
:             {
:                Total = Total * Total;
:                Count = Count + 1;
:             }

: where Total is type float and Count and Power are integers.  Am I
: missing something obvious?  Any help is appreciated.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int count = 0;
double number = 0, answer = 1;

if (argc > 1)
number = atof(argv);
if (argc > 2)
count = atoi(argv);

while (count--)

return 0;

Quote:
}

and then think about boundary conditions; overflow, underflow, negative
arguments, and so on.  And some error checking as well.

Will

Fri, 22 Jun 2001 03:00:00 GMT  exponents without POW

...

Quote:
>#include <stdio.h>
>#include <stdlib.h>

>int main(int argc, char *argv[])
>{
>   int count = 0;
>   double number = 0, answer = 1;

>   if (argc > 1)
>        number = atof(argv);
>   if (argc > 2)
>        count = atoi(argv);

>   while (count--)
>        answer *= number;

Here's an alternative that could be faster and possibly more accurate
for larger values of count

while (count != 0) {
if (count & 1)
number *= number;
count >>= 1;
}

Quote:
>   return 0;
>}

>and then think about boundary conditions; overflow, underflow, negative
>arguments, and so on.  And some error checking as well.

--
-----------------------------------------

-----------------------------------------

Fri, 22 Jun 2001 03:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages
 10. Exponent