在 C++ 中获取同一公式的值与在 C# 中获取的值不同

Getting Different Values for the same formula in C++ Than in C#

本文关键字:获取 C++      更新时间:2023-10-16

我有一个来自C++应用程序的公式,我已经用 C# 实现了这个公式,但我产生了截然不同的结果。

样本纬度为:

以下是C++实现:

double lat = 0.959931088596881 //In Radians
double lon = -3.14159265358979 //In Radians
double alt = altitude;
double a = 6378137;
double e = 8.1819190842622e-2;
double N = a / sqrt(1.0 - (e * e) * (sin(lat) * sin(lat)));
x = (N+alt) * cos(lat) * cos(lon);

X 的最终结果:-3.66659e+006

下面是 C# 实现:

double lat = this.DegreesToRadians(latitude);
double lon = this.DegreesToRadians(longitude);
double alt = altitude;
double a = 6378137;
double e = 8.1819190842622e-2;
double N = a / Math.Sqrt(1.0 - (e * e) * (Math.Sin(lat) * Math.Sin(lat)));
x = (N + alt) * Math.Cos(lat) * Math.Cos(lon);

X的最终结果:-3666593.52237417

我相信我一定缺少一些简单的东西。我尝试过拉出不同的零件和零件,但无法完全弄清楚差异发生在哪里。

我有一个来自 C++ 应用程序的公式,我已经在 C# 中实现了,但我产生了截然不同的结果。

不,你会得到非常非常相似的结果 - 有不同的表示形式。

  • C++: -3.66659e+006
  • C#: -3666593.52237417

这些是相同的数字(到 6 位有效数字( - C++表示只是使用科学表示。

下面是一段 C# 演示以两种方式显示的相同值:

using System;
class Test
{
    static void Main(string[] args)
    {
        // Source code using scientific representation
        double value = -3.66659e+006;
        Console.WriteLine(value.ToString("G")); // General representation
        Console.WriteLine(value.ToString("E")); // Scientific representation
    }
}

输出:

-3666590
-3.666590E+006

现在,仅根据您的输出,我们无法判断超出前 6 位有效数字的C++结果是什么,尽管您可能可以使用printf向您展示更多详细信息。但这些值至少非常相似