這兩個(gè)類的用法。BigInteger:任意大小的帶符號(hào)整數(shù)1.Int64, SByte, UI " /> 亚洲精品2区,精品一区二区三区蜜桃,少妇视频在线观看

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

.NET 4.0 Beta2中的BigInteger和Complex類

.NET4.0 Beta2中提供了新的System.Numerics命名空間,對(duì)應(yīng)于System.Numerics.dll。該命名空間下就兩個(gè)類BigInteger和Complex,我們來(lái)簡(jiǎn)單了解下
這兩個(gè)類的用法。

BigInteger:任意大小的帶符號(hào)整數(shù)

1.Int64, SByte, UInt16, UInt32, and UInt64這些都有一個(gè)MinValue和MaxValue屬性。而B(niǎo)igInteger沒(méi)有這兩個(gè)屬性,因?yàn)樗鼪](méi)有大小限制。
2.不可變的類型.
3.由于他沒(méi)有大小限制,理論上當(dāng)它足夠大的時(shí)候會(huì)出現(xiàn)OutOfMemoryException異常.

BigInteger初始化

1.我們可以使用已有的數(shù)據(jù)類型來(lái)初始化BigInteger,如下:

BigInteger bigIntFromDouble = new BigInteger(179032.6541);//會(huì)截取小點(diǎn)前的
BigInteger bigIntFromInt64 = new BigInteger(934157136952);

2.我們也可以使用超出現(xiàn)有數(shù)據(jù)類型范圍的方式來(lái)得到BigInteger:

byte[] bytes = { 5, 4, 3, 2, 1 };
BigInteger number = new BigInteger(bytes);
Console.WriteLine("The value of number is {0} (or 0x{0:x}).", number);
//The value of number is 4328719365 (or 0x102030405).
字節(jié)數(shù)組的第一個(gè)元素為16進(jìn)制的最低位,依次升高.

3.可以使用ParseTryParse方法將string的實(shí)例化為BigInteger:

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

posBigInt = BigInteger.Parse(positiveString);
Console.WriteLine(posBigInt);

BigInteger.TryParse(negativeString, out negBigInt);
Console.WriteLine(negBigInt);

4. 還可以使用靜態(tài)方法Pow如下:

BigInteger number = BigInteger.Pow(Int64.MaxValue, 3);

BigInteger支持所有的數(shù)學(xué)運(yùn)算,我們可以完全象使用其他整數(shù)類型一樣使用BigInteger

Complex復(fù)數(shù)類

1.var z1 = new Complex(); // this creates complex zero (0, 0)
var z2 = new Complex(2, 4);
var z3 = new Complex(3, 5);

Console.WriteLine("Complex zero: " + z1);
Console.WriteLine(z2 + " + " + z3 + " = " + (z2 + z3));

Console.WriteLine("|z2| = " + z2.Magnitude);
Console.WriteLine("Phase of z2 = " + z2.Phase);

2.我們可以使用一個(gè)ComplexFormatter類來(lái)輔助我們做格式化輸出,如下:

using System;
using System.Numerics;

public class ComplexFormatter :IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null
;
}

public string Format(string format, object arg,
IFormatProvider provider)
{
if (arg is Complex)
{
Complex c1 = (Complex) arg;
// Check if the format string has a precision specifier.
int precision;
string fmtString = String.Empty;
if (format.Length > 1) {
try {
precision = Int32.Parse(format.Substring(1));
}
catch (FormatException) {
precision = 0;
}
fmtString = "N" + precision.ToString();
}
if (format.Substring(0, 1).Equals("I", StringComparison.OrdinalIgnoreCase))
return c1.Real.ToString("N2") + " + " + c1.Imaginary.ToString("N2") + "i";
else if (format.Substring(0, 1).Equals("J", StringComparison.OrdinalIgnoreCase))
return c1.Real.ToString("N2") + " + " + c1.Imaginary.ToString("N2") + "j";
else
return
c1.ToString(format, provider);
}
else
{
if (arg is IFormattable)
return ((IFormattable) arg).ToString(format, provider);
else if (arg != null)
return arg.ToString();
else
return
String.Empty;
}
}
}

3.使用如下:

Complex c1 = new Complex(12.1, 15.4);
Console.WriteLine("Formatting with ToString():" + c1.ToString());
Console.WriteLine("Formatting with ToString(format): " + c1.ToString("N2"));
Console.WriteLine("Custom formatting with I0:" + String.Format(new ComplexFormatter(), "{0:I0}", c1));
Console.WriteLine("Custom formatting with J3:" + String.Format(new ComplexFormatter(), "{0:J3}", c1));

NET技術(shù).NET 4.0 Beta2中的BigInteger和Complex類,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 泾阳县| 天镇县| 德兴市| 洪雅县| 永定县| 宁城县| 高唐县| 八宿县| 浏阳市| 昭觉县| 盐亭县| 星子县| 亚东县| 泰顺县| 唐海县| 永安市| 黔西县| 乳山市| 五家渠市| 嘉鱼县| 云阳县| 新津县| 巴塘县| 旬邑县| 常德市| 黎平县| 阳山县| 左云县| 辽阳县| 仁布县| 鱼台县| 都昌县| 扎赉特旗| 江源县| 科技| 丹东市| 株洲市| 县级市| 南皮县| 双城市| 怀柔区|