Thread: Value of Pie Problem

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    pkr
    Posts
    32

    Value of Pie Problem

    I want this to display upto 100 decimal places what can I do.
    Please help me...

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
       cout <<768 * sqrt(2-sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+1)))))))));
        
        return 0;
    }
    I want this to display upto 100 decimal places what can I do

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Look up setprecision.

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Quote Originally Posted by http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_ computers
    Double precision, called "double" in the C language family, and "double precision" or "real*8" in Fortran. This is a binary format that occupies 64 bits (8 bytes) and its significand has a precision of 53 bits (about 16 decimal digits).
    You'll need to use an arbitrary precision math library.
    Last edited by User Name:; 07-22-2010 at 03:16 AM.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    pkr
    Posts
    32
    I need some 31 digits and the above code turned wrong and the right thing to use that i found was
    4*(4*arctan(1/5)-arctan(1/239))

    but how can you use this arctan function in c++ what is the header file?

    and also I used the set precision command but what data type to take I only got up to 16 decimal places using long

  5. #5
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    > I only got up to 16 decimal places using long
    Hm... I wonder why...

    > but how can you use this arctan function in c++ what is the header file?
    cmath would be my first guess.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    hi meadhikari,


    To get the value of pi to desired decimal places you need to consider two things. One is you need to use more of the half angle formula and at the same time you need to change the multiplier(which on your code is set to 768) to different figure. The other thing is as you use more of the half angle formula you need to consider your data type. The more you use half angle formula the more square root operation you do and consequently you loose precession to a considerable extent.

    It's better you change your formula or use an arbitrary precision math library as suggested above. The library helps you to display the value you calculated to the desired decimal places.

    Refer the documenation for arctan function on http://www.cplusplus.com/reference/clibrary/cmath/atan/

    By the way, it's an interesting piece of code!
    Last edited by iC++; 07-22-2010 at 05:37 AM. Reason: Added more clarification

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by iC++
    By the way, it's an interesting piece of code!
    That had to be implemented using a loop or a recursive function.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The simplest equation you can use with any floating point type:

    Code:
    template < typename Float >
    Float pi( void )
    {
    	static Float const 
    		value = Float( 4 ) * atan( Float( 1 ) );
    	return value;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Mar 2010
    Location
    pkr
    Posts
    32
    Please can anybody teach me in baby steps on arbitrary precision math library
    i do not need the files but just the name of the header file and syntax of the function to be used.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by meadhikari
    Please can anybody teach me in baby steps on arbitrary precision math library
    i do not need the files but just the name of the header file and syntax of the function to be used.
    Such libraries are non-standard, so it really depends on which one you choose to use (and consequently, you do need the files ). For example, you could use the GMP, possibly with the C++ interface.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Do this in hex and you're gold:

    http://en.wikipedia.org/wiki/BBP_alg...thm_for_.CF.80

    BBP Formula -- from Wolfram MathWorld

    Digit-Extraction Algorithm -- from Wolfram MathWorld

    Of course, since you need (only) 100 places, brute-force calculation with a high-precision library is the way to go. But if you ever need only the millionth decimal, you know what to do.
    Last edited by CodeMonkey; 07-23-2010 at 04:45 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    Quote Originally Posted by siavoshkc View Post
    That had to be implemented using a loop or a recursive function.
    I was referring to the idea of calculating the value of pi using a C++ code.

  13. #13
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    If you enter 'setprecision(25)', it gives you a lot of trailing zeroes. I think that 100 is simply too big.
    Regardless, here's the code (It gives you a blank screen, but if you make the number smaller, it'll work):

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    int main()
    {
    
       cout << fixed << showpoint
            << setprecision(100)
            << 768 * sqrt(2-sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+1)))))))));
        
       system ("pause");
       return 0;
    }

  14. #14
    Registered User
    Join Date
    Mar 2010
    Location
    pkr
    Posts
    32

    Why does this display zero any idea?

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        int n;
        cout << setprecision (100) << (12*atan(1/49) + 32*atan(1/57) - 5*atan(1/239) + 12*atan(1/110443))*4;
    	cin >> n;
        return 0;
    }

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are using integer divisions. In the world of integers 1 / 49 = 0, 1 / 57 = 0 etc.

    You have to use double literals in these calculations (at least one) to make the compiler treat this a floating point division: 1.0 / 49, 1.0 / 57

    However, no matter how elaborate the formula, and no matter how large values you give to setprecision, a double only has precision worth of 16-17 decimal digits. Therefore, it is impossible to represent pi with higher precision using the double data type. The setprecision() function only tells cout how to display the number, it does not affect the properties of a double data type.
    Last edited by anon; 07-28-2010 at 03:07 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem on - Selection on which item to purchase
    By karipap in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 06:19 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM