# Thread: Using a double number in while statement

1. ## 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.

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

5. 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.

6. 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.