Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Optimize power function for fixed point numbers (http://www.velocityreviews.com/forums/t598158-optimize-power-function-for-fixed-point-numbers.html)

suppamax 03-12-2008 04:16 PM

Optimize power function for fixed point numbers
 
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

Keith Thompson 03-12-2008 05:01 PM

Re: Optimize power function for fixed point numbers
 
suppamax <max.giacometti@gmail.com> 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) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

user923005 03-12-2008 07:00 PM

Re: Optimize power function for fixed point numbers
 
On Mar 12, 9:16*am, suppamax <max.giacome...@gmail.com> 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


user923005 03-12-2008 07:30 PM

Re: Optimize power function for fixed point numbers
 
On Mar 12, 9:16*am, suppamax <max.giacome...@gmail.com> 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()).

suppamax 03-13-2008 08:16 AM

Re: Optimize power function for fixed point numbers
 
> 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) <ks...@mib.org>
> 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

suppamax 03-13-2008 08:22 AM

Re: Optimize power function for fixed point numbers
 
> 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

suppamax 03-13-2008 08:24 AM

Re: Optimize power function for fixed point numbers
 

> 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

Richard Heathfield 03-13-2008 08:25 AM

Re: Optimize power function for fixed point numbers
 
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

user923005 03-13-2008 07:31 PM

Re: Optimize power function for fixed point numbers
 
On Mar 13, 1:24*am, suppamax <max.giacome...@gmail.com> 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?

Paul Hsieh 03-13-2008 10:14 PM

Re: Optimize power function for fixed point numbers
 
On Mar 12, 9:16 am, suppamax <max.giacome...@gmail.com> 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/


All times are GMT. The time now is 07:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.