Solved

pow(10,x) implementation!

Posted on 2003-11-03
6
1,846 Views
Last Modified: 2008-03-04
hi,

  could anybody tell me, how can i implement pow(10,x) function, where x can be double? ofcourse, i want to minimize use of library functions to speedup the process?
0
Comment
Question by:Vikram_B
  • 2
  • 2
  • 2
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9676305
here is the glibc implementation of pow() (which I would not strain to comprehend)

# define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
     (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres;            \
                   if ((sizeof (__real__ (Val1)) > sizeof (double)            \
                      || sizeof (__real__ (Val2)) > sizeof (double))    \
                     && __builtin_classify_type (__real__ (Val1)            \
                                           + __real__ (Val2))     \
                        == 8)                                    \
                   {                                          \
                     if (sizeof (__real__ (Val1)) == sizeof (Val1)      \
                         && sizeof (__real__ (Val2)) == sizeof (Val2))  \
                       __tgmres = __tgml(Fct) (Val1, Val2);            \
                     else                                          \
                       __tgmres = __tgml(Cfct) (Val1, Val2);            \
                   }                                          \
                   else if (sizeof (__real__ (Val1)) == sizeof (double)   \
                        || sizeof (__real__ (Val2)) == sizeof(double) \
                        || (__builtin_classify_type (__real__ (Val1)) \
                            != 8)                              \
                        || (__builtin_classify_type (__real__ (Val2)) \
                            != 8))                              \
                   {                                          \
                     if (sizeof (__real__ (Val1)) == sizeof (Val1)      \
                         && sizeof (__real__ (Val2)) == sizeof (Val2))  \
                       __tgmres = Fct (Val1, Val2);                  \
                     else                                          \
                       __tgmres = Cfct (Val1, Val2);                  \
                   }                                          \
                   else                                          \
                   {                                          \
                     if (sizeof (__real__ (Val1)) == sizeof (Val1)      \
                         && sizeof (__real__ (Val2)) == sizeof (Val2))  \
                       __tgmres = Fct##f (Val1, Val2);                  \
                     else                                          \
                       __tgmres = Cfct##f (Val1, Val2);                  \
                   }                                          \
                   __tgmres; }))


if you feel lost then there is always the simpler one

result = 1;
for ( i = 0;  i < x; i++ )
     result = result  * 10;

;o)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9676353
a little study shows that pow is implemented as a macro and not as a function... if you use pow(), your execution time would not be adversely affected... moreover, library implementation of pow() is bound to be more efficient what I or you would write... So I would say --- use pow()
0
 

Author Comment

by:Vikram_B
ID: 9676964
life is not always straignt-forward. i'm implementing the pow function on a dsp chip (TI'x 64xx) which doesn't have floating pt unit. thus, using libraty pow function is consuming lotta cpu cycles.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 5

Expert Comment

by:SethHoyt
ID: 9677024
You might consider changing bases to either 2 or e by precomputing the logarithm of 10 for the desired base. Many systems have an optimized implementation for exponentiation base e, and base 2 can be done quickly for powers in binary form using the square and multiply method.

To transform the base to 2, use the identity:

10^x = (2^lg(10))^x = 2^(lg(10)*x)

where lg() is the logarithm base 2. Typically, you will need to compute log(10)/log(2) using a different base, but this is done only once and stored for later use, so efficiency is not important here. The transformation is similar for base e, but unless a built-in function exists for exponentiation base e, you should use base 2 for this.

So the idea is to compute r = log(10)/log(2) one time, either globally or offline, then find 2^(r*x).


-Seth
0
 
LVL 5

Accepted Solution

by:
SethHoyt earned 50 total points
ID: 9677182
You can find the square and multiply method here:

http://www.math.ucalgary.ca/~kjell/papers/hardware/gordon98survey.pdf

-Seth
0
 

Author Comment

by:Vikram_B
ID: 9678117
thanx SethHoyt,

 that should work :)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert image to byte array 8 209
C simple state machine for client server handshake 4 339
Computer slow / BSOD 10 64
Memory going from 12gb to 64gb or 96gb. worth it? 15 203
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question