From: Christoph Kukulies Message-Id: <199703260838.JAA13050@gilberto.physik.rwth-aachen.de> Subject: Re: Newbie troubles with Sin and Cos In-Reply-To: from Andrew Deren at "Mar 25, 97 07:43:23 pm" To: aderen AT eecs DOT uic DOT edu Date: Wed, 26 Mar 1997 09:38:24 +0100 (MET) Cc: DBerry1 AT dca DOT gov DOT au, djgpp AT delorie DOT com Reply-To: Christoph Kukulies MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit > Your problem is that math sin and cosine function use radians not degrees. > To convert from degrees to radians use > radians = (pi * degrees) / 180.0; > where pi 3.14 or you can use math #define PI to get it more accurate > > Than your main function would be: > > main () > > { > > while (theta < 360) > > { > > result = sin((PI * theta) / 180.0); > > printf ("Sin of %i is %f\n",theta,result); > > theta++; > > } > > return 0; > > } > The other way to do it would be to say > while (theta < 2 * PI){ The original program contained some other errors, like 1/ result should be defined double (rather than float) though from the nature of sine/cosine they never are absolute > 1. 2/ theta should have been defined double as well since the argument of sin is double as well as the return value is. 3/ All calculations (like 2 * PI) and comparisons (like theta < 360) should be done in doubles resp. the appropriate types or proper casts should be used. C is not FORTRAN. > .. > theta += 0.0174532925; // (2*PI)/360 > } > I hope that helps > > On 26 Mar 1997 DBerry1 AT dca DOT gov DOT au wrote: > > > G'day > > > > I've just started playing around with DJGPP and have come across the > > following problem. > > > > I'm trying to write a routine that spins a pixel around in a circle. That > > works ok so far, except that the X and Y points are never next to each > > other - I think I have narrowed it down to the results I am getting from > > using COS and SIN. > > > > for instance here's the output from a simple prog I did to test my idea > > > > Sin of 0 is 0.000000 > > Sin of 1 is 0.841471 > > Sin of 2 is 0.909297 > > Sin of 3 is 0.141120 > > Sin of 4 is -0.756802 > > > > But using a calculator I get > > > > Sin of 0 is 0 > > Sin of 1 is 0.017452 > > Sin of 2 is 0.034899 > > Sin of 3 is 0.052335 > > Sin of 4 is 0.069756 > > > > Here's the simple prog... > > > > #include > > #include > > #include > > > > float result = 0.0; > > int theta = 0; > > > > main () > > { > > while (theta < 360) > > { > > result = sin(theta); > > printf ("Sin of %i is %f\n",theta,result); > > theta++; > > } > > return 0; > > } > > > > Am I using the wrong variable types to hold the results ? Do I need to do > > something special in DJGPP to get the magic numbers ? > > > > Thanks > > dberry AT dca DOT gov DOT au > > > > > > -- Christoph P. U. Kukulies kuku AT gil DOT physik DOT rwth-aachen DOT de