# Thread: Creating very long decimals

1. ## 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. Originally Posted by scwizzo
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. 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?

4. 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. 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.

6. 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. 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.

8. _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

9. 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. 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.

Popular pages Recent additions