I'm using this as my formula for sine.

My cosine and tangent functions work perfectly. But with sine, I can't get it to work right for values that give an answer of 1 or -1. Like 90 and -90. 90 is supposed to come back with 1, but it comes back with .9455. I'll post my whole program, but the part that I'm worried about is Sine.

#define _CRT_SECURE_NO_WARNINGS #define PI 3.141592653589793238 #define ALMOSTZERO 0.00000005 #include <stdio.h> #include <math.h> double cosd(double); double sind(double); double tand(double); void main() { double angle, anssin, anscos, anstan; // shell to test the function do { // the format specifier for double is lf // that’s lowercase L and f (for long float) printf("Enter angle (in degrees): "); scanf("%lf",&angle); anssin=sind(angle); anscos=cosd(angle); anstan=tand(angle); printf("Sin(%6.2lf)=%6.4lf ", angle, anssin); printf("Cos(%6.2lf)=%6.4lf ", angle, anscos); printf("Tan(%6.2lf)=%8.4g\n\n", angle, anstan); // edited to add 'g' } while (angle != -999.0); // -999 is sentinel value } double cosd(double angle_d) // this is my cosine function { double angle_r; angle_r = angle_d * (PI/180); double sum, term, n; sum = 1; term = 1; n = 1; while (term>ALMOSTZERO) { term = term * ((angle_r*angle_r)/((2*n)*(2*n-1))); sum = sum - term; n++; term = term * ((angle_r*angle_r)/((2*n)*(2*n-1))); sum = sum + term; n++; } return sum; } double sind(double angle_d) // this is my sine function { double angle_r; angle_r = angle_d * (PI/180); double sum, term; int n; sum = angle_r; term = 1; n = 1; while (term>ALMOSTZERO) { term = term * ((angle_r*angle_r*angle_r)/((3*n)*(3*n-1)*(3*n-2))); sum = sum - term; n++; term = term * ((angle_r*angle_r*angle_r)/((3*n)*(3*n-1)*(3*n-2))); sum = sum + term; n++; } return sum; } double tand(double angle_d) // tangent = sin/cos { double ans; float n1, n2, n3, n4; n1 = (angle_d - 90)/(360); n2 = (angle_d + 270)/(-360); n3 = (angle_d + 90)/(-360); n4 = (angle_d - 270)/(360); if (n1 >= 0 || n2 >= 0 || n3 >= 0 || n4 >= 0) // my way of testing to cosd =~ 0 { if (n1 >= 0) { return 1.7e+308; } if (n2 >= 0) { return 1.7e+308; } if (n3 >= 0) { return -1.7e+308; } if (n4 >= 0) { return -1.7e+308; } } else { ans = (sind(angle_d)/cosd(angle_d)); return ans; } }

I really need the help. I've been working on this for days and I can't find my mistake.

Thanks,

Sean

This post has been edited by **Sean7**: 22 October 2010 - 05:24 PM