【问题标题】:How can I get more than 100 decimal digits in C#?如何在 C# 中获得超过 100 个十进制数字?
【发布时间】:2011-10-25 10:16:33
【问题描述】:

在 C# 中是否可以得到超过 100 个十进制数字?

如果是,代码的必要部分是什么?

在 Java 中有一些东西叫 BigDecimal,但它仍然不能超过 55 位。

【问题讨论】:

  • What is the equivalent of the Java BigDecimal class in C#?的可能重复项(本质上:C#没有,需要用C#吗?)
  • 为什么投反对票?高精度算术问题似乎很正常,不是吗?
  • System.Decimal 不起作用,因为它只支持大约 28 位数字。 BigInteger 只支持整数。
  • 我认为您需要使用一些第三方库,例如 BigRational bcl.codeplex.com/releases/view/42782
  • @sehe:不,让它成为答案,意味着我将不得不支持它:)

标签: c# .net bigdecimal


【解决方案1】:

使用 J# 库:

Download the J# Redistributable 获取 J# 类库。 This article hints 关于如何在你的 .NET 项目中使用它来获得 ZIP 能力,并解释了一点关于 BigDecimal,但正如 Ramhound 解释的那样,这篇文章相对较旧。

下载后,将vsjlib 添加到您的项目中。它在 .NET 4.0 上对我不起作用,但在 2.0 项目上确实有效。它包含一个 BigDecimal 类等。但是,不确定是否最多可以提供 100 位小数,但您可以尝试一下。

使用 MPIR -- 多精度整数和有理数

您可以为.NET here (download version 0.2) 下载MPIR 库的包装器。然后,执行以下操作:

  • 将文件 \wrapper.*.dll 添加到您的项目中,确保在每次构建时将它们复制到您的 Debug 或 Release 目录。
  • \wrapper\xmpir.cs 添加到您的项目中
  • 将以下代码添加到您的项目中:

    // set the precision to 512 bits (adjust this to your needs)
    mpir.mpf_set_default_prec(512);
    ulong default_prc = mpir.mpf_get_default_prec();
    
    // init vars (important!)
    mpir.mpf_t val = mpir.mpf_init_set_d(.5);
    mpir.mpf_t result = mpir.mpf_init_set_ui(0);
    
    // calculate 0.5^200
    mpir.mpf_pow_ui(result, val, 200);
    double dresult = mpir.mpf_get_d(result);
    
    // convert result to a string, in the form 1.2345 (dot not in output) and exp holding exponent
    long exp;
    string sresult = mpir.mpf_get_string(out exp, 10, 0, result);
    
    // free vars (important!)
    mpir.mpf_clear(val);
    mpir.mpf_clear(result);
    

请注意,变量sresult 将只保存有效数字。您还必须添加用于打印指数或一堆零的逻辑。

结果是6.2230152778611417071440640537801242405902521687211671331011166147896988340353834411839448231257136169569665895551224821247160434722900390625E-60

完整 MPIR 库上的 documentation PDF file 显示了如何更详细地使用它。

【讨论】:

  • 感谢您的帮助,但正如您所知,该库使用 Bigdecimal 的 java 实现
  • 文章不是最新的。有一个类来操作一个Zip文件,这已经至少有5年了,在文章写完2年后。
  • @Ramhound,我用正确的 J# 下载位置更新了文本。
  • @HusseinX,我为 MPIR 库包装器添加了代码,使用起来有点麻烦,但那是因为它们来自 C++ 世界。但是文档很好。
【解决方案2】:

如果您使用 .net Framework 4.0,您可以参考 System.Numerics 程序集。 或者你可以从 codeplex 下载BigInteger

BigIngeger 的用法:http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx

例如:

string positiveString = "91389681247993671255432112000000";
string negativeString = "-90315837410896312071002088037140000";
BigInteger posBigInt = 0;
BigInteger negBigInt = 0;

try {
   posBigInt = BigInteger.Parse(positiveString);
   Console.WriteLine(posBigInt);
}
catch (FormatException)
{
   Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", 
                     positiveString);
}

if (BigInteger.TryParse(negativeString, out negBigInt))
  Console.WriteLine(negBigInt);
else
   Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", 
                      negativeString);

// The example displays the following output:
//   9.1389681247993671255432112E+31
//   -9.0315837410896312071002088037E+34

编辑: 这里有一个 bigNum 项目http://sine.codeplex.com/

using W3b.Sine;

public class ExpressionSample {

    public static void Main(String[] args) {

        Dictionary<String,BigNum> symbols = new Dictionary<String,BigNum>() {
            {"x", 100} // the implicit conversion defined
        };

        Expression expression = new Expression("1 + 2 / 3 * x");
        BigNum result = expression.Evaluate( symbols );
        Console.WriteLine( expression.ExpressionString + " == " + result.ToString() );

        BigNum x = 100;
        BigNum xPow100 = x.Pow( 100 );
        Console.WriteLine("100^100 == " + xPow100.ToString() );

    }
}

【讨论】:

  • 使用BigInteger 类如何帮助显示十进制值?
  • 不,它只计算ingeger
  • 您对问题进行了几次编辑,但我仍然不知道它如何帮助 OP 在 浮点算术(小数,即)。你只提到大整数。
  • 正弦可以显示100位精度的十进制值。详见sine.codeplex.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
相关资源
最近更新 更多