c++ - 如何在 C++ 中计算 10 位小数精度的 double 变量

标签 c++ visual-studio-2008-sp1

我要计算精度为 10 位小数的 double 类型。当然,我希望结果precesion 具有相同的10 位小数精度。但是,它不起作用,在 VS2008 中只能保留 6 个小数点。有什么理由或想法吗?

double dMag = 10;
double dPixelSize = 4.4;
double PixelWidth = 1024/2;
double PixelHeight = 768/2;
double umtomm = 1000;

double origin_PosX = 813.227696;
double origin_PosY = 676.195748;

double PosX = (origin_PosX - PixelWidth) * dPixelSize / dMag / umtomm;
double PosY = (origin_PosY - PixelHeight ) * dPixelSize / dMag / umtomm;

如果我检查 PosX 和 PosY,则结果分别为“0.132540、0.128566”。我预计结果分别为“0.13254018624000002、0.12856612912”。

谢谢

最佳答案

据我所知,您混淆了一些事情...

double 的精度不仅仅是小数点后 6 位。我强烈推荐阅读this wikipedia article这解释了底层 double 数据类型如何存储您的数字并解释了它的精度。

打印的精度默认限制为小数点后 6 位。为了解决这个问题,您需要学习如何使用 format string .

在你的评论中你提到使用

tmp1.Format("%f,%f\r\n", PosX, PosY); file.Write(tmp1,lstrlen(tmp1));

打印字符串。

尝试将此行更改为

tmp1.Format("%.10f,%.10f\r\n", PosX, PosY); file.Write(tmp1,lstrlen(tmp1));

你会看到它会在小数点后打印 10 位数字。

关于c++ - 如何在 C++ 中计算 10 位小数精度的 double 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20231621/

相关文章:

c++ - std::AVX 内在函数数组

c# - 如何使 [DebuggerDisplay] 尊重继承的类或至少使用集合?

visual-studio-2008-sp1 - 强制 VS2008 发出类似 "warning: comparison between signed and unsigned integer expressions"的 GCC 警告

c++ - 我编译了最新版本的 GCC。如何将 CLion 指向它的位置?

c# - 为什么回发后我的数据丢失了

visual-studio-2008 - 如何将Visual Studio 2008连接到Team Foundation Service和Visual Studio Online?

visual-studio-2008-sp1 - C# 自定义 PrintDialog PInvoke DevMode 问题

c++ - 如何引导 OGDF 中的边?

c++ - std::lock_guard 可以中断吗?

c++ - 转移 boost::asio::socket 堆栈变量的所有权