Rounding Issue, Radian Angles and Distance

Noob Here.

I have a problem where I have to round my outputs to 4 decimal places and I'm getting weird return values.

I'm using this with all variables being double or int since precision is secondary to getting it to work semi-accurately.

1
2
3
4
5
6
7
8
    //Rounds Input to 4 decimal places
    double round(double initial)
    {
	int newInitial = (initial * 10000);
	double round = newInitial / 10000;

	return round;
    }


This returns values along the lines of 0.781623...-1311 & 2.25194111-3

I have to find 2 things, a Positive radian angle for "vectorC" and Distance saved going from the Origin of a Coordinate plane by following one direct vectorC over the the path created by 2 others, vectorA
+vectorB.

Initially I was getting this problem with my function that finds the radian angle here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    /*Finds Positive Radian Angle of Vector C by using inverse tangent given x and y dimensions.
Using Pi at 11 decimals.
    */

    double angle(int x2,int y2)
    {
	double pi = 3.14159265359;
	double vAngle = atan(y2 / x2);


	
	if (x2 < 0 && y2 > 0)
	{
		vAngle = pi + vAngle;
	}
	if (x2 < 0 && y2 < 0)
	{
		vAngle = pi + vAngle;
	}
	if (x2 < 0 && y2 < 0)
	{
		vAngle = (2 * pi) + vAngle;
	}

	return vAngle;
    }


I was thinking that its because of the decimal length being to long for double but I'm also getting this issue with my Distance function here:

1
2
3
4
5
6
7
8
    //Uses distance formula to find the Magnitude of a "Vector" from Coordinates (x1,y1) to (x2,y2)
    double distance(double x1,double y1,double x2,double y2)
    {

	double vDistance = sqrt((pow(x2 - x1, 2) + pow(y2 - y1, 2)));
	return vDistance;

    }


I'm kinda lost. I know the math is correct but my implementation is breaking somewhere. Any help would be great.

EDIT: OK so it seems my functions are breaking before the rounding even occurs.

Heres an Example. Lets say we have 3 points. (0,0)(1,1)(1,-3)

Vector A from (0,0)->(1,1) has distance sqrt(2) aprox ~ 1.4142
Vector B from (1,1)->(1,-3) has distance 2
Vector C from (0,0)->(1,-3) has distance sqrt(10) aprox ~ 3.1623

When using distance() to find the values it returns 1.4142143.16228-1, 3.16228 , 2111-3. So there's something wrong with either my distance function or the numbers passed but the numbers passed are whole number doubles. Dont know why the first answer is coming back as a number with 2 decimals

Then with my Angle function the Angle should be x=arctan(-3/1)=5.0341. Instead im getting just -1. I can post the entire program if needed.
Last edited on
I don't see anything wrong although you could use the round() function and it's not worth using pow for squaring a number:
1
2
3
4
5
6
7
8
9
double round4(double initial)
{
   return round(initial * 1e4) / 1e4;
}

double distance(double x1,double y1,double x2,double y2)
{
	return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}

I don't know what you mean by the "first answer" (what's the first answer?) "coming back with 2 decimals".
For your angle function use the two-parameter function
atan2( y2, x2 )
and NOT
atan( y2 / x2 )
or you will run into divide-by-zero issues when x2 is zero.

Angle() should be taking two doubles for arguments, not two ints. Forget playing with doubles and ints. Coordinates, distances, angles etc should all be doubles.

By all means post a compileable code, and some sample input.
Last edited on
Topic archived. No new replies allowed.