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;
}

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.

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

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)

commented: C'mon, why are you asking this? Did my post imply it can't be done with C++? -3

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)

It isn't about which language you use. There are methods for calculating the digits of pi that don't need arbitrary-precision arithmetic, for example, Simon Plouffe's method.

Start with the math--pick an algorithm, and that will tell you the kind of language features you need.

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

First, is there even a way to do it in c++?

There should be a logical way to do it, but I don't think C++ provides this functions. Maybe some outside library?

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)

I am not sure but maybe matlab
http://www.mathworks.com/products/matlab/ or FORTRAN?

GMP First result in google and it works!

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?

Thanks for the link sergent. But how do i install it and import it? Is it compatible with dev c++?

Can anyone help please?

We have. Now you have to do something.

I don't know how to install GMP. I need help with that.

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.

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?

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.

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<int> mantissa; does.

So if i did calculate pi how would i take the individual parts of the fraction and store them into an int?

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.

In terms of data types, which holds the most decimal places? Im working on deriving a formula that will calculate parts of the decimal parts of pi? Is that the approach you guys would take?

Go read Tony's post again, ignoring the Python syntax and looking just at the formula. You don't need anything more than int, I think. (Python operator // is "floor division", which is the normal way that C handles int division). Also go read the links in that thread.

I understood most of it. (Which surprised me because i don't like python) The thing i didn't understand was what is the value of num_dec? It was part of the formula so it would be nice to know. I was going through old posts and someone suggested computing 1 digit at a time a storing it into an int. I was trying to wrap my kind around how to compute one digit at a time and i couldn't think of anything except for: taking a calculation and using the number thing (something like [0]) to take out one part at a time. I hope that makes sense and im just trying to keep an open mind.

Any help please???

num_dec is just controlling when to stop generating the decimals. I am preparing the decimals as string and vegaseat gave them as normal digits. If you are not using the value to calculate, but only save it to file that is mostly what you need. As you have not generators in C. You should probably collect all decimals in string variable or char array or print them to stdout.

Umm.... Simple terms please???

Ok. Listen to everybody here. LISTEN to them and actually do some work. Once you have some code proving that you've listened to them, maybe then you'll start seeing what they're talking about ;)

This isn't a place full of free answers, it's a place for you to come to for help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.