0

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?

Rick2047
  • 1,565
  • 7
  • 24
  • 35

2 Answers2

3

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
  • 15,510
  • 44
  • 57
  • But then, same should have had happend with double q1_Test2 = Math.Atan2(( double)Y, (double)X); // gives Math.Pi – Rick2047 Dec 26 '09 at 05:25
  • 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 Dec 26 '09 at 07:22
  • q1 's sign matters here for further calculations. Let see if I can do anything about it ... :( – Rick2047 Dec 28 '09 at 06:04
2

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: Averaging angles... Again

Community
  • 1
  • 1
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217