Help with a sine function in C

I need help with a sine function in C

Page 1 of 1

2 Replies - 2522 Views - Last Post: 22 October 2010 - 07:25 PM Rate Topic: -----

#1 Sean7  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 29-September 10

Help with a sine function in C

Posted 22 October 2010 - 05:17 PM

I'm working on an assignment for class that involves writing a sine, cosine, and tangent function in C. The assignment can be found here.

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


Is This A Good Question/Topic? 0
  • +

Replies To: Help with a sine function in C

#2 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: Help with a sine function in C

Posted 22 October 2010 - 06:56 PM

I think it's in your calculation.

You're trying to do sin x = x - x^3/3! + x^5/5! - x^7/7! ... etc.

When I do it by hand, I get
sum = angle_r (which is x)
then sum -= term, term being x^3/3!

So far, we have:
x - x^3/3!

Looking nice.

Next, we get term = term * angle_r^3 / [(3*n)*(3*n-1)*(3*n-2)]. From that I get:

(Original term) x^3/3! times x^3/(6*5*4)... which is x^6/6!. That is not x^5/5!.

We want :
sin x = x - x^3/3! + x^5/5! - x^7/7! ...

But instead we have :
sin x = x - x^3/3! + x^6/6! - x^9/9! ...

I hope this is clear enough, I'll probably try to edit it so it looks nicer.

Edit: Fixed my signs (+/-) and tried to clarify a bit.

This post has been edited by elgose: 22 October 2010 - 07:11 PM

Was This Post Helpful? 1
  • +
  • -

#3 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: Help with a sine function in C

Posted 22 October 2010 - 07:25 PM

I played around with it for a minute and got it to work. It's anti-DIC to just give you the code, of course, but I wanted to post this in case you needed a nudge in the right direction.

You're code starts off for the first 2 terms just fine. After that point it all falls apart - so you get an approximation of sin x that's not as close as the Taylor-series version.

Here's a simple program illistrating the concept so you can hopefully apply it to your code. If I want to find:

Y - Y^2 - Y^2.5 - Y^3.0 - Y^3.5 - Y^4.0 ...

I see that I take Y as my first term. For my second term, I take my previous term and multiply it by Y. Then it changes, for my 3rd term and beyond its the previous term multiplied by Y^0.5.
My pseudocode:
function mySeriesUpTo100Terms(y)
{
    calculation = y;
    calculation -= y * y;
    for (n = 3; i < 100; i++);
    {
        calculation -= y ^ (i+0.5);
    }
}



So I get things started outside of the loop, and then all the terms that easily follow the (Previous term) * (Y^0.5) rule get placed within my loop.

As another note - you may want to evaluate your tangent function as well.

Tan (180 Degrees) is 0, your program returns 1.7e+308. Also, Tan(90 Degrees) and Tan (270 Degrees) do not exist, yet your program returns 1.7e+308 for those calculations as well.

Edit: I just realized this was an actual assignment you linked to, so returning 1.7e+308 is appropriate for Tan(90) and Tan(270) according to the directions. On that note -

BE CAREFUL ABOUT POSTING ASSIGNMENTS AND YOUR HOMEWORK ONLINE, EVEN HERE.

Really, professors recently have been strolling through the forums - and if someone else in your class is checking out DIC (very likely) they might steal your code. You know what happens when two people turn in identical code?

You don't want to find out.

This post has been edited by elgose: 22 October 2010 - 07:37 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1