【问题标题】:Math.Atan2 or class instance problem in C#Math.Atan2 或 C# 中的类实例问题
【发布时间】:2009-12-24 10:39:41
【问题描述】:

这是我的问题(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

当我从另一个类中获取值时,变量值分别为 0.0 和 -21.0。它还在调试时显示在 IDE 中。 这里出了什么问题?

【问题讨论】:

  • 答案是一样的。处理角度时使用模数学。

标签: c# .net debugging atan2


【解决方案1】:

w2实际上必须是-0.0,格式为0

以下博客文章展示了如何实际测试这一点(Decimal.GetBits(value)): http://blogs.msdn.com/bclteam/archive/2006/10/12/decimal-negative-zero-representation-lakshan-fernando.aspx

【讨论】:

  • 但是,同样的情况应该发生在 double q1_Test2 = Math.Atan2((double)Y, (double)X); // 给出 Math.Pi
  • Math.Atan2(0.0, -21.0) 给出 pi,Math.Atan2(-0.0, -21.0) 给出 -pi。也许看起来这种“不应该”发生,但正如彼得指出的那样,这些在技术上是相同的角度。
  • q1 的符号在这里很重要,以便进一步计算。让我看看我能不能做点什么...... :(
【解决方案2】:

请注意,-Math.PI 和 Math.PI 在三角学中是等价的。将角度作为双倍进行比较几乎总是一个非常糟糕的主意。请参阅扩展 SO 讨论:Averaging angles... Again

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    相关资源
    最近更新 更多