【问题标题】:Long double not giving the required precisionLong double 未提供所需的精度
【发布时间】:2015-12-02 17:47:15
【问题描述】:

我正在从一个以指数值表示的精度为 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;

}

【问题讨论】:

  • 因为%Lf 字面意思是一个精度高达六点的数字。改用标准%f
  • 这是使用 C 函数 frpintf() 所以添加了 C 以获得适当的受众。
  • 嗯,这是为了写入文件。当我打印到终端时,它给出相同的值。
  • fprintf 中尝试%Lg%.20Lg 而不是%Lf - 这有帮助吗?
  • 如果你读的是科学记数法数字但写的是定点数,这是预期的行为。

标签: c++ c file floating-accuracy


【解决方案1】:

尝试将值打印为fixed

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

【讨论】:

    【解决方案2】:

    要查看C 中小数点后5 位的指数符号(科学)中的数字,请使用"%e"

    fprintf(fp2,"%.5Le\n",numberlist[j]);
    fprintf(fp2,"%.*Le\n", 5, numberlist[j]);
    fprintf(fp2,"%.Le\n",numberlist[j]);  // If not precision give, defaults to 6.
    

    要查看Printf width specifier to maintain precision 中的 FP 值,请使用

    #include <float.h>
    
    fprintf(fp2,"%.*Le\n", LDBL_DECIMAL_DIG - 1, numberlist[j]);
    // or simply in hexadecimal notation
    fprintf(fp2,"%.*La\n", numberlist[j]);
    

    【讨论】:

      【解决方案3】:

      如果您希望fprintf 在点后打印十位数字,您必须在格式化字符串中写入%.10fcout 的精度不影响fprintf

      并且你必须在数字之前将 IO 操纵器 fixed 传递给 cout 才能生效。否则,您打印的第一个数字将不是固定精度。

      cout << fixed << numberlist[j] << endl;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-28
        • 2013-06-27
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-31
        • 1970-01-01
        相关资源
        最近更新 更多