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[1]);
   if (argc > 2)
        count = atoi(argv[2]);

   while (count--)
        answer *= number;

   printf("The answer was %f\n", answer);

   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[1]);
>   if (argc > 2)
>        count = atoi(argv[2]);

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

>   printf("The answer was %f\n", answer);

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

   while (count != 0) {
      if (count & 1)
         answer *= number;
      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  
 
 [ 6 post ] 

 Relevant Pages 

1. pow() with integer exponents

2. computing powers without the pow() function.

3. powers without pow()

4. Float number formatting (Forcing 2Digit exponent)

5. Math operators is C. Exponent operator?

6. help with exponent errors

7. exponent in double type versus float

8. print with %g: length of exponent

9. Exponents

10. Exponent

11. Help with exponents.

12. How are Exponents Expressed in C?

 

 
Powered by phpBB® Forum Software