>> I have been trying to use cosine and sine in my application.
>> I have tried to use math.h but this doesn't seem to be a palmfile.
>> Does anyone have a suggestion?

Here's some code that's worked fine for me. Hope it helps.

Regards,
Steve Mann

# # #

#define MINUS_ONE ( double ) -1.0
#define DEGS360_RADIANS 360.0 * DEGREES_TO_RADS
#define MAX_COS_ITERATIONS      10
#define MAX_SIN_ITERATIONS      10

/*****************************************************
__sin

Calculate the sin of a double radians angle.
Approximation is x - ( x**3 / 3! ) + ( x**5 / 5! ) - ( x**7 / 7! ) + ...
*****************************************************/

double __sin ( double x ) {

        double  result, numerator, denominator, term, sign, factorial, x1;
        int cnt = 0;

// scale input angle to proper range of values

        x1 = x;
        while ( x1 > DEGS360_RADIANS ) {
                x1 = x1 - DEGS360_RADIANS;
        }

// initialize everything

        result  = x1;
        numerator = x1;
        denominator = ONE;
        factorial = ONE;
        sign = MINUS_ONE;

        for ( cnt = 0; cnt < MAX_SIN_ITERATIONS; cnt++ ) {

// calculate the next term

                numerator = numerator * x1 * x1;
                denominator = denominator * ( factorial + ONE ) * (
factorial + TWO );
                term = numerator / denominator;
                result = result + ( sign * term ) ;

// prepare for next sequence thru loop

                sign = sign * MINUS_ONE;
                factorial       = factorial + TWO;
        }

        return result;
}


/*****************************************************
__cos

Calculate the cos of a double radians angle
Approximation is 1 - ( x**2 / 2! ) + ( x**4 / 4! ) - ( x**6 / 6! ) + .....
*****************************************************/

double __cos ( double x ) {

        double  result, numerator, denominator, term, sign, factorial, x1;
        int     cnt = 0;


// scale input angle to proper range of values

        x1                                      = x;
        while ( x1 > DEGS360_RADIANS ) {
                x1 = x1 - DEGS360_RADIANS;
        }

// initialize everything

        result = ONE;
        numerator = x1 * x1;
        denominator = TWO;
        factorial       = TWO;
        sign = MINUS_ONE;

        for ( cnt = 0; cnt < MAX_COS_ITERATIONS; cnt++ ) {

// calculate the next term

                term = numerator / denominator;
                result = result + ( sign * term ) ;

// prepare for next sequence thru loop

                numerator = numerator * x1 * x1;
                denominator= denominator * ( factorial + ONE ) * (
factorial + TWO );
                factorial       = factorial + TWO;
                sign = -sign;
        }

        return result;
}

-------------------------------------------
Creative Digital Publishing Inc.
1317 Palm Street, San Luis Obispo, CA 93401-3117
-------------------------------------------
805.788.0138            805.593.3811 (fax)
[EMAIL PROTECTED]       http://www.cdpubs.com


Reply via email to