Value of Pie Problem

This is a discussion on Value of Pie Problem within the C++ Programming forums, part of the General Programming Boards category; I want this to display upto 100 decimal places what can I do. Please help me... Code: #include <iostream> #include ...

  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,231
    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,600
    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:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  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
    20,969
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    891
    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).

Page 1 of 2 12 LastLast
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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21