delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/11/02:57:43

Message-ID: <19970711085935.20112@gil.physik.rwth-aachen.de>
Date: Fri, 11 Jul 1997 08:59:35 +0200
From: Christoph Kukulies <kuku AT gilberto DOT physik DOT rwth-aachen DOT de>
To: Andrew Deren <aderen AT eecs DOT uic DOT edu>
Cc: Gareth Davies <dgmdavies AT onaustralia DOT com DOT au>, djgpp AT delorie DOT com
Subject: Re: math.h sin() function returns wrong value
References: <01bc8d93$99a3f7a0$2a39868b AT dgmdavies> <Pine DOT SUN DOT 3 DOT 95 DOT 970711001508 DOT 5798A-100000 AT bert DOT eecs DOT uic DOT edu>
Mime-Version: 1.0
In-Reply-To: <Pine.SUN.3.95.970711001508.5798A-100000@bert.eecs.uic.edu>; from Andrew Deren <aderen@eecs.uic.edu> on Fri, Jul 11, 1997 at 12:17:38AM -0500

On Fri, Jul 11, 1997 at 12:17:38AM -0500, Andrew Deren wrote:
> I did not really look at the whole code, but I could notice that you are
> using degrees instead of radians, and that what sin and other trig
> functions use. 

And you were passing int values to the sin() function which is
a programming error. OTOH, when using radians you would use
float (double) numbers anyway. But just to be understood as an
additional warning. Also, to avoid funny results, watch out for
places where you mix int's with doubles or floats. C doesn't
do automatic type conversion. So use casts in these places.

> 
> On 11 Jul 1997, Gareth Davies wrote:
> 
> > I'm pretty new to C, and this has really got me stuffed.  Can anybody give
> > me a hand?
> > The idea was to write a pretty simple two player tank game with djgpp v2
> > and allegro, where you can rotate the tank and move forward.  I used
> > trigonometry to figure out the x and y modifiers on an up-arrow keypress,
> > but the sin() function has been returning weird values.
> > For example, I wrote a program to test this -
> > 
> > /* Test program for sin() function and angle conversion, by Gareth Davies
> > 1997
> >    It probably isn't great code, but I was trying to figure out the problem
> > */
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <math.h>
> > 
> > void main()
> > {
> >   char textangle;			/* the text version of angle */
> >   int angle;			/* the angle to be passed to sin() */
> >   float trigx, trigy, trigangle;	/* the results of the trig and pythagoras 
> > 			   calculations */
> > 
> >   printf("sin(45) = %f\n", sin(45));
                                   ^^
> > 
> >   printf("angle = ");
> >   gets(textangle);			/* get an angle out of 256, and store it in
> >   angle = atoi(textangle);			   the angle variable */
> > 
> >   trigangle = angle * 1.40625;		/* conversion from an allegro fixed point
> >   printf("trigangle = %f\n", trigangle);	   style angle, out of 256 instead
> > of 360 */
> > 
> >   trigy = sin(trigangle);			/* here's the problem.  returns 0.850904, 
> >   printf("trigy = %f\n", trigy);		   instead of the correct 0.707106 */
> > 
> >   trigy = trigy * 5;			/* scaling the triangle */
> >   printf("trigy * 5 = %f\n", trigy);
> > 
> >   trigx = sqrt(25 - pow(trigy, 2));		/* pythagoras, to work out the
> > x-modifier */
> >   printf("trigx^2 = %f\n", trigx);
> > 
> >   trigx = sqrt(trigx);			/* more pythag */
> >   printf("trigx = %f\n", trigx);
> > 
> > 					/* final result */
> >   printf("X-modifier = %f\nY-modifier = %f\n", trigx, trigy);
> > }
> > 
> > A note that I am converting from the allegro angle format, out of 256
> > instead of 360.  Still, it doesn't matter.  If I enter 32 as the angle, the
> > converted angle is 45, which is right, and then when I call the sin()
> > function I get a completely different answer.  Can anybody help?  I've
> > tried including the math library (-lm on the gcc command line), compiling
> > with the floating point emulator, and just about anything else I can think
> > of.  Do I need to get djgpp version 3?  At the moment, I've got version 2.
> > 
> > Gareth
> > 

-- 
Chris Christoph P. U. Kukulies kuku AT gil DOT physik DOT rwth-aachen DOT de

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019