Pi to many decimal places
Ok so i want to make a program that calculates pi. Ive done that already but it only gives me so many decimal places. I want it to give me tons. Im using Dev C++ if that helps too. Here's my code:
#include <iostream.h>
#include <math.h>
int main()
{
double pi = 0;
int elements;
cout << "How many???";
cin >> elements;
for (int n = 1; n <= elements; n++)
{
pi += (double) pow(-1, n+1)/(2*n-1);
}
pi *= 4;
cout << "Estimated PI value (" << elements << " elements): " << pi;
system("pause");
return 0;
}
penguino138
Junior Poster in Training
68 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
You can't use 'normal numbers' like ints or longs. You need to come up with a system that allows you to compute 1 number at a time. Using arrays for each number where each array element holds 1 digit of the numbers might work for you.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Arbitrary precision arithmetic is not easy, and even when well written, not fast. Are you constrained to use C++? Then I suggest you search like this otherwise, use a more appropriate language/tool
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
First, is there even a way to do it in c++? If i absolutely have to use an outside program/language then i will. And what are some examples of those programs/languages? (Id assume things like java aren't any of them)
penguino138
Junior Poster in Training
68 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
Did you ignore my link? It certainly can be done in C / C++, but the effort is large, either for you or the folk who created a library.
As for other options:Copy someone else's work. For instance 1000 digits (and formulae) or 1.25 million digits
Find an algorithm that doesn't need to keep the whole answer in memory, but just improves the existing result... and then get rid of the first N digits of that result. See prior link to various formulae
Use algebraic software
Use a language that directly supports big floats: Perl and Python both do...
Strongly consider that you need to learn how to effectively use a search engine
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
I know his to use a search engine and i read that site link. Anyway. I already know the algorithm i want to use. Its 4(1-1/3+1/5-1/7+1/9) etc. That's what the for loop is for. To repeat that a ton of times. So what's the next step from here?
penguino138
Junior Poster in Training
68 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
Thanks for the link sergent. But how do i install it and import it? Is it compatible with dev c++?
penguino138
Junior Poster in Training
68 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
penguino138
Junior Poster in Training
68 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
We have. Now you have to do something.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I don't know how to install GMP. I need help with that.
penguino138
Junior Poster in Training
68 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
What you want is for someone to do it for you. What you need is to make use of the tools that you already have available. Really. You claim that you know how to use Google but it doesn't seem like that to this observer, because you aren't actually doing that.
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
You guys are a little harsh but i forgive you. So if i don't want to use a library like GMP can't i just use arrays like you said. Could you please give me an example of how you could storel like 5 decimal places into each array?
penguino138
Junior Poster in Training
68 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
GMP is open source. Why don't you download it and just take the parts you need?
Or read about IEEE-754 and generalize it to your needs.
What good is 5 decimal places to your task??
If you want to store big numbers consider
class bigfloat {
int sign;
int exponenent;
std::list<int> mantissa;
}
where mantissa stores a number in base 2^32 format (see below). Don't forget you will need to implement the math operators and some way to print these things.
base-2^32: Base 10 stores things as digit*10^N in the Nth place. Base-2^32 stores things as 1*(an int) in the 0th place, 2^32*(an int) in the 1-th place, 2^64*(an int) in the 2nd place, etc.
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
Thanks for the very complicated info. If you could explain a little simpler that would be great but ill look over it for a while. I don't quite understand what the function std::list mantissa; does.
penguino138
Junior Poster in Training
68 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
So if i did calculate pi how would i take the individual parts of the fraction and store them into an int?
penguino138
Junior Poster in Training
68 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
mantissa: Look again. I was suggesting a class that could be used for dealing with very large floating point numbers. The mantissa is not a function but the 'digits' part of a floating point representation. (The exponent is the 'size' part and the sign is the sign). Follow and re-read the IEEE link for more detail.
how would i store them into an int: Any way that works. This is one of the essential parts of the code you propose to write. I have no interest in figuring out the details for you.
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852