如何在C++中读取位数较多的浮点数?

发布于 2024-10-07 05:02:43 字数 818 浏览 0 评论 0原文

当我尝试从文件中读取浮点数时遇到问题。我需要读取一行中给出的 3 个浮点数,如下所示:
v -30.50889491515995 -31.95820181187489 0
(我正在从 Rhinoceros 的 .obj 文件进行解析器)

这是我的代码(在此之前我读取一个字符串以查看是否是“v”):

fstream f(name.c_str());  
...  
f>>p.x>>p.y>>p.z;  

name 是之前从标准输入读取的字符串。
P是一个结构体:

typedef struct Point{  
double x;  
double y;  
double z;  
}Point;  

问题是读取到的数据是:
-30.5089 -31.9582 0
而不是
-30.50889491515995 -31.95820181187489 0
它四舍五入到小数点后 4 位,我不想要这样!

我尝试使用 fscanf 进行读取,但无法向其发送 fstream 对象。像这样的事情:

fscanf(f,"%f %f %f",p.x,p.y,p.z);

我也尝试过这个,但它不起作用:

f>>setprecision(10)>>fixed>>p.x>>p.y>>p.z;

关于如何避免这种情况的任何想法?我需要更精确的顶点坐标!

非常感谢。

I'm having issues when I try to read a float number from a file. I need to read 3 float numbers given in a line like this:
v -30.50889491515995 -31.95820181187489 0
(I'm doing a parser from a .obj file from Rhinoceros)

Here is my code (before this I read a string to see if is a 'v'):

fstream f(name.c_str());  
...  
f>>p.x>>p.y>>p.z;  

name is a string readed before from standard input.
P is a struct:

typedef struct Point{  
double x;  
double y;  
double z;  
}Point;  

The problem is that the data readed is:
-30.5089 -31.9582 0
instead of
-30.50889491515995 -31.95820181187489 0
It rounds at 4 decimals, and I don't want that!

I tried to read with fscanf but I can't send it a fstream object. Something like this:

fscanf(f,"%f %f %f",p.x,p.y,p.z);

I also tried this, but it didn't work:

f>>setprecision(10)>>fixed>>p.x>>p.y>>p.z;

Any ideas of how to avoid this? I need more precision in the vertex coordinates!

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平定天下 2024-10-14 05:02:43

C++ 总是以全精度输入数字,但您需要指定显示的精度(即输出值时):

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
    std::istringstream iss("30.50889491515995 -31.95820181187489 0");

    float x, y, z;

    if (iss >> x >> y >> z)
        std::cout << std::setprecision(10) << std::fixed
                  << x << ' ' << y << ' ' << z << '\n';
}

输出:

30.5088939667 -31.9582023621 0.0000000000

C++ always inputs the numbers with full precision, but you need to specify the precision for display (i.e. when outputting the values):

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
    std::istringstream iss("30.50889491515995 -31.95820181187489 0");

    float x, y, z;

    if (iss >> x >> y >> z)
        std::cout << std::setprecision(10) << std::fixed
                  << x << ' ' << y << ' ' << z << '\n';
}

output:

30.5088939667 -31.9582023621 0.0000000000
灰色世界里的红玫瑰 2024-10-14 05:02:43

set precision 应该做你想做的事情。你说这不起作用——你从中得到了什么?

您是否尝试在调试器中运行它?是否有可能是输入不正确?

setprecision should do what you are wanting to do. You said it didn't work--what did you get out of it?

Did you try running it in a debugger? Is it possible that it is not being input properly?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文