Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting quotient of two BigIntegers as double

Tags:

c#

math

What's the best way to convert the quotient of two C# BigIntegers while retaining as much precision as possible? My current solution is:

Math.Exp(BigInteger.Log(dividend) - BigInteger.Log(divisor));

I'm guessing this is suboptimal.

like image 887
Andreas Brinck Avatar asked Jan 13 '11 11:01

Andreas Brinck


1 Answers

First read this article. It contains what you want to do.

Then, work out the continued fraction expansion of dividend / divisor, and stop when you reached wanted accuracy. You won't need the full expensive division operation (I suppose it is O(n log^2 n) or something like that), you'll need only integer division / remainder.

Nevertheless, provided BigInteger.Log returns doubles, the exp(log a / log b) thing will work great, and I think it may be faster than the continued fraction expansion. You need two conversions to double (likely fast), and accuracy is preserved throughout the operation (even if log divisor and log dividend are very close to each other).

like image 198
Alexandre C. Avatar answered Oct 26 '22 13:10

Alexandre C.