我失去了十进制GPS坐标的精度

I am losing precision of decimal GPS coordinates

本文关键字:坐标 精度 GPS 十进制 失去      更新时间:2023-10-16

在c++程序中存储GPS坐标的最佳变量类型是什么?我写了下面的代码只是为了尝试一下,不管我选择什么变量类型,问题仍然存在。

#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
class Coordinate {
    public:
            float xcoor, ycoor;
            int radius;
            void set_values (float, float, int);
};
void Coordinate::set_values (float x, float y, int r){
    xcoor = x;
    ycoor = y;
    radius = r;
}
int main (){
    Coordinate test;
    test.set_values (32.682633, -117.181554, 50);
    cout << "coordinates: (" << test.xcoor << "," << test.ycoor << ")n";
    return 0;
}

下面的代码输出值:(32.6826, -117.182)

显然我失去了大量的精度,但是我有办法保持它吗?我以前没有做过GPS坐标,也找不到有类似问题的人。谢谢你的帮助。

在算术中使用浮点变量可能会导致精度的损失,但据我所知,您没有对坐标进行任何计算。

我怀疑你是假设std::cout输出浮点变量具有完全精度,这不是默认情况下的情况。

double test = 1.23456789;
std::cout.precision(10);
std::cout << "test: " << test << std::endl; // prints "test: 1.23456789"

请参阅这个问题,以及ostreams的文档以获取更多信息

据我所知,它只打印6位数字。尝试setw或setprecision。使用前者,您可以设置一个常数来打印数字或字符(这在对齐时很方便),后者设置程序打印的数字数量。

对于浮点数,你不应该丢失这些数字中的任何数据