📄 tinytrig.c
字号:
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: _tan * * DESCRIPTION: Calculate the tangent of a double radians number. * * RETURNED: The result. If the cosine is zero, the * value returned is either very small or very large.*****************************************************/double _tan // ( out ) the result( double x // ( in ) the number to get the tangent of){ double sinx, cosx, result;// Scale input angle to proper value between -2 PI and + 2 PI.// The sin and cos power equations are not as accurate outside that range.// Do it here once instead of within both sin and cos. if (( x > TWO_PI ) || ( x < - TWO_PI )) x = ScaleNearZero ( x, TWO_PI ); // Get the sine and cosine of the number. sinx = _sin ( x ); cosx = _cos ( x ); result = sinx / cosx; // Eliminate divide by zero problems if ( cosx == 0.0 ) { if ( sinx >= 0.0 ) result = POS_INFINITY; else result = NEG_INFINITY; } // Otherwise, calculate the result else result = sinx / cosx; return result; }/***************************************************** * FUNCTION: _asin * * DESCRIPTION: Calculate the inverse sine of a double value. * * RETURNED: The matching value in radians. * Returns zero if incoming value is not * a valid sine value between -1 and +1.*****************************************************/double _asin // ( out ) the result( double x // ( in ) the sine to calculate the inverse of){ double sinx = 0.0;// Check for valid incoming sine value if (( x >= - 1.0 ) && ( x <= 1.0 )) { // Make suitable for table lookup, lookup the radians equivalent,// and readjust the resulting sign if appropriate. sinx = _abs ( x ); sinx = TTLookup ( sinx, sines, radians ); if ( x < 0.0 ) sinx = -1.0 * sinx; } return sinx; }/***************************************************** * FUNCTION: _acos * * DESCRIPTION: Calculate the inverse cosine of a double value. * * RETURNED: The matching value in radians. * Returns zero if the incoming value is not * a valid cosine value between -1 and +1.*****************************************************/double _acos // ( out ) the result( double x // ( in ) the number to get th inverse cosine of){ double cosx = 0.0;// Check for valid incoming cosine value if (( x >= - 1.0 ) && ( x <= 1.0 )) {// Make suitable for table lookup, lookup the radians equivalent,// and readjust the resulting sign if appropriate. cosx = _abs ( x ); cosx = TTLookup ( cosx, cosines, radians ); if ( x < 0.0 ) cosx = PI - cosx; } return cosx;}/***************************************************** * FUNCTION: _atan * * DESCRIPTION: Calculate the inverse tangent of a double value. * * RETURNED: The matching value in radians. * Returns -PI/2 or + PI/2 if the incoming value is not * a valid tangent value between -552.0 and +552 * ( -89.9 <-> 89.9 degrees).*****************************************************/double _atan // ( out ) the result( double x // ( in ) the number to get the inverse tangent of){ double tanx = 0.0; // Check for valid incoming tangent value. if ( x < TAN_MIN_VALUE ) tanx = - HALF_PI; else if ( x > TAN_MAX_VALUE ) tanx = HALF_PI; else { // Make suitable for table lookup, lookup the radians equivalent,// and readjust the resulting sign if appropriate. tanx = _abs ( x ); tanx = TTLookup ( tanx, tangents, radians ); if ( x < 0.0 ) tanx = - 1.0 * tanx; } return tanx;}/***************************************************** * FUNCTION: _sin2 * * DESCRIPTION: Lookup the sine of a double radians number. * * RETURNED: The result.*****************************************************/double _sin2 // ( out ) the result( double x // ( in ) the number to get the sine of){ double result = 0.0, xRange = x, xval;// Scale input angle to between -2 PI and + 2 PI and// adjust for negative value. if (( x > TWO_PI ) || ( x < - TWO_PI)) xRange = ScaleNearZero ( x, TWO_PI ); xval = _abs ( xRange ); // Convert to proper sign & range for table lookup if (( xval > ( HALF_PI )) && ( xval <= PI )) xval = PI - xval; else if (( xval > PI ) && ( xval <= ( 3.0 * HALF_PI ))) xval = xval - PI; else if ( xval > ( 3.0 * HALF_PI )) xval = ( TWO_PI ) - xval; // Lookup the radians equivalent result = TTLookup ( xval, radians, sines ); // Adjust the final sign if necessary if (( xRange > PI ) || (( xRange < 0.0 ) && ( xRange > - PI ))) result = - result; return result; }/***************************************************** * FUNCTION: _cos2 * * DESCRIPTION: Lookup the cosine of a double radians number. * * RETURNED: The result.*****************************************************/double _cos2 // ( out ) the result( double x // ( in ) the number to get the sine of){ double result = 0.0, xRange = x, xval;// Scale input angle to between - PI and + PI and// adjust for negative value. if (( x > TWO_PI ) || ( x < - TWO_PI )) xRange = ScaleNearZero ( x, TWO_PI ); xval = _abs ( xRange ); // Convert to proper sign & range for table lookup if (( xval > ( HALF_PI )) && ( xval <= PI )) xval = PI - xval; else if (( xval > PI ) && ( xval <= ( 3.0 * HALF_PI ))) xval = xval - PI; else if ( xval > ( 3.0 * HALF_PI )) xval = TWO_PI - xval; // Lookup the radians equivalent result = TTLookup ( xval, radians, cosines ); // Adjust the final sign if necessary if ((( xRange > ( - 3.0 * HALF_PI )) && ( xRange < - HALF_PI)) || (( xRange > HALF_PI ) && ( xRange < ( 3.0 * HALF_PI )))) result = - result; return result; }/***************************************************** * FUNCTION: _tan2 * * DESCRIPTION: Calculate the tangent of a double radians number. * * RETURNED: The result. If the cosine is zero, the * value returned is either "NEG_INFINITY" * or "POS_INFINITY", very small and very large values.*****************************************************/double _tan2 // ( out ) the result( double x // ( in ) the number to get the tangent of){ double sinx, cosx, result, xval = x;// Scale input angle to proper value between -2 PI and + 2 PI.// The sin and cos power series are not as accurate outside that range.// Do it here once instead of within both sin and cos. if (( x > TWO_PI ) || ( x < - TWO_PI )) x = ScaleNearZero ( x, TWO_PI ); // Get the sine and cosine of the number. sinx = _sin2 ( x ); cosx = _cos2 ( x );// Eliminate divide by zero problems if ( cosx == 0.0 ) { if ( sinx >= 0.0 ) result = POS_INFINITY; else result = NEG_INFINITY; } // Otherwise, calculate the result else result = sinx / cosx; return result; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -