在 C++ 中获取同一公式的值与在 C# 中获取的值不同
Getting Different Values for the same formula in C++ Than in C#
我有一个来自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
向您展示更多详细信息。但这些值至少非常相似。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 具有默认值的引用获取函数
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 在c++中获取大文件的大小