将BigInteger乘以Double [英] Multiplying BigInteger by Double

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

问题描述

我的物理老师给全班做了艰巨的任务。我正在尝试创建一个程序,它将为我计算一些内容。在某一点上,我需要将一定数量的分子乘以一个百分比。 Ulong不能容纳最大为6022 * 10 ^ 19的数字,因此我必须使用.net 4.0中的BigInteger。但是,乘法不能应用于BigInteger和double。我只需要整数。有什么办法可以绕过这个?
代码如下:

My Physics teacher gave the class a hard task. I'm trying to create a program that will calculate some things for me. At a certain point I need to multiply an amount of molecules by a percentage. Ulong can't hold numbers as large as 6022 * 10 ^ 19, so I have to use BigInteger from .net 4.0. However multiplication cannot be applied to BigInteger and double. I only need the whole number. Is there any way to bypass this? The code is as follows:

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.");
}


推荐答案

您最好关闭开关所有计算要么是 double ,要么是 BigInteger 。 C# double 的范围足以容纳远远大于10 19 的值,但是答案将有一点不精确。可以肯定的是,对于解决任务而言,这已经足够了。

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 ,先相乘,然后进行除法。

If you switch to BigInteger, multiply first, and then do the division:

inkmoles = (inkmoles * pencapmoles) / molesinbucket;

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

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