Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include <math.h>
- #define PI_OVER_6 0.523598776
- double mycos(double theta, int n) {
- double x, y, z;
- // compute k
- double k = 1;
- for (int i = 0; i < n; ++i) {
- k *= sqrt(1 + pow(2, -2 * i));
- }
- x = 1/k;
- y = 0;
- z = theta;
- double nextX, nextY, nextZ;
- double alpha;
- double measuredError = 0;
- for (int i = 0; i < n; ++i) {
- alpha = atan(pow(2, -i));
- measuredError = x - cos(PI_OVER_6);
- printf("x(%d) = %lf\n", i, x);
- printf("y(%d) = %lf\n", i, y);
- printf("z(%d) = %lf\n", i, z);
- printf("alpha(%d) = %lf\n", i, alpha);
- printf("Measured error: %lf\n", measuredError);
- if (z >= 0) {
- nextX = x - (pow(2, -i) * y);
- nextY = y + (pow(2, -i) * x);
- nextZ = z - atan(pow(2, -i));
- } else {
- nextX = x + (pow(2, -i) * y);
- nextY = y - (pow(2, -i) * x);
- nextZ = z + atan(pow(2, -i));
- }
- x = nextX;
- y = nextY;
- z = nextZ;
- }
- return nextX;
- }
- int main() {
- double piOver6 = 0.523598776;
- double theta = piOver6, result;
- int n = 0;
- while(1) {
- //printf("cos(theta) = ?; type in theta: \n");
- //scanf("%lf", &theta);
- printf("type in n (number of iterations): \n");
- scanf("%d", &n);
- printf("RESULT: %lf\n\n", mycos(theta, n));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement