Thread: Creating very long decimals

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    Creating very long decimals

    I am trying to make a program that will calculate a huge decimal place. Something that is out to the 100,000th place, or possibly more. I am using taylor series to approximate pi as my testing number. The first 16 digits are a given since double will automatically be to that extent. I was thinking of sprintf-ing the nth number in scientific notation so I can get it to drop all the leading zeros. From there I'd chop off the decimal points and take the whole number. From there it'd be added to a string at the end. It would only find one decimal point at a time, but I think it should work. Only problem with this is the taylor series for pi is exponentially decreasing. So getting one decimal at a time does not work (if one is .00003948739743 and another is .000000004343 the numbers between the 3 on the first and the 4 on the second will be lost). Unless I took the entire scientific number, passed it to a user-defined function to take off the "e-X" at the end, and the decimal point, and from there add it to the string.

    Does anyone have any other/better ideas for getting a very long decimal place? I've done some searching and nothing really helpful has popped up.

    EDIT: to save on having to store this in a double or float, I only want to calculate a small chunk of the decimals at a time, and strcat them to a buffer and paste it in to a window for verification.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by scwizzo View Post
    I am using taylor series to approximate pi as my testing number.

    I only want to calculate a small chunk of the decimals at a time.
    These are mutually inconsistent, unless you mean to calculate the entire Taylor series again for each small chunk of decimals (that is, you can't just drop a lot of the sixes from say 1/6 = .16666666666666666666666666666666666666....... because the first time around you only cared about the .16666 part). You'll also have "overflow" where computing a smaller chunk will change the answer to a larger chunk, so you'll have to be ready to change already-computed numbers.

    All in all, I wouldn't recommend this tactic. (After all, 100000 decimal places only requires what, 310000 bits or so? No problem!)

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Are you trying to store a high precision number in memory or are you trying to calculate a Taylor series to arbitrary precision, discarding the total as you print it out?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Let me reword this. I am doing the taylor series in a loop incrementing n every cycle. Depending on how accurate I want to be, I adjust how many times n gets incremented. By saying I am doing it in small chunks I mean something like this...
    Code:
        while(m < 20){ //i replaced n with m 
            pi = pi + (1.0/((2*m+1)*pow(3.0,m) ) ) ;
            m++;
            pi = pi - (1.0/((2*m+1)*pow(3.0,m) ) ) ;
            m++;
        }
    It does the next remainder, and makes it part of the already found taylor series. It does not recalculate the entire series. I can see how that was confusing. I basically want to know/get an idea of how to store a very accurate number, since double can only store so much.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I basically want to know/get an idea of how to store a very accurate number, since double can only store so much.
    You could use a bignum library like the GMP.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I looked at GMP and i can't find a version of it that has the libgmp.a file, unless it expects you to make your own library from all the files it has. Would it be possible to do something like...
    Code:
    typedef double myDouble[20]; //20 is just arbitrary, can be any number
    Or perhaps use malloc() to get the space I need? I have not really messed around with typedef and malloc before so I am not sure. Thanks for the help so far.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I looked at GMP and i can't find a version of it that has the libgmp.a file, unless it expects you to make your own library from all the files it has.
    Yes, I believe the official distribution only provides source.

    Would it be possible to do something like...
    ... and thus create your own bignum implementation. Of course, but why bother unless you need to? Besides it taking time from your actual work, you probably will not be able to optimise it as well as the relevant experts.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    _IF_ you are going to implement a multiprecision library, you probably don't want to use "double" as your base-type, as to get "more precision", you will have to calculate each digit on it's own. So some integer type (simplest form is to store each decimal digit as a char, but it is less efficient than using a longer integer type in binary form).

    Using this technique, only the memory available to the application is limiting the number of decimals you can use - so you can quite easily [that is, once you have the four basic math operations working correctly - this part is not particularly easy, but not extremely hard either] create an applicaiton that has millions of decimals precision in the result.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    haha... I think i fell in the deep end. After going through a few GMP and CLN manuals I got totally lost, and making my own precision decimal seems to be even harder than implementing an already made one. The only way I can really see this working for me is if each decimal of Pi was calculated one digit at a time (like matsp said), and storing that in to a char or similar. But since I don't know of a real way to calculate just the remainder in integer form this is kind of beating a dead horse.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I wrote my own classes for arbitrarily long integers and rediculously large and accurate floating point types. IIRC someone has already successfully written code to calculate PI from it. Well anyway, it's on my homepage.

    btw the link in your sig seems to be broken.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. sorting using pointer to pointer not working
    By eager2no in forum C Programming
    Replies: 17
    Last Post: 09-21-2008, 12:52 AM
  3. Problem in Converting Unsigned long to String
    By cprogrammer_18 in forum C Programming
    Replies: 8
    Last Post: 01-14-2006, 08:57 AM
  4. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  5. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM