Hi everyone.
I’m currently working on a very simple little project in C++ for a experienced programmers portfolio application for sydney uni. I decided to create a little tool in C++ that generates pi to a certain level of precision using a formula I derived from something I found in one of my Maths text books:
[FONT=Arial]π/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 …[/FONT]
I turned the series into an equation (used an equation editor cause typing an equation like this is too hard and pointless cause no one would be able to/want to read it).
[FONT=Arial]
[/FONT]
I then made my program (really basic C++ program, but thats because I suck at C++)
#include <iostream> // No comment needed
using namespace std; // same as ^
// My functions: gen_pi is the main function that creates pi to the level of detail that is specificed in parameters
// pow_index is my own function for raising a number to power of an index eg 2^3 = 8 = pow_index(2,3)
float gen_pi(int);
float pow_index(float,int);
// Variable for storing user input
int input = 1;
// Main function
int main()
{
// If the user enters 0 then the program will quit here
while(input != 0)
{
// Displayes the value of pi, first run of the program displayes pi to the precision of 1
cout << "PI = " << gen_pi(input);
// Blah blah blah
cout << "
This program is a tool for generating pi to certain levels of percision"
<< "
Please enter the level of detail you wish to see pi generated to"
<< "
Must be higher than 1, after about 20000 there is very noticable delay, 0=quit"
<< "
Enter level of detail:";
// Enter level of detail
cin >> input;
// Clear screen
system("CLS");
}
return 0;
}
// Generate Pi function
float gen_pi(int n)
{
// Loop checks if n (precision) = 1, if so then just return 1.
if(n!=1)
{
// Main part of the gen_pi function, this is the code version of my equation
float pi = 0;
for (float i=1;i<=n;i++)
{
pi = pi + (-1 * pow_index(-1,i) * (1 / (2*i - 1)));
}
pi = 4 * pi;
// Return value of pi.
return pi;
// As explained above, if n=1, then just return 1
}
else
{
return 1;
}
}
// My 'raise number to index' function, simple stuff and works perfect. :D
float pow_index(float base, int index)
{
float answer = 1;
for(int i=1;i<=index;i++)
{
answer = answer * base;
}
return answer;
}
OK… and now the problem…
The value my program is returning is only to 5 decimal places… the value is correct and the program works fine, it’s just 5 decimal places isn’t much. I need at least 9 or so, but would be happy with 50+ even. So my question is how to make it display more precision/decimal places in the final value.
Example of what I mean:
[FONT=Arial]π[/FONT] = 3.14159 (3.141592654…)
Example output from my program:
precision of 1 → [FONT=Arial]π[/FONT] = 1
precision of 2 → [FONT=Arial]π[/FONT] = 2.66667
precision of 3 → [FONT=Arial]π[/FONT] = 3.46667
precision of 100 → [FONT=Arial]π[/FONT] = 3.13159
precision of 40000 → [FONT=Arial]π[/FONT] = 3.14157
What I wanted is more decimal places. So something like [FONT=Arial]π[/FONT] = 3.13159015684518946519816518911546484845…(not a real generated value of pi, I just mashed the keyboard :crazy:) you know, lots more! As many as possible!
If more than this is not possible with standard C++ variables then how could I create my own functions and data types in C++ to get more precise values?
Any help is appreciated, comments of the quality of the program(do ya think ‘it sucks’ or is it ‘fully sick mate’) and advice are more than welcome to improve this program as this is meant to be a display of my many years programming in C++… all 0.5 of them.