在C ++中使用大整数部分处理双精度 [英] Handling doubles with large integer parts in C++

查看:140
本文介绍了在C ++中使用大整数部分处理双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C ++中处理具有大整数部分(例如515876.12//5117789.22)的亚米级坐标,但是四舍五入时遇到了问题:

I need to handle sub-metre coordinates in C++ which have large integer parts (e.g. 515876.12 // 5117789.22), but I'm having issues with rounding:

double inUTMX = 560351.12 is displayed as 560351
double inUTMY = 5113570.22 is displayed as 5.11357e+06

我可以在必要时将坐标标准化以进行处理(例如/1e5),但首先需要通过命令行读取亚米级坐标.问题是它们总是四舍五入.

I can normalise the coordinates for processing if necessary (e.g. /1e5), but I need to read-in the sub-metre coordinates via command line in the first place. Trouble is they always get rounded.

在C ++中,有没有一种很好的方法来处理具有大整数值的双精度数?

Is there a neat way to deal with doubles that have large integer values in C++?

(在Python中对其进行了尝试,它以浮点数的形式存储了整个精度,只是想知道我要去哪里出错了.)

(Tried it in Python it stores the entire precision fine as a float, just wondering where I'm going wrong.)

任何想法/指针都值得赞赏.

Any ideas / pointers much appreciated.

推荐答案

您可以使用std::setprecision修改double的流式传输方式.

You can modify how doubles are streamed by using std::setprecision.

示例:

#include <iostream>
#include <iomanip>

int main () {
   double inUTMX = 560351.12;
   double inUTMY = 5113570.22;

   std::cout << std::setprecision(20) << inUTMX << std::endl;
   std::cout << std::setprecision(20) << inUTMY << std::endl;

  return 0;
}

输出:

560351.11999999999534
5113570.2199999997392

这篇关于在C ++中使用大整数部分处理双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆