BigInteger division in C#

后端 未结 7 1549
礼貌的吻别
礼貌的吻别 2020-12-09 19:05

I am writing a class which needs accurate division of the BigInteger class in C#.

Example:

BigInteger x = BigInteger.Parse(\"10000000000000000000000         


        
7条回答
  •  误落风尘
    2020-12-09 19:43

    In the above example, the numbers are still small enough to be converted to double, so in this case you can say

    double result = (double)x / (double)y;
    

    If x and y are too huge for a double but still comparable, maybe this great trick is helpful:

    double result = Math.Exp(BigInteger.Log(x) - BigInteger.Log(y));
    

    But in general, when the BigInteger are huge, and their quotient is huge too, this is hard to do without importing a third-party library.

提交回复
热议问题