详解.NET 4.0 Beta2的Complex和BigInteger类

开发 后端
在这里我们将介绍.NET 4.0 Beta2中的Complex和BigInteger类,希望本文能对大家有所帮助。

本文将介绍的是.NET 4.0 Beta2中的Complex和BigInteger类,BigInteger类主要用途在于对不同数据的处理。希望对大家有所帮助。

.NET4.0 Beta2中提供了新的System.Numerics命名空间,对应于System.Numerics.dll。该命名空间下就两个类BigInteger和Complex,我们来简单了解下这两个类的用法。

BigInteger:任意大小的带符号整数

1.Int64, SByte, UInt16, UInt32, and UInt64这些都有一个MinValue和MaxValue属性。而BigInteger没有这两个属性,因为它没有大小限制。2.不可变的类型.3.由于他没有大小限制,理论上当它足够大的时候会出现OutOfMemoryException异常.

BigInteger类初始化

1.我们可以使用已有的数据类型来初始化BigInteger,如下:

  1. BigInteger bigIntFromDouble = new BigInteger(179032.6541);//会截取小点前的  
  2. BigInteger bigIntFromInt64 = new BigInteger(934157136952); 

2.我们也可以使用超出现有数据类型范围的方式来得到BigInteger:

  1. byte[] bytes = { 5, 4, 3, 2, 1 };  
  2. BigInteger number = new BigInteger(bytes);  
  3. Console.WriteLine("The value of number is {0} (or 0x{0:x}).", number);   
  4. //The value of number is 4328719365 (or 0x102030405).    

字节数组的第一个元素为16进制的最低位,依次升高.3.可以使用Parse或 TryParse方法将string的实例化为BigInteger:

  1. string positiveString = "91389681247993671255432112000000";  
  2. string negativeString = "-90315837410896312071002088037140000";  
  3. BigInteger posBigInt = 0;  
  4. BigInteger negBigInt = 0;  
  5.  
  6. posBigInt = BigInteger.Parse(positiveString);  
  7. Console.WriteLine(posBigInt);  
  8.  
  9. BigInteger.TryParse(negativeString, out negBigInt);  
  10. Console.WriteLine(negBigInt); 

4. 还可以使用静态方法Pow如下:

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

BigInteger支持所有的数学运算,我们可以完全象使用其他整数类型一样使用BigInteger

Complex复数类

  1. 1.var z1 = new Complex(); // this creates complex zero (0, 0)  
  2.   var z2 = new Complex(2, 4);  
  3.   var z3 = new Complex(3, 5);  
  4.  
  5.   Console.WriteLine("Complex zero: " + z1);  
  6.   Console.WriteLine(z2 + " + " + z3 + " = " + (z2 + z3));  
  7.  
  8.   Console.WriteLine("|z2| = " + z2.Magnitude);  
  9.   Console.WriteLine("Phase of z2 = " + z2.Phase); 

2.我们可以使用一个ComplexFormatter类来辅助我们做格式化输出,如下:

  1. using System;  
  2. using System.Numerics;  
  3.  
  4. public class ComplexFormatter :IFormatProvider, ICustomFormatter  
  5. {  
  6.    public object GetFormat(Type formatType)   
  7.    {     
  8.       if (formatType == typeof(ICustomFormatter))  
  9.          return this;  
  10.       else 
  11.          return null;  
  12.    }  
  13.  
  14.    public string Format(string format, object arg,   
  15.                         IFormatProvider provider)  
  16.    {  
  17.       if (arg is Complex)  
  18.       {  
  19.          Complex c1 = (Complex) arg;   
  20.          // Check if the format string has a precision specifier.  
  21.          int precision;  
  22.          string fmtString = String.Empty;  
  23.          if (format.Length > 1) {  
  24.             try {  
  25.                precision = Int32.Parse(format.Substring(1));  
  26.             }  
  27.             catch (FormatException) {  
  28.                precision = 0;  
  29.             }  
  30.             fmtString = "N" + precision.ToString();  
  31.          }  
  32.          if (format.Substring(0, 1).Equals("I", StringComparison.OrdinalIgnoreCase))  
  33.             return c1.Real.ToString("N2") + " + " + c1.Imaginary.ToString("N2") + "i";  
  34.          else if (format.Substring(0, 1).Equals("J", StringComparison.OrdinalIgnoreCase))  
  35.             return c1.Real.ToString("N2") + " + " + c1.Imaginary.ToString("N2") + "j";  
  36.          else 
  37.             return c1.ToString(format, provider);  
  38.       }  
  39.       else 
  40.       {  
  41.          if (arg is IFormattable)  
  42.             return ((IFormattable) arg).ToString(format, provider);  
  43.          else if (arg != null)   
  44.             return arg.ToString();  
  45.          else 
  46.             return String.Empty;  
  47.       }                          
  48.    }  

3.使用如下:

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

原文标题:.NET 4.0 Beta2中的BigInteger和Complex类

链接:http://www.cnblogs.com/carysun/archive/2009/10/25/Numerics-BigIntegerComplex.html

【编辑推荐】

  1. Visual Studio 2010响应Ribbon控件消息
  2. 微软称C++将得到Visual Studio 2010更多支持
  3. Visual Studio 2010新特性:动态语言功能
  4. 微软对Visual Studio 2010的Bug熟视无睹?
  5. Visual Studio 2010 Beta1试用手记
责任编辑:彭凡 来源: 博客园
相关推荐

2009-10-28 09:23:27

WF4.0 Beta2

2009-11-24 15:49:48

.NET 4.0 Be

2009-11-25 11:31:36

Visual Stud

2013-10-10 15:08:36

UbuntuUbuntu 13.1gnome

2023-08-07 15:42:25

ArkUI-X鸿蒙

2009-12-04 09:14:05

.NET 4.0

2012-06-12 13:23:58

LinuxLinux Deepi

2021-08-13 10:09:36

鸿蒙HarmonyOS应用

2010-02-24 14:24:35

.NET 4.0

2011-02-17 16:59:00

VS.NET 2010

2009-08-03 09:21:35

.NET 4.0 Be.NET

2013-06-25 10:13:11

iOS7WWDC苹果

2009-07-30 13:17:44

STM.NET.NET 4 Beta

2009-12-14 09:40:50

VS 2008 Bet

2011-09-27 10:40:48

Ubuntu 11.1

2011-12-13 20:47:33

iOS 5.1

2016-11-09 16:43:40

苹果iOS 10.2

2009-12-02 14:44:09

VS2010 Beta

2009-05-08 10:09:30

.NET4.0PLINQ

2009-02-02 09:13:50

Windows7Windows微软
点赞
收藏

51CTO技术栈公众号