Thread: Using a double number in while statement

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23

    Using a double number in while statement

    I am working on a problem that outputs how many terms are used to determine the amount of terms to find different amounts of Pi, such as 3.0, 3.1, 3.14. Could anyone help me on this code, especially with using a double number in a while statement. the forumla is:
    Pi = 4 x (1-1/3+1/5-1/7+1/9-1/11+1/13....)


    Code:
    #include <iostream>
    
    #include <cmath>
    
    #include <iomanip>
    
    using namespace std;
    
    double pif(double q);
    
    int terms=0;
    
    int main()
    
    {double q=3.1;
    
    cout << "Pi Value Terms\n--------------------------\n";
    
    cout << fixed << setprecision(2);
    
    cout << pif(q)<< " "<< terms << endl; 
    
    
    
    
    return 0;
    
    }
    
    
    double pif (double q)
    
    {
    int sign = -1;
    
    double n=1.0;
    
    
    double pi=0;
    
    while (what would I put here to compare pi to a double?)
    
    {
    
    pi = sign *(1/(2.0*n-1.0);
    
    sign = -sign
    
    n++;
    
    terms++;
    
    }
    
    pi*=4.0;
    
    return pi;
    
    }
    Edit: Didn't check code, had to delete a segment to show where I needed assistance.
    Changed the sign variation from a power to sign=-sign.
    Last edited by Daesom; 10-31-2006 at 12:34 PM. Reason: Edit: Didn't check code, had to delete a segment to show where I needed assistance.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    35
    if i remember correctly....

    doubles aren't 100% accurate. i don't think you can compare them conditionally. if you know the precision of the numbers you can multiply by 10's to make them whole numbers, compare the two and then divide...this is only my theory..i've never actually done it.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    Thank you computation, I think you are on the right track with that one actually. Let me think about that, if anyone else has any other ideas, please post them.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. don't use pow to calculate coeffitiant that can be +1 or -1
    use variable sign that can be set to sign=-sign on each iteration
    2. the formula is actually a sum - don't forget to use + in your calculations
    3. every time you add something to your value, the resulting error is less then the last member added so just wait when this value your add to the pi is less then desired accuracity (don't forget to take in the account the sign and the 4.0 coeffitient)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    Quote Originally Posted by vart
    1. don't use pow to calculate coeffitiant that can be +1 or -1
    use variable sign that can be set to sign=-sign on each iteration
    2. the formula is actually a sum - don't forget to use + in your calculations
    3. every time you add something to your value, the resulting error is less then the last member added so just wait when this value your add to the pi is less then desired accuracity (don't forget to take in the account the sign and the 4.0 coeffitient)
    Sorry I am helping my cousin with this code, I usually use the sign = -sign when doing something like this, just didn't notice that in his code. Thanks for pointing it out, correction was made.

  7. #7
    Registered User Xeridanus's Avatar
    Join Date
    Oct 2006
    Location
    QLD, Aussieland
    Posts
    11
    perhaps a function that truncates a double to a certain number of decimal points by using the multiply and then returns the truncated double.

    double truncDoub(double toBeTrunced, int degreeOfAccuracy);

    you could use the sprintf and sscanf functions and a temp buffer to change it to an int. i know it's messy but i have done it before without any problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected declaration or statement at end of input
    By irelandmc in forum C Programming
    Replies: 5
    Last Post: 06-23-2009, 02:41 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Rectangular Approximation Program Help
    By Noah in forum C Programming
    Replies: 4
    Last Post: 03-15-2006, 02:23 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM