setting 100 digits precision

Mar 7, 2017 at 10:56pm
I cannot get the value of 22.0/7.0 with 100 digits precision. What am I missing? Do I need a new library? If so which one and how do I link here?

1
2
3
4
5
6
7
8
9
10
  #include <iostream>
  using namespace std;
  int main()
  {
    cout << fixed;
    cout.precision(100);
    cout << "22/7 = " << 22.0/7.0 << endl;
    return 0;
   }
Mar 7, 2017 at 11:15pm
the machine has limits. I think a standard double gets 16 or so significant digits, maybe a few more. On top of that, 22/7 isn't even close to pi after like 4 digits. A tenbyte has a few more digits, I forget how many digits, but its nowhere near 100, its probably like 20-25 range. If you want big precision, youll need a non atomic variable type, which means writing your own or using a freebie large number class -- and those perform like the whales they are.

I don't know what setprecision will accept as input, but its not going to do anything past 20 or so for your output.

Mar 7, 2017 at 11:41pm
Thank you jonin.
Any suggestions, how to begin writing such files? I appreciate any related links or example files.
Mar 8, 2017 at 12:49am
> Do I need a new library? If so which one and how do I link here?

Boost multiprecision is very easy to use. Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iomanip>

int main()
{
    using namespace boost::multiprecision ;

    // floating point with precision of 100 decimal digits
    using float100 = cpp_dec_float_100 ;
    std::cout << "22/7 == " << std::fixed << std::setprecision(100)
              << float100(22.0) / 7.0 << '\n' ;

    // floating point with precision of 501 decimal digits
    using float501 = number< cpp_dec_float<501> > ;
    std::cout << "\n22/7 == " << std::fixed << std::setprecision(500)
              << float501(22.0) / 7.0 << '\n' ;
}

http://coliru.stacked-crooked.com/a/861345ce44bbfda1
Topic archived. No new replies allowed.