Thread: store pi O.o;

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    155

    store pi O.o;

    Hi. I am making a program to find a circle's circumference. I want to use the longer pi ... but I dont know how to store all the digits in it. I can get some of them using float, but I like to have more ... like over 32m of em XD.I kinda knew that float wouldnt work fully, seeing as the money size would be way to larg to store it all at once. Anyone have a idea to maybe store all these digists?

    Code:
    #include "c:\tt.h"
    
    using namespace std;
    int main(void)
    {
        float pi = 3.14159265358979323846264338327950288419716939937510;
        cout<<"pi is "<<pi<<endl;
        system("pause");
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The number of digits displayed is different than the number stored. You need to use io manipulators like setprecision to display more digits.

    You should use a long double to store your pi value, since it has a greater precision and will hold the value more accurately than a float. I'm not sure even long double can store all of those digits. If you really need them all, you might need a separate number library.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    im sure this has come up before, search either here or another search engine. i quickly seen a neat way of doing it, store it as a char array. since it would be a pain to type somethign like: char pi[] = {'3','.',etc',, id imagine it would be feasible with file i/o..

    ie, save a file with the value of pi in it. write a seperate program to read in that file, char by char, and create an output file that would create the code for you.

    example input file:
    3.14etc

    example output file:
    char pi[] = {'3','.',... etc

    you can then use this second file and paste it to your code... however the line would be incredibly long if you were to actually do it to 32m..

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Brazil
    Posts
    13
    If you need to do calculations with it there are some arbitrary precision libraries floating around on the internet, Google for them if that's the case.

    If you don't need to do any calculations with it, just use a character as someone said before, but you can just use a string:
    Code:
    string pi="3.14159265358979323846264338327950288419716939937510";

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    oh whoops thought this was C, good thinking.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Quote Originally Posted by nadroj
    im sure this has come up before, search either here or another search engine. i quickly seen a neat way of doing it, store it as a char array. since it would be a pain to type somethign like: char pi[] = {'3','.',etc',, id imagine it would be feasible with file i/o..

    ie, save a file with the value of pi in it. write a seperate program to read in that file, char by char, and create an output file that would create the code for you.

    example input file:
    3.14etc

    example output file:
    char pi[] = {'3','.',... etc

    you can then use this second file and paste it to your code... however the line would be incredibly long if you were to actually do it to 32m..

    Neat idea ^.^ yea its all ready in a text file I made... XD Sorry, I didnt know what to totaly search for really so when I was looking for.

    The number of digits displayed is different than the number stored. You need to use io manipulators like setprecision to display more digits.

    You should use a long double to store your pi value, since it has a greater precision and will hold the value more accurately than a float. I'm not sure even long double can store all of those digits. If you really need them all, you might need a separate number library.
    I think so too. I know the long double cant deal with them all either.

    If you need to do calculations with it there are some arbitrary precision libraries floating around on the internet, Google for them if that's the case.

    If you don't need to do any calculations with it, just use a character as someone said before, but you can just use a string:

    Code:
    string pi="3.14159265358979323846264338327950288419716939 937510";
    Hmmm, I didnt think that O.o; but I thought string cant add like it would a int or something that deal with numbers.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> but I thought string cant add like it would a int or something that deal with numbers.
    Right, that's why a string or char array are not options if you need to do any calculations with it.

  8. #8
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Yeah, I don't think he's looking just for the number pi, he wants to work with it.

    As already mentioned, you're better off with an arbitrary precision library. There are many free ones.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Yea... I want to work with the numbersXD Sorry I missed read the string thing there, thought he was telling me that, that would work too. Yea I am looking up the precision library stuff.
    Last edited by adr; 10-26-2006 at 07:10 PM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I think so too. I know the long double cant deal with them all either.
    float.h defines constants like
    FLT_DIG
    DBL_DIG
    LDBL_DIG
    Which represent the number of decimal digits a float can store with any degree of accuracy.

    For many implementations, FLT_DIG is 6 and DBL_DIG is 15.
    Code:
    float pi = 3.14159265358979323846264338327950288419716939937510;
    double pi = 3.14159265358979323846264338327950288419716939937510;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by adr
    I want to use the longer pi ...
    There's your problem right there!
    You don't need more precision than that, you just want it, and for no good reason.

    doubles are more than accurate enough for your every need, and anything more is just pointless until you can prove that doubles are insufficient, and I don't think you can. What could you possibly be doing with this circumference result that wouldn't work with doubles?
    What are you REALLY trying to achieve here?

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    doubles are more than accurate enough for your every need, and anything more is just pointless until you can prove that doubles are insufficient, and I don't think you can. What could you possibly be doing with this circumference result that wouldn't work with doubles?
    What are you REALLY trying to achieve here?
    I am just running a test was all. Is that so wrong? Its no differnt then people making another C++ program that someone els has done befor; so what I am really trying to achieve is what I want to achieve. Lol, Yea ^^; I just want to see how much differnt would it be with more digits from pi then the stander 3.14.


    Anyways, i've been looking on the net for a bit here and found many arbitrary precision library. Which one do you think is perty good; befor I start downloading them and find out they may not work for what I am trying to do or maybe not run for my compiler? I am using Dev-C++ ver. 4.9.9.2.
    Last edited by adr; 10-27-2006 at 07:30 PM.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I just want to see how much differnt would it be with more digits from pi then the stander 3.14.
    Not much. In real life the accuracy of measurement will be limited anyway. If you are able to measure the diameter with the accuracy of a millimeter, you can't expect to calculate the circumference to more decimal places. You'll just have to round off any places after millimeters.

    So, if your base unit is metres 3.1416 would be all you need to find the circumference in mm. (If you have a circle with diameter 1 m exactly, the circumference rounded to millimeters will be 3.142. You need pi up to the 4th decimal place, so you can round the value properly to the 3rd decimal place. If you use a more accurate pi, the result after rounding will still be the same.)

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Quote Originally Posted by anon
    Not much. In real life the accuracy of measurement will be limited anyway. If you are able to measure the diameter with the accuracy of a millimeter, you can't expect to calculate the circumference to more decimal places. You'll just have to round off any places after millimeters.

    So, if your base unit is metres 3.1416 would be all you need to find the circumference in mm. (If you have a circle with diameter 1 m exactly, the circumference rounded to millimeters will be 3.142. You need pi up to the 4th decimal place, so you can round the value properly to the 3rd decimal place. If you use a more accurate pi, the result after rounding will still be the same.)
    Hmm ^.^ ok, thanks for clearning that up.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, actually my explanation may be a little faulty. But still, using a more precise pi doesn't change the orders of magnitude, and for most purposes double would be enough. If you measure in kilometers, there's no point in calculating the circumference in centimeters etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Pi - Hm, somethign is not right here.
    By MadnessRed in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2008, 01:07 PM
  3. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  4. Pi Calculation
    By EvilGuru in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 04:25 AM
  5. C for PI
    By Lynux-Penguin in forum C Programming
    Replies: 13
    Last Post: 04-28-2002, 07:37 PM