Physics and Astronomy |
Back to top
Introduction to C - a complete exampleThis example should give us enough to understand the following example, although there are a few issues we will want to come back to in the next lecture:// // Demonstrate the trigonometric identity // cos(a+b) equals cos(a)*cos(b) - sin(a)*sin(b) // #include <stdio.h> #include <math.h> int main() { double a, b, cos_a_plus_b, error; a = 0.707; // Random values b = 0.1234; cos_a_plus_b = cos(a) * cos(b) - sin(a) * sin(b); error = cos_a_plus_b - cos(a+b); printf("I calculate cos(%g) to be %g\n", a + b, cos_a_plus_b); printf("with an error of %g\n", error); return 0; } ExerciseAnswer the following questions before reading the next section:
Things to notice
On my machine the program prints: I calculate cos(0.8304) to be 0.674581 with an error of -1.11022e-16Log in |