Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Optimize power function for fixed point numbers

Reply
Thread Tools

Optimize power function for fixed point numbers

 
 
suppamax
Guest
Posts: n/a
 
      03-12-2008
Hi everybody!

I'm writing a C program for a PIC18F microcontroller.

I need to calculate a power function, in which both base and exponent
are fixed point numbers (ex: 3.15^1.13).

Using pow() function is too expensive...

Is there another way to do that?


Thanks,



Max
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-12-2008
suppamax <(E-Mail Removed)> writes:
> I'm writing a C program for a PIC18F microcontroller.
>
> I need to calculate a power function, in which both base and exponent
> are fixed point numbers (ex: 3.15^1.13).
>
> Using pow() function is too expensive...
>
> Is there another way to do that?


How are these fixed point numbers represented? Does your compiler
have special support for them? Standard C's only arithmetic types are
integer and floating-point.

If the exponent were always an integer, you could do it with repeated
multiplication; you could save a few multiplications with judicious
use of squaring. But with a non-integral exponent, you're going to
have to do something very similar to what the pow() function does.

I don't think you've given us enough information to help you. We need
a better idea of how the operands are represented, what values they
can have, how precise you need the result to be, and so forth.

It's possible that comp.programming might be a better place to ask;
the solution you're looking for is likely to be an algorithm rather
that something specific to C.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
user923005
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 9:16*am, suppamax <(E-Mail Removed)> wrote:
> Hi everybody!
>
> I'm writing a C program for a PIC18F microcontroller.
>
> I need to calculate a power function, in which both base and exponent
> are fixed point numbers (ex: 3.15^1.13).
>
> Using pow() function is too expensive...
>
> Is there another way to do that?


Maybe this can help:
http://www.daimi.au.dk/~ivan/FastExpproject.pdf

You might look at the float implementation on the Cephes site:
http://www.moshier.net/#Cephes

 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 9:16*am, suppamax <(E-Mail Removed)> wrote:
> Hi everybody!
>
> I'm writing a C program for a PIC18F microcontroller.
>
> I need to calculate a power function, in which both base and exponent
> are fixed point numbers (ex: 3.15^1.13).
>
> Using pow() function is too expensive...
>
> Is there another way to do that?


Can you tell us why you need the power function?
There may be a work-around (e.g. using Horner's rule to evaluate
polynomials instead of pow()).
 
Reply With Quote
 
suppamax
Guest
Posts: n/a
 
      03-13-2008
> How are these fixed point numbers represented? Does your compiler
> have special support for them? Standard C's only arithmetic types are
> integer and floating-point.
>
> If the exponent were always an integer, you could do it with repeated
> multiplication; you could save a few multiplications with judicious
> use of squaring. But with a non-integral exponent, you're going to
> have to do something very similar to what the pow() function does.
>
> I don't think you've given us enough information to help you. We need
> a better idea of how the operands are represented, what values they
> can have, how precise you need the result to be, and so forth.
>
> It's possible that comp.programming might be a better place to ask;
> the solution you're looking for is likely to be an algorithm rather
> that something specific to C.
>
> --
> Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
> Nokia
> "We must do something. This is something. Therefore, we must do this."
> -- Antony Jay and Jonathan Lynn, "Yes Minister"



Numbers always have 2 digits, and are represented as integers.
For example, if the correct value is 3.15, it will be represented as
315.



Max
 
Reply With Quote
 
suppamax
Guest
Posts: n/a
 
      03-13-2008
> Can you tell us why you need the power function?
> There may be a work-around (e.g. using Horner's rule to evaluate
> polynomials instead of pow()).


The function I need to realize is something like

exp = 1.15;
result = 0;
while (...) {
[evaluate base: it will be, for example, 4.77]
result += pow(base, exp);
}



Max
 
Reply With Quote
 
suppamax
Guest
Posts: n/a
 
      03-13-2008

> That's a little confusing. If numbers always have 2 digits, 315 is not a
> number! Did you mean 3 digits?
>
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> "Usenet is a strange place" - dmr 29 July 1999



Sorry...

2 decimal digits.

so 3.15 -> 315




Max
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-13-2008
suppamax said:

<snip>

> Numbers always have 2 digits, and are represented as integers.
> For example, if the correct value is 3.15, it will be represented as
> 315.


That's a little confusing. If numbers always have 2 digits, 315 is not a
number! Did you mean 3 digits?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      03-13-2008
On Mar 13, 1:24*am, suppamax <(E-Mail Removed)> wrote:
> > That's a little confusing. If numbers always have 2 digits, 315 is not a
> > number! Did you mean 3 digits?

>
> > --
> > Richard Heathfield <http://www.cpax.org.uk>
> > Email: -http://www. +rjh@
> > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> > "Usenet is a strange place" - dmr 29 July 1999

>
> Sorry...
>
> 2 decimal digits.
>
> so 3.15 -> 315


What is the largest possible value in your system?
What is the smallest possible value in your system?
How much memory space do you have available?
 
Reply With Quote
 
Paul Hsieh
Guest
Posts: n/a
 
      03-13-2008
On Mar 12, 9:16 am, suppamax <(E-Mail Removed)> wrote:
> Hi everybody!
>
> I'm writing a C program for a PIC18F microcontroller.
>
> I need to calculate a power function, in which both base and exponent
> are fixed point numbers (ex: 3.15^1.13).
>
> Using pow() function is too expensive...
>
> Is there another way to do that?


It doesn't seem obvious to me. I guess you would want a break down
like:

two_pow_fromIM ( y * two_log_toIM ( x ) );

The idea would be that two_log_toIM and two_pow_fromIM could be
implemented as a scaling (normalize to the range 1 <= x < 2) then
either a post or pre-shift along with a table look up if the
resolution was small enough (and possibly perform interpolations). To
_fromIM and _toIM reflect the fact you might like to convert it to a
temporarily higher resolution intermediate value, or range corrected
for the particular input values.

I am not aware of any really good approximations to log() or 2exp()
except for taylor series or rational function approximations, which
will end up doing no better than using pow() directly. This table
based stuff would obviously compromise accuracy/resolution.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
fixed point representation and signed numbers blackpadme VHDL 4 09-07-2008 03:03 PM
Re: fixed point representation and signed numbers blackpadme VHDL 0 09-05-2008 04:06 PM
Re: fixed point representation and signed numbers KJ VHDL 0 09-05-2008 03:26 PM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 PM



Advertisments