>I'm looking for a math library for a simple apllication.
>How do you make sin and cos (calculator application?).
Here are two simple routines with adjustable accuracy. They are taken
from Advanced Palm Programming (http://www.cdpubs.com/paka/).
Regards,
Steve Mann
# # #
#define TT_ACCURACY 1.0e-6 // adjust as needed
/*****************************************************
* FUNCTION: _sin
*
* DESCRIPTION: Calculate the sine of a double radians number
* using the power series sin x = x -
* ( x**3 / 3! ) + ( x**5 / 5! ) - ( x**7 / 7! ) + ...
*
* RETURNED: The result.
*****************************************************/
double _sin // ( out ) the result
(
double x // ( in ) the number to get the sine of
)
{
double result, prevResult, numerator, denominator, term, sign,
factorial, diff;
// Scale input angle to proper value between -2 PI and + 2 PI.\
// The power series is not as accurate outside that range.
if (( x > TWO_PI ) || ( x < - TWO_PI ))
x = ScaleNearZero ( x, TWO_PI );
// initialize everything
result = x;
numerator = x;
denominator = 1.0;
factorial = -1.0;
sign = 1.0;
// Add the next term to the power series until within allowable limits.
do
{
// prepare for next term calculation
factorial = factorial + 2.0;
prevResult = result;
sign = sign * - 1.0;
// calculate the next term
numerator = numerator * x * x;
denominator = denominator * ( factorial + 1.0 ) *
( factorial + 2.0 );
term = numerator / denominator;
result = result + ( sign * term );
// Prep for termination check. Stop when the library's accuracy is reached.
diff = _abs ( _abs ( prevResult ) - _abs ( result));
}
while ( diff > TT_ACCURACY );
return result;
}
/*****************************************************
* FUNCTION: _cos
*
* DESCRIPTION: Calculate the cosine of a double radians number
* using the power series cos x = 1 -
* ( x**2 / 2! ) + ( x**4 / 4! ) - ( x**6 / 6! ) + .....
*
* RETURNED: The result.
*****************************************************/
double _cos // ( out ) the result
(
double x // ( in ) The number to get the cosine of
)
{
double result, prevResult, diff, numerator, denominator,
term, sign, factorial;
UInt8 cnt = 0;
// Scale input angle to proper value between -2 PI and + 2 PI.\
// The power series is not as accurate outside that range.
if (( x > TWO_PI ) || ( x < - TWO_PI ))
x = ScaleNearZero ( x, TWO_PI );
// initialize everything
result = 1.0;
prevResult = 1.0;
numerator = x * x;
denominator = 2.0;
factorial = 0.0;
sign = 1.0;
do
{
// Prep for the next calculation
prevResult = result;
sign = -sign;
factorial = factorial + 2.0;
// Calculate the next term
term = numerator / denominator;
result = result + ( sign * term );
// Prepare for next sequence thru loop
numerator = numerator * x * x;
denominator = denominator * ( factorial + 1.0 ) *
( factorial + 2.0 );
// Check for loop termination. Stop when the library's accuracy is reached.
diff = _abs ( _abs ( prevResult ) - _abs ( result));
}
while ( diff > TT_ACCURACY );
return result;
}
/*****************************************************
* FUNCTION: ScaleNearZero
*
* DESCRIPTION: Scale a radians number that is outside the
* range - scale <--> scale to something within that range.
* Assume scale is either PI or 2PI
*
* RETURNED: The scaled result.
*****************************************************/
static double ScaleNearZero // ( out ) the scaled result
(
double x, // ( in ) the number to scale
double scale // ( in ) the range to scale it to
)
{
double adjCntD, result;
UInt32 adjCnt;
// Get the number of scale increments in the input value.
adjCntD = scale;
adjCntD = x / adjCntD;
adjCntD = _abs ( adjCntD );
adjCnt = ( UInt32 ) adjCntD;
// Adjust increment count for scale factor == PI
if ( scale == PI )
adjCnt = adjCnt * 2;
// Adjust the input value accordingly.
if ( x > 0.0 )
result = x - ( scale * ( double ) adjCnt );
else
result = x + ( scale * ( double ) adjCnt );
return ( result );
}
--
-------------------------------------------
Creative Digital Publishing Inc.
1315 Palm Street, San Luis Obispo, CA 93401-3117
-------------------------------------------
805.784.9461 805.784.9462 (fax)
[EMAIL PROTECTED] http://www.cdpubs.com
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/