## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

## 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:

`sine`value``

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

Example D-1. sine/sine.c
`[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. * **********************************************************/ ...`

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required