Description
Purpose:
- Experience using arrays and numeric functions.
- Create and debug a program with greater complexity.
Assignment:
Write a program that demonstrates the correct operation of the described sin_() function.
float sin_ (float input_angle)
The calculation of this trigonometric value will be performed in a multi-stage process; first a look up table (array) will be built storing the sin() for all values between 0 degrees and 359 degrees using a taylor series. (see https://en.wikipedia.org/wiki/Taylor_series for background and the algorithm to be used).
This lookup table is created once and then used when the function is called for the life of the program.
The sin_() function will use the lookup table to linearly interpolate between two values of the lookup table to return the answer. (see https://en.wikipedia.org/wiki/Linear_interpolation for background and the equation to be used for linear interpolation).
The advantage to this design is the function call is very fast, requiring only some addition and division. Slower calculations, like the calculation of the sin in the lookup table is done only one time during initialization.
An example of using a look up table and interpolation:
X | Y |
0 | 0 |
5 | 25 |
10 | 100 |
15 | 225 |
20 | 400 |
To solve for X = 7
Y = (25(10-7) +100(7-5) ) / (10-5)
Y = (75 +200) / 5
Y = 55
In this example, the function we are interpolating is Y=X*X. An X value of 7 would give 49, so the Error in using the lookup table and interpolation is:
Error = Correct – Measured
Error = 49 – 55 = -6
You will find the sin() function calculated as described above with be much closer to actual value.
Specific Requirements for the sin_() function:
- The input angle is provided to the function in degrees.
- Input values must be >= 0.0 and < 360.0 . Values outside of this range return a 0.0
- A function named init(), with no parameters, will be called as the first line of the main program. This function will build the look up table as described.
- Use a Taylor Series with 4 terms to calculate the sin value stored in the look up table. (the definition of ‘terms’ is flexible, you are allowed to count the starting X term as a ‘term’ or not. In any event, the number of terms is fixed)
- Calculate the factorial using an iterative (non-recursive) function.
Specific Requirements for the program:
- A function named init(), with no parameters, will be called as the first line of the main program. This function will build the look up table as described. It is acceptable for the look up table to be a global variable.
- The program will read from stdin.
- The input shall be a single floating point number on a line.
- Input numbers out of range will be ignored.
- The program shall continue to read input and calculate a result until a -1 is entered. A -1 will cause the program to exit
- For each number entered, with the exception of a -1, the program will print 4 numbers; the input value, the value returned from sin_, the value returned from sin(), and the difference between sin() and sin_() {the Error}. The 4 numbers will be printed on a single line, separated by a space. No other output is to be printed.
- When you test the program, create a data file with input values, one per line, with a -1 being the last number in the file. Use bash input redirection ( “<”) to execute the program with this input file.
- Each output number will be separated by a space.
- Assigning an integer to a float truncates. Use this technique to generate the index into the look up table.
- Headers to be used are stdio.h, assert.h, and math.h
- For numerical constants (like PI), use the constants already defined in math.h . The predefined constant for PI is a macro definition called M_PI. Look in the system header file if you are curious. Due to the differences in implementation, it is permitted to define M_PI in your program as a macro symbol. (#define M_PI <some number>). If you define your own PI, make sure it is correct and has enough digits of precision.
- Every function must have comments that describe its purpose,implementation, and interface.
- Any pre and post conditions for a function must be described in the comments, and checked at runtime using assert (). For grading purposes, ‘assert’ must show up at least two times in the file.
- The program will be contained in a single C file. The file shall be named hw02_last_name_first_name.c .
- Use the option -std=c99 when you compile this code.
- You will turn into blackboard a single C file.
Grading Criteria:
- Program compiles without warnings. ( -Wall )
- Output values are correct. This will be verified by a set of test values created by the grader.
- The output as described above should be easily consumed by gnuplot for a graphical evaluation of the program outputs
- The interpolation between values is visible on the graph.
- Compliance with assignment requirements.
Example Output. (Note the numerical accuracy may differ depending on your code, the specific compiler used, and the size of operands used)
Note. These examples are not exhaustive and do not test all of the functionality described for this homework assignment.
[bdavis@localhost hw2]$ ./a.out
-1
[bdavis@localhost hw2]$ ./a.out
0
0.000000 0.000000 0.000000 0.000000
90
90.000000 1.000004 1.000000 -0.000004
180
180.000000 -0.006925 -0.000000 0.006925
270
270.000000 -1.000004 -1.000000 0.000004
359
359.000000 -0.017453 -0.017453 -0.000000
-1
[bdavis@localhost hw2]$ ./a.out <hw2_test_data_1.dat >ofile
[bdavis@localhost hw2]$ gnuplot
gnuplot> plot “ofile” using 1,”ofile” using 2,”ofile” using 3,”ofile” using 4
gnuplot> quit