Re: Best processors for trig?



Tauno Voipio wrote:
The MacLaurin series for a log (calculated for log(1 + x))
needs 5000 terms for 4 digits. Pick your favourite formula
collection and have a look.

5000 terms?

I recently needed an exp/log pair for a fixed-point DSP, and came up
with the equivalent of the following code (recited from memory), which
needs one loop iteration per mantissa bit.
// as many as you need, enlarge tables accordingly
#define BITS 10

// 2**-1 .. 2**-10
const double tab1[] = {
0.5,
0.25,
0.125,
0.0625,
0.03125,
0.015625,
0.0078125,
0.00390625,
0.001953125,
0.0009765625,
};

// base ** tab1[i]
const double tab2[] = {
1.4142135623731,
1.18920711500272,
1.09050773266526,
1.04427378242741,
1.02189714865412,
1.0108892860517,
1.0054299011128,
1.0027112750502,
1.00135471989211,
1.00067713069307,
};

// compute 2**a, for a \in [0,1).
double mypow(double a)
{
int i;
double result = 1.0;
for (i = 0; i < BITS; ++i) {
if (a >= tab1[i]) {
result *= tab2[i];
a -= tab1[i];
}
}
return result;
}

// compute ld(a) for a \in [0.5, 1]
double mylog(double a)
{
int i;
double result = 0.0;
for (i = 0; i < BITS; ++i) {
// a inverse-of-tab2 table could be handy here
if (a <= 1.0 / tab2[i]) {
a *= tab2[i];
result -= tab1[i];
}
}
return result;
}
The good thing is that this works for all bases and scales (e.g. also
for converting decibels into levels and back), and only uses operations
easily available in a fixed-point DSP.

Maybe it can be done faster, but at least I'm faster than using my
compiler's runtime library, and I'm nowhere near 5000 terms.


Stefan

.



Relevant Pages

  • Re: Funky function
    ... >> It makes it possible to call this member function on a const object ... >> object may or may not be const). ... int Data; ... in the next line you try to call a function foo on MyData. ...
    (comp.lang.cpp)
  • Re: Difficulty with nested template classes (newbie)
    ... the huge post but I am including first the errors, then the template ... Reference initialized with 'const double', ... matrix& lookup (int row, int col); ... // iterator operator++ ...
    (comp.lang.cpp)
  • Re: accessor member functions and const
    ... > reference the returned value v. ... int& val ... const int& valconst ... so one cannot modify the object via the reference. ...
    (alt.comp.lang.learn.c-cpp)
  • [2.6.6-BK] NTFS 2.1.11 Really final cleanups.
    ... throughout the NTFS code to ntfschar since uchar_t is already defined by POSIX ... +BOOL find_attr(const ATTR_TYPES type, const ntfschar *name, const u32 name_len, ... register int rc; ...
    (Linux-Kernel)
  • Re: On-the-fly Le Chatelier
    ... LeChatelier(int P, int V, int T, int N) ... void setP(int P) ... change(const change& c) ... > virtual int GetA() const ...
    (comp.lang.cpp)