delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/11/01:17:55

Date: Fri, 11 Jul 1997 00:17:38 -0500 (CDT)
From: Andrew Deren <aderen AT eecs DOT uic DOT edu>
To: Gareth Davies <dgmdavies AT onaustralia DOT com DOT au>
cc: djgpp AT delorie DOT com
Subject: Re: math.h sin() function returns wrong value
In-Reply-To: <01bc8d93$99a3f7a0$2a39868b@dgmdavies>
Message-ID: <Pine.SUN.3.95.970711001508.5798A-100000@bert.eecs.uic.edu>
MIME-Version: 1.0

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. 

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
> 

- Raw text -


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