Appendix D. A Program to Compute a Sine Using a Power Series
This program is designed to compute the sine function using a power series. A very limited floating-point format is used to demonstrate some of the problems that can occur when using floating-point.
The program is invoked by:
sinevalue
where value is an angle in radians.
The program computes each term in the power series and displays the result. It continues computing terms until the last term is so small that it doesn’t contribute to the final result.
For comparison purposes, the result of the library function
sin
is displayed as well as the
computed sine.
The sine.c Program
[File: sine/sine.c] /********************************************************** * sine -- Computes sine using very simple floating * * arithmetic. * * * * Usage: * * sine <value> * * * * <value> is an angle in radians * * * * Format used in f.fffe+X * * * * f.fff is a 4-digit fraction * * + is a sign (+ or -) * * X is a single digit exponent * * * * sine(x) = x - x**3 + x**5 - x**7 * * ----- ---- ---- . . . . * * 3! 5! 7! * * * * Warning: This program is intended to show some of the * * problems with floating-point. It is not intended * * to be used to produce exact values for the * * sine function. * * * * Note: Even though we specify only one digit for the * * exponent, two are used for some calculations. * * We have to do this because printf has no * * format for a single digit exponent. * **********************************************************/ ...
Get Practical C Programming, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.