delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/10/23:50:21

From: "Gareth Davies" <dgmdavies AT onaustralia DOT com DOT au>
Subject: math.h sin() function returns wrong value
Newsgroups: comp.os.msdos.djgpp
Message-ID: <01bc8d93$99a3f7a0$2a39868b@dgmdavies>
NNTP-Posting-Host: 139.134.57.42
Date: 11 Jul 97 01:51:37 GMT
Lines: 47
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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