gpt4 book ai didi

c++ - Long double 没有提供所需的精度

转载 作者:太空狗 更新时间:2023-10-29 23:44:13 26 4
gpt4 key购买 nike

我正在读取一个文件,该文件的值以指数值的形式精确到 7 位数字

4.81591e-007

5.17968e-007

5.03954e-008

-8.30735e-007

虽然我得到的值只有 5 点精度

0.000000

0.000001

0.000000

-0.000001

-0.000002

代码如下,是

#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
#include<map>
#include<cmath>
#include<sstream>
#include<string>
#include<queue>
#include<cstdlib>
#include<cstdio>
#include<fstream>
using namespace std;
int main()
{
FILE *fp2;
fp2=fopen("output","w");
int i=0;
ifstream myFile;
myFile.open("sruthi_DATA_.txt");
vector<long double>numberlist;
long double number;
while(myFile >> number){ //
numberlist.push_back(number);
i++;
}
int j;
for(j=0;j<i;j++)
{
cout.precision(10);
cout<<numberlist[j]<<fixed<<endl;
fprintf(fp2,"%Lf\n",numberlist[j]);
}
fclose(fp);
fclose(fp2);

return 0;

}

最佳答案

尝试将值打印为固定

http://www.cplusplus.com/reference/iomanip/setprecision/
http://www.cplusplus.com/reference/cstdio/printf/

 std::cout << std::fixed << std::setprecision(10) << value << "\n";

定义:

 fixed:       the value is represented with exactly as many digits in
the decimal part as specified by the precision field
(precision) and with no exponent part.

scientific: as many decimal digits as the precision field
(precision). Finally, this notation always includes an
exponential part consisting on the letter e followed by
an optional sign and three exponential digits.

在 C 中打印:

  fprintf(fp2, "%.10f\n", value);

格式说明符的定义是:

  %[flags][width][.precision][length]specifier

Precision: For a, A, e, E, f and F specifiers: this is the number
of digits to be printed after the decimal point.

因此,要在 C++ 中实现与 C(%f) 相同的格式,您需要使用 std::fixed

关于c++ - Long double 没有提供所需的精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34049530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com