2

I am reading from a file which has values to a precision of 7 digits in the terms of exponential values

4.81591e-007

5.17968e-007

5.03954e-008

-8.30735e-007

Though the values I am getting is with only a 5 point precision

0.000000

0.000001

0.000000

-0.000001

-0.000002

The code is as follows and is

#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;

}
Community
  • 1
  • 1
user3481478
  • 387
  • 1
  • 3
  • 19

3 Answers3

5

Try printing the values as fixed

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

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

Definition:

 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.

For printing in C:

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

The definition of the format specifier is:

  %[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.

So to achieve the same format in C++ as you get with C(%f) you need to use std::fixed.

Martin York
  • 257,169
  • 86
  • 333
  • 562
2

If you want fprintf to print ten digits after the dot you have to write %.10f in the formatting string. The precision of cout does not influence fprintf.

And you have to pass the IO manipulator fixed to cout before the numbers for it to take effect. Otherwise the first number you print will not be in fixed precition.

cout << fixed << numberlist[j] << endl;
maddin45
  • 737
  • 7
  • 17
2

To see the number in C in exponential notation (scientific) with 5 places after the decimal point use "%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.

To see a FP value in Printf width specifier to maintain precision use

#include <float.h>

fprintf(fp2,"%.*Le\n", LDBL_DECIMAL_DIG - 1, numberlist[j]);
// or simply in hexadecimal notation
fprintf(fp2,"%.*La\n", numberlist[j]);
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256