pi calculations

This is a discussion on pi calculations within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main () { int pi1,pi2,count, x=-1, var; while (count<1000000000){ cout.precision(7); pi1=1+x(1/var); var=var+2; x=x*-1;} ...

  1. #1
    nbice
    Guest

    Angry pi calculations

    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
    	int pi1,pi2,count, x=-1, var;
    	while (count<1000000000){
    		cout.precision(7);
    		pi1=1+x(1/var);
    		var=var+2;
    		x=x*-1;}
    	if (pi1==pi2){
    		count ++;}
    	return 0;
    }
    this is what i have and i can't figure out how to utilize the cout.precision(n). if you have any information on a code please i would appreciate your help a lot. i have to get calculations using double and float. using these calculations of pi. any help is very much appreciated

    Leibniz / 4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15 + 1/17 - ...
    Euler ^2 / 6 = 1 + 1/2^2 + 1/3^2 + 1/4^2 + ...

    the assignment is at http://cs.ua.edu/114/assignments/program2.htm


    Code tags added by Kermi3.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Could you tell us what you are do with this program? You can use manipulator to format your code.

    Mr. C.

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [ code ] at the beginning of your code and [ /code ] at the end, only without the spaces. More information on code tags may be found at the link on my signature.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try something like

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    int main()
    {
       double pi = 3.14152;
       cout.setprecision(3);
       cout << pi;
    }

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Does kermi3 really exist? I mean I am having deja vu here I saw the same post on another thread! Ah what is this world going to?

    kermi, there is no need to dignify this post with a response I am just kidding.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    52
    mean I am having deja vu here I saw the same post on another thread!
    Uhh oh. We all knows what means...a glitch in the matrix!
    Turn no more aside and brood
    Upon love's bitter mysteries
    For Fergus rules the brazen cars...

  7. #7
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595



    ...Ands someone's watching too much Fox.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i have to get calculations using double and float.
    Then declare your variables as double or float.

    double pi1, pi2, x=-1.0, var;
    int count;

    In your calculation, to do multiplication, use *.
    Also you need to initialize both count and var.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pi - Hm, somethign is not right here.
    By MadnessRed in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2008, 02:07 PM
  2. Pi Calculation
    By EvilGuru in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 05:25 AM
  3. Pi and the standard library
    By FOOTOO in forum C Programming
    Replies: 7
    Last Post: 04-15-2005, 12:23 PM
  4. C for PI
    By Lynux-Penguin in forum C Programming
    Replies: 13
    Last Post: 04-28-2002, 08:37 PM

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