0

I have an algorithm to calculate pi. However, my compiler (Xcode) is only displaying up to about 5 decimal places. Is there a way to make it display the full precision with or without changing the compiler settings? I have tried looking in the compiler settings although haven't found anything to change the displayed precision. I was thinking of using std::to_string() in hopes that it would turn the full number into a string, although it didn't. This is my algorithm:

double pi15(int precision){
    double output = 0;
    
    for (int n=0;n<=precision;n++){
        output += (exp(-1.0,n%2)/exp(1024.0,n))*(-(32.0/(4.0*n+1.0))-(1.0/(4.0*n+3.0))+(256.0/(10.0*n+1.0))-(64.0/(10.0*n+3.0))-(4.0/(10.0*n+5.0))-(4.0/(10.0*n+7.0))+(1.0/(10.0*n+9.0)));
        std::cout << std::to_string(output/64) << std::endl;
    }
    return output/64;
}
NY can
  • 56
  • 1
  • 5
  • 1
    Are you aware of the precision/accuracy limits of `double`? – Mooing Duck Mar 16 '21 at 15:23
  • 1
    `to_string` is for quick conversion to string. It doesn't allow for changing the formatting. But `std::cout` is compatible with `double` and it does support changes to the format. You don't need `to_string` here. – François Andrieux Mar 16 '21 at 15:24
  • Yes, but I was wondering whether I could maybe multiply it by some power of 10 to make it a full integer and then output it as pi*some power of 10 – NY can Mar 16 '21 at 15:24
  • 1
    `to_string()` is making your life worse, not better. The stream insertion operator (`cout <<`) respect the width and precision current set on the stream, but `to_string()` is standalone. – Ben Voigt Mar 16 '21 at 15:24
  • https://en.cppreference.com/w/cpp/io/manip/setprecision – Ben Voigt Mar 16 '21 at 15:25
  • 2
    @NYcan You can't do that with floating point type. For why, see: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – NathanOliver Mar 16 '21 at 15:25
  • 1
    Multiplying a floating point value makes it larger, but doesn't change the precision or accuracy. In fact, that usually makes the accuracy worse. – Mooing Duck Mar 16 '21 at 15:26
  • I tried using ``std::cout`` without ``std::to_string()`` but it displayed an even lower precision – NY can Mar 16 '21 at 15:26
  • 1
    **Always** remember that the "precision" of a floating-point number is entirely dependent on its internal representation – not the algorithm that converts it to a printable string. And, "the number of *significant digits"* is dependent on the exact mathematical operation that is being performed. – Mike Robinson Mar 16 '21 at 15:26
  • 1
    @Nathan: Sure you can, every floating-point value has a finite number of digits in its decimal representation. Doesn't make it a good idea. – Ben Voigt Mar 16 '21 at 15:26
  • Be very sure that you understand the "mantissa and exponent" that is used to represent floating-point values. It has many subtle implications. *"Garbage In, Garbage Out"* is very easy to do! – Mike Robinson Mar 16 '21 at 15:27
  • You could adapt [this code](https://www.codeproject.com/Articles/11692/Calculate-pi-to-one-million-decimal-places) that calculates π to an arbitrary precision, such as a million decimal places. – Eljay Mar 16 '21 at 15:29

0 Answers0