-2

I'm trying to get an output to file such as this:

1.11111
11.1111
111.111
1111.11
111111

In other words, I try to set the significance of my output, instead of my precision. I've tried

oFile <<setprecision(6);

and

fprintf(oFile, "%6f", varName);

To no avail.

Any help is much appreciated.

Cheers

EDIT: Sorry for the incomplete question. Here's a Minimal, Complete, and Verifiable example:

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;
int main() {
    ofstream oFile("output.dat");
    float var1 = 10.9993;
    float var2 = 11;
    oFile << var1 << endl;
    oFile << var2 << endl;
    oFile << std::setprecision(6) << var2 - var1 <<endl; //10.9993 - 11 = -0.000699997
    oFile.close();
    /* 
     * output
     * 10.9993
     * 11
     * 0.000699997
    */
    FILE * oFile2;
    oFile2 = fopen("output2.dat","w");
    fprintf(oFile2, "%6f \n%6f \n%6f \n",var1, var2, var2-var1);
    fclose(oFile2);
    /*
     * output
     * 10.999300 
     * 11.000000 
     * 0.000700 
    */
}

So I want to have at most 6 significant digits in every case, regardless of the precision, i.e:

10.9993
11 or 11.0000 that does not matter
0.00070

OK, so I've ended up multiplying each variable out of decimal points, subtracted and divided back into decimal points. This seems to work. Crazy that there does not seem to be a functionality to set significance in C++.

BdB
  • 471
  • 5
  • 18

2 Answers2

0

I am not 100% sure if this is what you asked for, but I managed to get the result

1.11111
11.1111
111.111
1111.11
11111.1
111111

with the following code :

double test=1.11111;
for(int j=0;j<6;j++)
{
    cout<<test<<endl;
    test*=10;
}

No need for iomanip or changing any precision for this case.

A. Andevski
  • 437
  • 1
  • 5
  • 17
  • What happens if you add a few digits to each double? – laune Apr 22 '14 at 15:43
  • Then iomanip will be needed. You simply count the total number of digits and use that in cout< – A. Andevski Apr 22 '14 at 17:00
  • This does not limit the number of significant digits. Required is to output (e.g.) `1.23456` from, say `1.23456789`. And how do you propose to "count the total number of [decimal] digits" in a double that's the result of some computation? – laune Apr 24 '14 at 05:19
0

Here is code that sets the double values with more digits than you want to be displayed, and two techniques to get what you want.

#include <iostream>
#include <iomanip>
#include <stdio.h>

int main(){
  double d[6] = { 1.1111123, 11.111123, 111.11123,
                  1111.1123, 11111.123, 111111.23 };

  for( int i = 0; i < 6; i++ ){
    std::cout << std::setprecision(6) << d[i] << std::endl;
  }

  for( int i = 0; i < 6; i++ ){
    char buf[20];
    sprintf( buf, "%6g", d[i] );
    std::cout << buf << std::endl;
  }
  {
    char buf[20];
    sprintf( buf, "%6g", d[1]-d[0] );
    std::cout << buf << std::endl;
  }
}

Can be improved, but it shows the basic ingredients.

Later

d[0] = 10.9993;
d[1] = 11;
d[2] = d[1] - d[0];
for( int i = 0; i < 3; i++ ){
  std::cout << std::setprecision(6) << d[i] << std::endl;
}
std::cout << std::setprecision(6) << d[1]-d[0] << std::endl;

for( int i = 0; i < 3; i++ ){
  char buf[20];
  sprintf( buf, "%6g", d[i] );
  std::cout << buf << std::endl;
}

This produces

10.9993
11
0.0007
0.0007
10.9993
    11
0.0007
0.0007
laune
  • 31,114
  • 3
  • 29
  • 42
  • When I adapt your code to my use case, it does not work when I subtract the values (var2 - var1). – BdB Apr 24 '14 at 17:17
  • Your implementation works now. It seems I should use doubles instead of floats... don't know why though... However when I use this functionality in the context of my code it breaks down again. – BdB Apr 24 '14 at 17:36