问题
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?
回答1:
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
回答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
来源:https://stackoverflow.com/questions/1957801/math-atan2-or-class-instance-problem-in-c-sharp