You most likely don't want to use BigInteger
at all. Instead, decimal
might be what you're looking for.
Something like this:
decimal[] HCC = new [] { 2M, 4M, 6M, 12M, 24M };
...
S += 1M / HCC[i];
Decimal is in base-10, rather than base-2, so it will guarantee you a decimal precision, rather than binary. It also has quite a huge range, so it should be good enough for your use.
Oh, and if you've got 70-digit numbers there, how do you expect them to affect a number you only need to be precise to 7 decimal digits? I think you're missing a point somewhere...
EDIT:
You're still missing a crucial point - if you really are working with a growing series of numbers, the numbers with digits above 20 will have no impact on the result anyway. The sum of the series converges on a given number, and you can approximate the first n significant digits easily (have a look at Taylor series for how this is used to e.g. compute sin (x)
, which is just another infinite decimal expression).
Now, if the series is finite and isn't necessarily growing, the simples thing you can do is to order it before doing the sum. Input the numbers in double
- don't worry about lost precision, it will not affect the outcome (this takes a bit of mathematical analysis, but I have no idea what your background is, so if you can't prove this yourself, you might want to ask a question on math SE). Order them from largest to smallest. Sum them up one after another. The result should be quite precise to about 15 digits - you'll lose the far decimal digits quickly as you start adding up the larger 1 / x
, but those will not affect the result anyway.
Of course, if you already know this, you can easily get to proving that you don't need to count the big numbers in the first place - they cannot affect decimals higer than their own digit count plus about 1-2. The exact behaviour is much more pronounced if the growth rate is really big (a very simple example being a geometric series with G(n) = 2 ^ n
), but that seems to approximate your case rather well, given that you're working with small and extremely large numbers in the same series. If this is not good enough, you can try doing partial sums of numbers that are close enough to add with precision on e.g. decimal
, and then adding up those sums - high-school algebra should be good enough for that :)
All in all, doing the math in your head (or on paper) helps a lot. Infinite decimal expression is something quite normal in math, and there's a lot of tools to work with those. And with approximation, rounding, limits and other stuff related to this.