Math.Atan2 or class instance problem in C#

守給你的承諾、 提交于 2019-12-12 04:34:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!