我失去了十进制GPS坐标的精度
I am losing precision of decimal GPS coordinates
在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。使用前者,您可以设置一个常数来打印数字或字符(这在对齐时很方便),后者设置程序打印的数字数量。
对于浮点数,你不应该丢失这些数字中的任何数据
相关文章:
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 如何计算具有指定类型的表达式的相对精度和绝对精度
- 如何打印boost多精度128位无符号整数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 2d(3d)坐标的哈希映射(即双精度向量)
- 我失去了十进制GPS坐标的精度