Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Im writing a progarm to calculate PI and my problem is c++ rounds everything in division so i have an endless answer of "3" ive done seprecsision for cout statement and that is fine but i need to fix the division my equation is PI = 4-4/3+4/5-4/7+4/9-4/11+4/13-4/15...

Here is my actual math part of my script:

Cal = 4/Odd;

Odd += 2;

Calc -= Cal;

Cal = 4/Odd;

Odd += 2;

Calc += Cal;

Pi = Calc;

This is in a for loop then it saves and repeats

Here is my actual math part of my script:

Cal = 4/Odd;

Odd += 2;

Calc -= Cal;

Cal = 4/Odd;

Odd += 2;

Calc += Cal;

Pi = Calc;

This is in a for loop then it saves and repeats

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

as rstaveley say make any number of them float

or write it

cal = (float) 4/Odd

1) go to extended instead of double. This increases the number of bits used for the mantissa.

Then you have the problem that the mantissa is limited and the value won't change the exponent of the number 3.1415e0: At some place the number calc + 4/2n+1 can't be distinguished from calc + 4/2n+1 - 4/2n+3 with the chosen precision of your var. To reach a higher precision you have to remove the leading. Now it helps to remove some known digits of pi and so changing the exponent of it. Thereby you increase the available bits for the mantissa to distinguish these numbers.

I hate to be the bearer of bad news here but you're not going to live long enough to push this to the max. PI is a transcendental irrational number and has an infinite number of digits. Using computers PI has been calculated to many millions of digits but you're unlikely to even approach that using your methods.

http://projectpi.sourceforge.net/

If you're going to try and accomplish something, at least use a technique that is potentially usefule.

Here is my script

//Pi Calculation Software Alpha

//START ODD VALUE : 3

//START CALC VALUE : 4

//:Placed in files:

#include <iostream.h>

#include <fstream.h>

#include <stdlib.h>

#include <math.h>

#include <iomanip>

char Buffer[100];

int Odd;

int Oddc;

double Cal;

double Calc;

double Pi;

int i;

int pre = 52;

void LoadFiles();

void DoIt();

void ShowIt();

void SaveIt();

void main ()

{

LoadFiles();

system("pause");

DoIt();

}

void LoadFiles()

{

ifstream ODDB ("Odd.txt");

ODDB.getline(Buffer,100);

Odd = atoi(Buffer);

ODDB.close();

ifstream CalcB ("Calc.txt");

CalcB.getline(Buffer,100);

Calc = atof(Buffer);

CalcB.close();

}

void DoIt()

{

//Calculates and saves every 10000 calculations

for (i = 0; i <= 5000; i++)

{

Cal = (float) 4/Odd;

Odd += 2;

Calc -= Cal;

Cal = (float) 4/Odd;

Odd += 2;

Calc += Cal;

Pi = Calc;

ShowIt();

}

SaveIt();

DoIt();

}

void ShowIt()

{

cout << "Calc: ";

cout << Odd/2-1;

cout << " PI: ";

cout << setprecision(pre) << Pi;

cout << "\n";

}

void SaveIt()

{

ofstream PiC ("Pi.txt");

PiC << setprecision(pre) << Pi;

PiC.close();

ofstream ODDC ("Odd.txt");

ODDC << Odd;

ODDC.close();

ofstream CalcC ("Calc.txt");

CalcC << setprecision(pre) << Calc;

CalcC.close();

}

long double is a 80 bit floating point number

double is a 64 bit float,

and

float is 32 bit float.

In your above example you mix new and old style STL headers:

#include <iostream.h> // old style

#include <fstream.h> // old style

#include <iomanip> // new style

This can be dangerous. The preferred way should be only the new style (without the .h)

#include <iostream>

#include <fstream>

#include <iomanip>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

Write 4.0 to get a division on a double.

e.g.

Cal = 4.0/Odd;

Make sure all of your variebles are doubles for double precision.