Forums

pow() and log() replacements?

Started by cfrancois_55116 February 19, 2006
Has anyone coded spece efficient replacements for pow() and log()?  The 
BasicX library versions of these consume so much stack that they are 
basically useless.  Has anyone coded a Taylor's expansion for these 
that they's like to share?
	
cfrancois_55116 wrote:

> Has anyone coded spece efficient replacements for
pow() and log()?  The
> BasicX library versions of these consume so much stack that they are
> basically useless.  Has anyone coded a Taylor's expansion for these
> that they's like to share?

How did you come to the conclusion that these functions are wasteful of  
RAM? They are but for exact measurements see part 4 of my series of 
articles on the Internals of BasicX. In section 4.4.6 I show how to 
replace the code for Sin() using an abbreviated Taylor series.

The following webpage gives some algorithms for calculating log() and 
log10() - http://www.dattalo.com/technical/theory/logs.html. You should 
be able to do some space enhancements for pwr() along the same lines as 
documented in section 4.4 of the article and by reusing some of the code 
from that section - see also section 4.3.5 to see what needs rewriting.

Mike
http://home.austin.rr.com/perks/micros/Articles/
	
I used bxdism. log() uses 63 bytes of stack while pow() uses an 
astonishing 100, probably because it calls log() in a Taylor's
expansion. http://www.efunda.com/math/taylor_series/logarithmic.cfm
has several Taylor's series for these but I was hoping someone already
went to the trouble of coding.  Maybe I'm missing something, but to 
use 63 and 100 bytes, they must be coded with almost complete 
disregard for stack usage.  At 100 bytes, it pretty much rules out 
using pow() in a multi-task app.

BTW, I've been looking through your articles- good stuff.

                    STACK USAGE ANALYSIS
                    ===================
Length Parameters Returns Local Vars Stack Usage Total Stack Name of 
Routine
------ ---------- ------- ---------- ----------- ----------- ---------
------ 
251       4         4       26           39         63     
sys~mathlibrary.sys~log
100       8         4        8           34        100     
sys~mathlibrary.sys~pow
	--- In basicx@basi..., Mike Perks <basicx@...> wrote:
>
> cfrancois_55116 wrote:
> 
> > Has anyone coded spece efficient replacements for pow() and log
()?  The
> > BasicX library versions of these consume so
much stack that they 
are
> > basically useless.  Has anyone coded a
Taylor's expansion for 
these
> > that they's like to share?
> 
> How did you come to the conclusion that these functions are 
wasteful of  
> RAM? They are but for exact measurements see part
4 of my series of 
> articles on the Internals of BasicX. In section 4.4.6 I show how to 
> replace the code for Sin() using an abbreviated Taylor series.
> 
> The following webpage gives some algorithms for calculating log() 
and 
> log10() -
http://www.dattalo.com/technical/theory/logs.html. You 
should 
> be able to do some space enhancements for pwr()
along the same 
lines as 
> documented in section 4.4 of the article and by
reusing some of the 
code 
> from that section - see also section 4.3.5 to see
what needs 
rewriting.
> 
> Mike
> http://home.austin.rr.com/perks/micros/Articles/
>
	
cfrancois_55116 wrote:

> I used bxdism. log() uses 63 bytes of stack while
pow() uses an
> astonishing 100, probably because it calls log() in a Taylor's
> expansion. http://www.efunda.com/math/taylor_series/logarithmic.cfm
> has several Taylor's series for these but I was hoping someone already
> went to the trouble of coding.  Maybe I'm missing something, but to
> use 63 and 100 bytes, they must be coded with almost complete
> disregard for stack usage.  At 100 bytes, it pretty much rules out
> using pow() in a multi-task app.
>
> BTW, I've been looking through your articles- good stuff.

This is quite straightforward coding with just a little optimization for 
the stack. I have posted some code on my Experiments page ( 
http://home.austin.rr.com/perks/micros/Experiments/ ) that implements 
the Log function (which I call Ln). I only used the first six terms from 
the Taylor expansion and achieved an accuracy better than 0.0001%.

Here is the extract from bxDism:

Length ... Local Vars Stack Usage Total Stack Name of Routine
------     ---------- ----------- ----------- ---------------
  276          14           26         26     log.ln
  251          26           39         63     sys~mathlibrary.sys~log

The function is slightly longer (could probably be improved) but it only 
uses 26 bytes on the stack versus 63. In addition a welcome improvement 
is that it seems to be more than 3 times faster than the BasicX code. 
This performance improvement is surprising but nonetheless very welcome. 
Try the code for yourself and verify both the results and that my 
implementation is correct.

Mike
BasicX, ZBasic and AVR expert
http://home.austin.rr.com/perks/micros/