Extra Digits of Precision

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emaghero
    New Member
    • Oct 2006
    • 85

    Extra Digits of Precision

    Does anyone know how to ouput extra digits of precision in C++?

    I am using Microsft Visual Studio to write my C++ code and I want to get at least 20 - 30 digits of precision on screen and for outputting to a txt file.

    At the moment I use printf("%16.24l f\n", number);

    I would like to know if there is a C++ equivalent?

    emaghero.
  • ssharish
    New Member
    • Oct 2006
    • 13

    #2
    Well there is member function with the cout object called a precision which actually set or gets the precision. The following shows u on how to use it

    Code:
    cout.precision(5); // Sets the precsion to 5
    some sample code using the precision

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	double value = 1.23652458759653;
    
    	cout.precision(20);
    	cout<<"The double printed with 5 precision -> "<<value<<endl;
    
    	cin.get();
    	return 0;
    }
    
    /*my output
    The double printed with 5 precision -> 1.2365245875965301
    */
    ssharish

    Comment

    • emaghero
      New Member
      • Oct 2006
      • 85

      #3
      That looks like it will do the business.

      Thanks very much

      emaghero

      Comment

      • vninja
        New Member
        • Oct 2006
        • 40

        #4
        if you want to be lazier you could just use
        setprecition(N)
        before the value eg

        cout << "the value " << setprecition(N) << value;

        use N as a global const or as a var to make it easier on yourself

        Comment

        • emaghero
          New Member
          • Oct 2006
          • 85

          #5
          Originally posted by vninja
          if you want to be lazier you could just use
          setprecition(N)
          before the value eg

          cout << "the value " << setprecition(N) << value;

          use N as a global const or as a var to make it easier on yourself
          Thanks very much. I think I like that one better.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by emaghero
            I am using Microsft Visual Studio to write my C++ code and I want to get at least 20 - 30 digits of precision on screen and for outputting to a txt file.
            There is not much point specifying 20-30 bit precision when the double type (which is the most precise type that MSVC++ supports) only holds 15 bits of precision. You will just end up with 5 - 15 0s at the end of your number.

            Comment

            Working...