如何在C#中获得超过100个十进制数字?
是否可以在C#中获得超过100个十进制数字?
如果是,那么代码的必要部分是什么?
在Java中有一些东西叫BigDecimal
但它仍然不能超过55位。
使用J#库:
下载J# Redistributable以获取J#类库。 本文提示如何使用它来获取.NET项目中的ZIPfunction,并解释一下BigDecimal,但Ramhound解释说,这篇文章相对较旧。
下载后,将vsjlib
添加到项目中。 它在.NET 4.0上对我不起作用,但它确实适用于2.0项目。 它包含一个BigDecimal类。 但是,不确定,如果它给你最多100个小数,但你可以尝试一下。
使用MPIR – 多精度整数和理性
您可以在此处下载适用于.NET的MPIR库的包装器(下载版本0.2) 。 然后,执行以下操作:
- 将files
\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库上的文档PDF文件显示了如何更详细地使用它。
如果使用.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 symbols = new Dictionary() { {"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() ); } }
- 更快地替换Dictionary
- 如何使用WPF MediaKit或类似程序从使用DES创建的DirectShowfilter图形渲染到WPF控件?
- 为什么我必须在DataGridViewRow上调用Cast 扩展方法?
- 为什么我的断点在Visual Studio中重复?
- 使用C#的POST方法使用数据进行PHP加密和解密
- 为什么我不能在WebForm中声明UserControl的子元素(属性)?
- 从.net DLL导出本机C函数?
- 对PInvoke函数的调用使堆栈失衡。 这可能是因为托管的PInvoke ..(.NET 4)
- 目标框架中缺少“WindowsBase”,“PresentationCore”,“PresentationFramework”