Question about floats

Hey, I am interested to know how I could get numbers to be equal to two significant digits after a point like 1.00, 0.50, 0.33, 0.25, 0.20 0.17 when dividing 1 by x where x is increasing by one in each for loop. The code I wrote is below. The problem is that when I use float, double or long double I get extensions after a dot such as 0.333333 or just one digit after dot like 0.5 or 0.20. So, basically I want two significant digits after a dot for my program.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
  cout << "\t" << "*** Table ***" << endl << endl;
  cout << "x" << "\t" << "x^2" << "\t" << "x^3" << "\t" << "1/x" << endl;
  for(long double i=1, y=1; i<=10 && y<=10; i++, y++)
  {
    cout << i << "\t" << i*i << "\t" << i*i*i << "\t" << 1/i << endl;
  }
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <iomanip>

int main()
{
    // https://stdcxx.apache.org/doc/stdlibug/28-3.html#2832
    std::cout << std::fixed << std::setprecision(2) ;

    for( int n = 1 ; n < 11 ; ++n )
        std::cout << " 1/" << n << " == " << 1.0/n << '\n' ;
}
if you need to process a lot, you might try moving it into an integer:

int x = afloat*100;
int y = anotherfloat*100;
if (x == y) ...
int result = somthing;
...
finaldouble = result/100;

you can add some rounding if you feel it is the better way, and cast it, this is just the quick example.
Last edited on
JLBorges, thanks. But I would like for the precision to go only for fourth column not all of them. Any ideas for one column only?
> But I would like for the precision to go only for fourth column not all of them.

Only the last columns needs to be a floating point value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>

int main()
{
    std::cout << "      *** Table ***\n\n"
              << " x   x^2     x^3    1/x\n"
              << "-----------------------\n"
              << std::fixed << std::setprecision(2) ;

    for( int n = 1 ; n < 11 ; ++n )
    {
        std::cout << std::setw(2) << n << std::setw(6) << n*n
                  << std::setw(7) << n*n*n << "    " << 1.0/n << '\n' ;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
using namespace std;

//=====================================================================

struct fmt                                       // general format object
{
   char form;
   int width, precision;
   char ch;
   fmt( char f, int w, int p = 6, char c = ' ' ) : form( f ), width( w ), precision( p ), ch( c ) {}
};

ostream& operator << ( ostream &strm, fmt o )    // format manipulator
{
   char t = tolower( o.form ), c = o.ch;
   int w = o.width, p = o.precision;
   if      ( t == 'i' ) strm                << setw( w );                         // integer
   else if ( t == 'f' ) strm << fixed       << setw( w ) << setprecision( p );    // fixed-point
   else if ( t == 'e' ) strm << scientific  << setw( w ) << setprecision( p );    // scientific
   else if ( t == 'a' ) strm                << setw( w );                         // text
   else if ( t == 's' ) for ( int i = 0; i < w; i++ ) strm << ' ';                // space
   else if ( t == 'c' ) for ( int i = 0; i < w; i++ ) strm << c;                  // character
   return strm;
}

//======================================================================

int main()
{
    const int w = 8;
    #define W << setw( 8 ) <<
    #define NL << '\n'

    cout W "x" W "x^2" W "x^3" W "1/x" NL << string( 4 * w, '-' ) NL;

    for( int n = 1 ; n < 11 ; ++n ) cout W n W n * n W n * n * n << fmt( 'f', w, 2 ) << 1.0 / n NL;
}

       x     x^2     x^3     1/x
--------------------------------
       1       1       1    1.00
       2       4       8    0.50
       3       9      27    0.33
       4      16      64    0.25
       5      25     125    0.20
       6      36     216    0.17
       7      49     343    0.14
       8      64     512    0.13
       9      81     729    0.11
      10     100    1000    0.10

JLBorges thanks! I figured the rest out!
Last edited on
Topic archived. No new replies allowed.