0

I'm writing a program to find the value of pi, and want it to show more than the standard 16 decimal places. How do I create a variable that can hold more than 16 decimal places? My current program is written below. Using Dev-C++:

#include<iostream.h>
#include<conio.h>
#include<math.h>

int factorial(int a)
{
    int b=1,c=1;
    for(c; c<=a; c++)
    {
        b=b*c;
    }
    return b;
}

int main()
{
    cout.precision(300);
    long int n,a;
    long double z=0,pi,num,den;
    for(n=0; n<1000000; n++)
    {      //begin for
        num=(pow(factorial(2*n),3))*((42*n)+5);
        den=(pow(factorial(n),6))*(pow(16,(3*n)+1));
        z=z+(num/den);

        pi=1/z;
        if(n%1==0)
        { 
            cout<<z<<endl; //test print statement
            cin>>a;
            cout<<pi;
            cout<<endl;
        }
    } 
    getch();
    return 0;      //end for
}
Alexandros
  • 3,044
  • 1
  • 23
  • 37
viraj
  • 1,784
  • 4
  • 35
  • 52
  • search SO with keywords "big number" and C++ – lamwaiman1988 May 08 '11 at 16:01
  • http://stackoverflow.com/questions/1047203/best-bignum-library-to-solve-project-euler-problems-in-c – CMircea May 08 '11 at 16:02
  • @viraj It'd be really helpful if in the future, you'd indent your code coherently. That way, when we read it, we'll find it far easier to help you! (I'll fix it this time) – Jonathan Sterling May 08 '11 at 16:02
  • 4
    It's `iostream`, not `iostream.h`, and `cmath`, not `math.h`. – Etienne de Martel May 08 '11 at 16:03
  • my compiler says that iostream.h is depriciated/antiquated, but when i use just iostream, it doesnt work. Also, math.h seems to be working for me :/ – viraj May 08 '11 at 16:11
  • 1
    @viraj: if you use the modern headers (`iostream` rather than `iostream.h`) then the names of things in the Standard Library will be in the `std` namespace, not the global one. So you'll have to write `std::cout` instead of `cout`, etc. Or use `using namespace std;` or `using std::cout;` (etc.) to make them available without namespace qualification. – Mike Seymour May 08 '11 at 16:52

3 Answers3

2

If you don't want to use an existing high-precision arithmetic library, then here are a few pointers for writing your own. It will be a fair amount of work (and quite fiddly to debug), but quite a good learning exercise if you've got the time.

  • Store each number as an array of smaller "digits". For a very simple (but inefficient) implementation, these could literally be decimal digits, with values from 0 to 9 - this will then be very easy to print. For a more efficient implementation, I'd probably use 32-bit values for the "digits". You'll also need to decide how to represent negative numbers, whether the array should be fixed or variable size, and (for working with non-integers) whether the decimal point should be fixed or floating.

  • Implement basic arithmetic using the algorithms you learnt in primary school: addition with carry, subtraction with borrow, long multiplication and long division.

  • pow and factorial, needed for your algorithm can be implemented simply as repeated multiplication; other algorithms are available if this isn't fast enough (and for functions like sqrt and sin that can't be represented exactly with basic operations).

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • The efficiency of the program isnt really too much of a factor right now. Optimization can be carried out later. – viraj May 09 '11 at 15:44
0

Sounds like you want a bignum library. Have a look at the GNU Multiple Precision Arithmetic Library for a widely-used open source alternative.

hammar
  • 138,522
  • 17
  • 304
  • 385
0

You can use one of the bigint libraries on the internet, for example: https://mattmccutchen.net/bigint/

cyphorious
  • 809
  • 3
  • 7
  • 19
  • thats a last option. Id like to code something myself, if its possible. is it so difficult that you have to use an entire lirary ? – viraj May 08 '11 at 16:06
  • Of course you can implement your big datatype on your own, but why reinventing the wheel, when there are good and tested solutions out there? – cyphorious May 08 '11 at 16:10
  • I'm still learning, so its better for me to write stuff on my own rather than using precompiled libraries. – viraj May 08 '11 at 16:16
  • @viraj: Bignum arithmetic can be fairly complicated. I would suggest practicing on something simpler. – hammar May 08 '11 at 16:47
  • well, there must be a simpler way of doing things. using classes, or something like that. – viraj May 08 '11 at 16:57
  • This is only for big integers. Or does it work with decimals too. – viraj May 09 '11 at 15:46
  • If you only want to practice, then you can try to write your own Bignum class. Normally you represent the big numbers in Strings. Of course you need to overload operations like =,+,-,*,/ and implement their logics. – cyphorious May 10 '11 at 11:46