BigInteger的划分在C# [英] BigInteger division in C#

查看:210
本文介绍了BigInteger的划分在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写这需要在C#BigInteger类的准确划分一类。

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

例如:

BigInteger x = BigInteger.Parse("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
BigInteger y = BigInteger.Parse("2000000000000000000000000000000000000000000000000000000000000000000000000000000000000");

x /= y;

Console.WriteLine(x.ToString());

//Output = 0

问题是,作为一个整数,自然不成立十进制值。 我怎样才能克服这个得到0.5(给定的例子)的实际结果。

The problem is that being an Integer, naturally it does not hold decimal values. How can I overcome this to get the real result of 0.5 (given example).

P.S。该解决方案必须能够准确地划分任何的BigInteger,不只是例子!

P.S. The solution must be able to accurately divide any BigInteger, not just the example!

推荐答案

在上面的例子中,该数字仍然足够小,可以转换为,所以在这情况下,你可以说

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;

如果 X 太庞大了但依然不相上下,也许这大招是有帮助的:

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));

但在一般情况下,当的BigInteger 是巨大的,他们的智商是巨大的太多,这是很难做到没有导入第三方库。

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.

这篇关于BigInteger的划分在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆