我的物理老师给全班布置了一项艰巨的任务。我正在尝试创建一个程序来为我计算一些事情。在某个时刻,我需要将分子数量乘以百分比。 Ulong 不能容纳大到 6022 * 10 ^ 19 的数字,所以我必须使用 .net 4.0 中的 BigInteger。但是乘法不能应用于 BigInteger 和 double。我只需要整个数字。有什么办法可以绕过这个吗?
代码如下:
private static BigInteger pencapmoles = BigInteger.Pow(6022/9, 19);
private static BigInteger inkmoles = pencapmoles;
private static BigInteger tries = 0;
private static BigInteger molesinbucket = BigInteger.Pow(26761768, 19);
private static double percentage;
static void Main(string[] args)
{
while (inkmoles > 1)
{
percentage = (double)(inkmoles / molesinbucket);
inkmoles = pencapmoles * percentage;
tries++;
}
Console.WriteLine($"It only took us {tries} tries.");
}
You would be better off switching all computations either to double
or to BigInteger
. C# double
has enough range to hold values a lot greater than 1019, but the answer is going to have a slight imprecision. It is a near certainty that it is going to be enough for the purposes of solvind your task.
如果你切换到BigInteger
,先乘,再除:
inkmoles = (inkmoles * pencapmoles) / molesinbucket;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)