1

How can I print a specific number of digits after decimal point in cpp?

As like, if I want to print more than 30 digits after decimal point while dividing 22 by 7 then how can I? Plz!

Ashish Deb
  • 25
  • 1
  • 8
  • 3
    Read a book about C++, more specifically primitive data types is what you are looking for. – ProXicT Sep 20 '16 at 22:55
  • If you want an accurate result, you'll need to use arbitrary precision libraries, or roll your own long division algorithm to compute the value to 30 decimal places. There is currently no built-in datatype in C++ that guarantees that many significant figures. – paddy Sep 21 '16 at 00:14

1 Answers1

5

Below is working code snippet of printing a certain amount of decimal points. So couple things to note: 1)Library needed is iomanip. 2) fixed means everything after decimal point 3) setprecision() means number of digits.

If you don't put fixed then it would count whole numbers before the decimal point as well. However since you want 30 AFTER decimal point you put fixed and setprecision(30).

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   double answer = 22.0/7.0;
   cout << "22.0 / 7.0 = " << fixed << setprecision(30) << answer << endl;

   return 0;
}
Omid CompSCI
  • 1,861
  • 3
  • 17
  • 29
  • 1
    using namespace std; ? – Sam Redway Sep 20 '16 at 23:03
  • 1
    nice to fix the indentation as well – Sam Redway Sep 20 '16 at 23:04
  • Gotcha! Thanks @SamRedway – Omid CompSCI Sep 20 '16 at 23:05
  • `setprecision(30)` does not mean it will make the output *precise* to 30 decimal places. –  Sep 20 '16 at 23:11
  • @RawN Yes that is why I said you need fixed. Isn't it correct with fixed? – Omid CompSCI Sep 20 '16 at 23:12
  • Double can only store 16 decimal digits. –  Sep 20 '16 at 23:13
  • Try using g++ compiler and you will see this output: 3.142857142857142793701541449991 – Omid CompSCI Sep 20 '16 at 23:13
  • 2
    @OmidCompSCI Which is plain wrong approximation after the 15th digit. The result should repeat `142857` over and over again. –  Sep 20 '16 at 23:21
  • @RawN, ah I see what you mean, I wonder why those specific garbage numbers after. – Omid CompSCI Sep 20 '16 at 23:26
  • I guess it is pretty unlikely to even go over anything 16 precision points over anyways. As in most cases it won't make a significant difference in what you're doing. Probably not gonna use C++ if your doing something like that precise.. – Omid CompSCI Sep 20 '16 at 23:27
  • @paddy what do you mean? When would it be critical to use precisely more than 16 decimal points? I can only think of Embedded systems that need exact values, but not sure if they need that precise. – Omid CompSCI Sep 20 '16 at 23:58
  • 1
    Okay [here is an example](http://stackoverflow.com/a/9943855/1553090). It's common to require much higher precision when you have error in intermediate calculations which then scales to much larger error in a final result. As for your comment on not using C++ for high-precision work, that's simply untrue. – paddy Sep 21 '16 at 00:10