// 1-15-C.c #include #include /* Function declarations */ int main(); double my_sin( double x ); /* Function definitions */ /* int main() * * Compare our implementation of sin(x) using its * Taylor series with the implementation provided * by the C standard library. */ int main() { double x = 3.2; /* Print floating-point numbers with 16 digits * after the decimal point. */ printf( "my_sin(x): %.16f\n", my_sin( x ) ); printf( "sin(x): %.16f\n", sin( x ) ); printf( "reference: %.16f\n", -0.058374143427579909137 ); return 0; } /* double my_sin( double x ) * * Approximate sin(x) using the Taylor series * * x^3 x^5 x^7 * sin(x) = x - --- + --- - --- + ... * 3! 5! 7! * * Rather than calculating each power and factorial * separately, calculate each term from the previous * term: * * x^2 * next = -term -----------, * n(n + 1) * * where n takes the values 2, 4, 6, ... */ double my_sin( double x ) { if ( !isfinite( x ) ) { return NAN; } double result = x; double term = x; double n; /* Since C99, variables may be declared in the * initialization expression of a for loop: * * for ( double n = 2.0; ... ) * * Declaring 'n' separately also allows this * code to compile using older versions of C. */ for ( n = 2.0; ; n += 2.0 ) { term *= -(x/n)*(x/(n + 1.0)); if ( fabs( term ) < 1e-16 ) { break; } result += term; } return result; } ``` ```c #include #include /* Function declarations */ int main(); double my_sin( double x ); /* Function definitions */ /* int main() * * Compare our implementation of sin(x) using its * Taylor series with the implementation provided * by the C standard library. */ int main() { double x = 3.2; /* Print floating-point numbers with 16 digits * after the decimal point. */ printf( "my_sin(x): %.16f\n", my_sin( x ) ); printf( "sin(x): %.16f\n", sin( x ) ); printf( "reference: %.16f\n", -0.058374143427579909137 ); return 0; } /* double my_sin( double x ) * * Approximate sin(x) using the Taylor series * * x^3 x^5 x^7 * sin(x) = x - --- + --- - --- + ... * 3! 5! 7! * * Rather than calculating each power and factorial * separately, calculate each term from the previous * term: * * x^2 * next = -term -----------, * n(n + 1) * * where n takes the values 2, 4, 6, ... */ double my_sin( double x ) { if ( !isfinite( x ) ) { return NAN; } double result = x; double term = x; double n; /* Since C99, variables may be declared in the * initialization expression of a for loop: * * for ( double n = 2.0; ... ) * * Declaring 'n' separately also allows this * code to compile using older versions of C. */ for ( n = 2.0; ; n += 2.0 ) { term *= -(x/n)*(x/(n + 1.0)); if ( fabs( term ) < 1e-16 ) { break; } result += term; } return result; } ``` ```c #include #include /* Function declarations */ int main(); double my_sin( double x ); /* Function definitions */ /* int main() * * Compare our implementation of sin(x) using its * Taylor series with the implementation provided * by the C standard library. */ int main() { double x = 3.2; /* Print floating-point numbers with 16 digits * after the decimal point. */ printf( "my_sin(x): %.16f\n", my_sin( x ) ); printf( "sin(x): %.16f\n", sin( x ) ); printf( "reference: %.16f\n", -0.058374143427579909137 ); return 0; } /* double my_sin( double x ) * * Approximate sin(x) using the Taylor series * * x^3 x^5 x^7 * sin(x) = x - --- + --- - --- + ... * 3! 5! 7! * * Rather than calculating each power and factorial * separately, calculate each term from the previous * term: * * x^2 * next = -term -----------, * n(n + 1) * * where n takes the values 2, 4, 6, ... */ double my_sin( double x ) { if ( !isfinite( x ) ) { return NAN; } double result = x; double term = x; double n; /* Since C99, variables may be declared in the * initialization expression of a for loop: * * for ( double n = 2.0; ... ) * * Declaring 'n' separately also allows this * code to compile using older versions of C. */ for ( n = 2.0; ; n += 2.0 ) { term *= -(x/n)*(x/(n + 1.0)); if ( fabs( term ) < 1e-16 ) { break; } result += term; } return result; }