. Net 4.0 biginteger and complex class

Source: Internet
Author: User

. Net4.0 beta2 provides the new system. numerics namespace, which corresponds to system. numerics. dll. The namespace contains two classes: biginteger and complex.
The usage of these two classes.

Biginteger: A signed integer of any size. It can be an integer of any size.

1. int64, sbyte, uint16, uint32, and uint64 all have a minvalue and maxvalue attribute. Biginteger does not have these two attributes because it has no size limit.
2. unchangeable type.
3. Because he does not have the size limit, in theory, when it is large enough, an outofmemoryexception will occur.

Biginteger Initialization

1. You can use an existing data type to initialize biginteger as follows:

 
BigintegerBigintfromdouble =NewBiginteger(179032.6541); // Captures
BigintegerBigintfromint64 =NewBiginteger(934157136952);

2. We can also use a method that exceeds the range of existing data types to obtain biginteger:

Byte[] Bytes = {5,4,3,2,1};
BigintegerNumber =NewBiginteger(Bytes );
Console. Writeline ("The value of number is {0} (or 0x {0: x }).", Number );
// The value of number is 4328719365 (or 0x102030405 ).
The first element of the byte array is a hexadecimal percentile, which increases sequentially.

3. You can use the parse or tryparse method to instantiate a string as biginteger:

StringPositivestring ="91389681247993671255432112000000";
StringNegativestring ="-90315837410896312071002088037140000";
BigintegerPosbigint =0;
BigintegerNegbigint =0;

Posbigint =Biginteger. Parse (positivestring );
Console. Writeline (posbigint );

Biginteger. Tryparse (negativestring,OutNegbigint );
Console. Writeline (negbigint );

 

4. You can also use the static POW method as follows:

 
BigintegerNumber =Biginteger. Pow (Int64. Maxvalue,3);

 

Biginteger supports all mathematical operations. We can use biginteger exactly like other integer types.

Complex plural class

1. varZ1 =NewComplex();// This creates complex zero (0, 0)
VaRZ2 =NewComplex(2,4);
VaRZ3 =NewComplex(3,5);

Console. Writeline ("Complex zero :"+ Z1 );
Console. Writeline (Z2 +"+"+ Z3 +"="+ (Z2 + Z3 ));

Console. Writeline ("| Z2 | ="+ Z2.magn.pdf );
Console. Writeline ("Phase of Z2 ="+ Z2.phase );

2. We can useComplexformatterClass to help us format the output, as shown below:

 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. Use the following:

  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 ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.