views:

177

answers:

2

Here is my problem (C#) :

double Y = 0.0;
double X = -21.0;

double q1_Test = Math.Atan2(0.0, -21.0);               // gives Math.Pi
double q1_Test2  = Math.Atan2(( double)Y, (double)X);  // gives Math.Pi

double w1 = <SomeclassInstanceGlobalHere>.getW1();  // This is a class which updates a variable
double w2 = <SomeclassInstanceGlobalHere>.getW2();  // This is a class which updates a variable

double q1  = Math.Atan2(w2, w1);           // ** gives -Math.Pi ** ???
//w2 shows 0.0 and w1 shows -21.0

When I get the values from the other class, variable values are 0.0 and -21.0 respectively. It also shows in the IDE while debugging. What is going wrong here?

+3  A: 

w2 must actually be -0.0, which is formatted as 0

The following blog post shows how you can actually test for this (Decimal.GetBits(value)): http://blogs.msdn.com/bclteam/archive/2006/10/12/decimal-negative-zero-representation-lakshan-fernando.aspx

Rob Fonseca-Ensor
But then, same should have had happend withdouble q1_Test2 = Math.Atan2(( double)Y, (double)X); // gives Math.Pi
Rahul2047
Math.Atan2(0.0, -21.0) gives pi, Math.Atan2(-0.0, -21.0) gives -pi. Maybe it seems like this "shouldn't" happen, but as peter points out, these are technically the same angle.
Rob Fonseca-Ensor
q1 's sign matters here for further calculations. Let see if I can do anything about it ... :(
Rahul2047
+2  A: 

Note that -Math.PI and Math.PI are equivalent for the purposes of trigonometry. It is almost always a very poor idea to compare angles as if they were doubles. See extended SO discussion: http://stackoverflow.com/questions/1813483/1813809#1813809

peter.murray.rust