C functions to calculate sines & cosines

C functions to calculate sines & cosines

Hi
I'm using the Tasking C196 compiler, i'm looking for C function to calculate
sines and cosines.

--
Maurizio Montanari
Via Argine Sinistro Canale molino 32
San Bartolo (RA)
Tel. 039-544-497811

http://www.geocities.com/SiliconValley/Heights/8094/

C functions to calculate sines & cosines

you'll probably use some sort of series expansion.  If noone helps you
go to comp.lang.asm.x86 and try to find the address of terje matheson (he's a
regular);  he's an x86 assembler guru but he ought to be able to stear you to a
suitable set of algorithms.

David

> Hi
> I'm using the Tasking C196 compiler, i'm looking for C function to calculate
> sines and cosines.

> --
> Maurizio Montanari
> Via Argine Sinistro Canale molino 32
> San Bartolo (RA)
> Tel. 039-544-497811

> http://www.geocities.com/SiliconValley/Heights/8094/

--
---------------------------------------------------------------

http://www.geocities.com/Area51/Station/5196/ttc.html      (tao te ching)
http://www.geocities.com/Area51/Station/5196/treasure.html (computer page)

C functions to calculate sines & cosines

> Hi
> I'm using the Tasking C196 compiler, i'm looking for C function
> to calculate sines and cosines.

If you do a lookup table for the first quadrant only (0-90), then
the values for the other three quadrants may be developed from
that with simple arithmetic.  The coarseness of the table and the
need for any interpolation depends on your requirements. To get
a cos-value, add 90 to it and treat it as a sin. (ISTR that is
the right way round).  Your upfront software should correctly
manage angles greater than 360, that may also be +ve or -ve.

--
Tony Williams.

C functions to calculate sines & cosines

> > Hi
> > I'm using the Tasking C196 compiler, i'm looking for C function
> > to calculate sines and cosines.
>  If you do a lookup table for the first quadrant only (0-90), then

You can trim the size of your table a bit by noting that sin X = X
for small X, etc.

Quote:>  the values for the other three quadrants may be developed from
>  that with simple arithmetic.  The coarseness of the table and the
>  need for any interpolation depends on your requirements. To get
>  a cos-value, add 90 to it and treat it as a sin. (ISTR that is
>  the right way round).  Your upfront software should correctly
>  manage angles greater than 360, that may also be +ve or -ve.

Depending on precision, etc., you might also investigate using
a CORDIC implementation.  It's reasonably fast (usually much
faster than a Taylor series expansion, etc.) and isn't too
bad for memory requirements, etc.  (e.g. a 32 bit fixed point
representation only needs a few hundred bytes of const)

--don

[return address is munged...]

C functions to calculate sines & cosines

>> Hi
>> I'm using the Tasking C196 compiler, i'm looking for C function
>> to calculate sines and cosines.

> If you do a lookup table for the first quadrant only (0-90), then
> the values for the other three quadrants may be developed from
> that with simple arithmetic.  The coarseness of the table and the
> need for any interpolation depends on your requirements. To get
> a cos-value, add 90 to it and treat it as a sin. (ISTR that is
> the right way round).  Your upfront software should correctly
> manage angles greater than 360, that may also be +ve or -ve.

One option that I have used before is to split the first 90 degrees into
several straight line segments (I used 5). Then use a y = m*x + c type
calculation for each segment.

--
Phil Harrison

C functions to calculate sines & cosines

Refer to a mathematics book that has infinite series expansions.
For example:
sinx = x(1-x**2/pi**2)(1-x**2/4pi**2)(1-x**2/9pi**2)...
cosx = (1-4x**2/pi**2)(1-4x**2/9pi**2)(1-4x**2/25pi**2)...
OR
Use a lookup table.
...DT

> Hi
> I'm using the Tasking C196 compiler, i'm looking for C function to
calculate
> sines and cosines.

> --
> Maurizio Montanari
> Via Argine Sinistro Canale molino 32
> San Bartolo (RA)
> Tel. 039-544-497811

> http://www.geocities.com/SiliconValley/Heights/8094/

C functions to calculate sines & cosines

> One option that I have used before is to split the first 90 degrees into
> several straight line segments (I used 5). Then use a y = m*x + c type
> calculation for each segment.

That brings back the days of piecewise linearisation, usually for
thermocouples. I remember doing an analogue triangle-sine converter
in just three straight lines, but it _was_ a little jaggy up at the
tips of the sines.  Your 5 is much more respectable, maybe 2 lines
to go from 0-70 deg, and the last 3 concentrated in the last 20 deg.

--
Tony Williams.

C functions to calculate sines & cosines

>> One option that I have used before is to split the first 90 degrees into
>> several straight line segments (I used 5). Then use a y = m*x + c type
>> calculation for each segment.

> That brings back the days of piecewise linearisation, usually for
> thermocouples. I remember doing an analogue triangle-sine converter
> in just three straight lines, but it _was_ a little jaggy up at the
> tips of the sines.  Your 5 is much more respectable, maybe 2 lines
> to go from 0-70 deg, and the last 3 concentrated in the last 20 deg.

This is the function I used to give an 8-bit approximation to
256*sin(angle). Note that the angle must be stored as a 16 bit int (or
promoted to a 16 bit in in the calculations). I seem to remember that it
was accurate to within 3% or so. The angle must also be within 0 to 90.

if (angle <= 35)
s = (angle*17 + 2 )/4;

else if (angle <= 60)
s = angle*3 + 45;

else if (angle <= 78)
s = ((angle*3 + 1)/2) + 134;

else if (angle <= 87)
s = ((angle + 1)/2) + 212;

else
s = 255;
--
Phil Harrison

C functions to calculate sines & cosines

> Hi
> I'm using the Tasking C196 compiler, i'm looking for C function to calculate
> sines and cosines.

There is a nice summary of common methods at
http://www.interstice.com/~sdattalo/technical/theory/sinewave.html.

--
Steve Strobel                           WWW: http://www.link-comm.com
Link Communications, Inc.               Phone: (406) 245-5002 ext 102

C functions to calculate sines & cosines

>>  Hi I'm using the Tasking C196 compiler, i'm looking for C
>> function to calculate sines and cosines.

Steve> There is a nice summary of common methods at
Steve> http://www.interstice.com/~sdattalo/technical/theory/sinewave.html.

This is a very nice URL.  I might add that there is a good book
called "Computer Approximations" that has several descriptions
of series to approximate different functions.  It includes a
five term polynomial that is a chebysev fit to the function
'sin (pi/2* X)'.

Here is a chebysev polynomial approximation.
The error is less than +/- .000 000 5
sin (pi/2 * X) = c1*X + c3*X^3 + c5*X^5 + c7*X^7 + c9*X^9

where
c1 = 1.57079631847
c3 = -.64596371106
c5 =  .07968967928
c7 = -.00467376557
c9 =  .00015148419

This function needs 9 multiplications and four additions.
The accuracy is better than a 16 bit scaled integer can
handle.  If high accuracy is required in a 16 bit system,
multi-word calculation can be used to get a sin value that
is accurate to the machine constant. Ie, the error is in
the fact that you are using 16 bits to represent a number.

I have seen some 'professional' libraries that do not
approximate sine as well as this polynomial does.

The Pade rational given in the URL requires 9 multiplies,
six additions and a divide.  It does not give a maximum
error value.

Bill Pringlmeir.

--
Danger - End of world ahead.

C functions to calculate sines & cosines

>...
>     Steve> There is a nice summary of common methods at
>     Steve>
> http://www.interstice.com/~sdattalo/technical/theory/sinewave.html.

> This is a very nice URL.  I might add that there is a good book
> called "Computer Approximations" that has several descriptions
> of series to approximate different functions.  It includes a
> five term polynomial that is a chebysev fit to the function
> 'sin (pi/2* X)'...

I'd like to second his observation about this fine book.  We have
used it since the '70s as the basis for fixed-point fraction
arithmetic libraries in our embedded systems products.  You can
get excellent performance and accuracy this way without the overhead
of floating point on chips that don't have hardware FP.

The book is a little hard to find, though.  A complete reference (from
the last one we bought, in 1983) is:

Computer Approximations, by John F. Hart et. al.
Robert E. Krieger Publishing Co.
Malabar, FL
ISBN 0-88275-642-7

Original ed. 1968, John Wiley & Sons, Inc.
Reprinted in 1978 w/ corrections by Krieger

Enjoy,
Elizabeth

--
===============================================
Elizabeth D. Rather  (US & Canada) 800-55-FORTH
FORTH Inc.                      +1 310-372-8493
111 N. Sepulveda Blvd.     Fax: +1 310-318-7130
Manhattan Beach, CA 90266
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
===============================================

C functions to calculate sines & cosines

>Here is a chebysev polynomial approximation.
>The error is less than +/- .000 000 5
>sin (pi/2 * X) = c1*X + c3*X^3 + c5*X^5 + c7*X^7 + c9*X^9

You should mention that this is only valid for -1<= X <=1, use the
periodicity of sin to extend it.  These polynomial fits get immediately
terrible outside of their range of validity.

Another reference for these types of rational approximations is
Abramowitz and Stegun, "Handbook of Mathematical Functions."   One of
the most useful books I know.

--
Tom Shield
Aerospace Engineering and Mechanics
University of Minnesota

http://www.aem.umn.edu/people/faculty/shield/