在C ++中使用大整数部分处理双精度

编程入门 行业动态 更新时间:2024-10-26 08:26:34
本文介绍了在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 ++中使用大整数部分处理双精度

本文发布于:2023-10-28 13:41:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536796.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   精度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!