BigInteger
.NET 4.0 introduces a new namespace named System.Numerics(in System.Numerics.dll) , which defines a structure named BigInteger. Represents an arbitrarily large signed integer.
Code Sample:
Sample Output:
Source Code Download.
.NET 4.0 introduces a new namespace named System.Numerics(in System.Numerics.dll) , which defines a structure named BigInteger. Represents an arbitrarily large signed integer.
- The BigInteger type is an immutable type (BigInteger objects are immutable, which means that internally, the common language runtime actually creates a new BigInteger object and assigns it a value one greater than its previous value. )
- The BigInteger arbitrarily large integer whose value in theory has no upper or lower bounds
- OutOfMemoryException can be thrown for any operation that causes a BigInteger value to grow too large
- The uninitialized value of a BigInteger is Zero.
Adding referdnce to Numerics dll |
Reference added |
Metadata info |
Instantiating BigInteger
- You can use the new keyword
1: // using double
2: BigInteger bigIntObjectDouble = new BigInteger(179032.6541);
3: // using long
4: BigInteger bigIntObjectLong = new BigInteger(324234234);
1: byte[] byteArray = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
2: // using Byte Array
3: BigInteger bigIntObjectByByteArr = new BigInteger(byteArray);
- You can declare a BigInteger variable and assign it a value
1: long longValue = 5645634573;
2: // using long value assignment
3: BigInteger bigIntObjectByLongVal = longValue;
- You can assign a decimal or floating-point value to a BigInteger object if you cast the value or convert it first.
1: // using double by casting first
2: BigInteger bigIntObjectByDoubleVal = (BigInteger)9876543.21021;
Code Sample:
1: using System;
2: using System.Numerics;
3:
4: namespace Numieric_BigIntSample
5: {
6: class Program
7: {
8: static void Main(string[] args)
9: {
10:
11: Console.WriteLine("BigInteger Usage\n");
12:
13: #region Instantiating BigInteger
14: Console.WriteLine("Instantiating BigInteger Starts \n");
15: //You can use the new keyword
16: Console.WriteLine("(1) You can use the new keyword");
17:
18: // using double
19: BigInteger bigIntObjectDouble = new BigInteger(987654.3210123);
20: Console.WriteLine(" >using double {0}", bigIntObjectDouble);
21:
22: // using long
23: BigInteger bigIntObjectLong = new BigInteger(9898989898);
24: Console.WriteLine(" >using long {0}", bigIntObjectLong);
25:
26:
27: byte[] byteArray = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
28: // using Byte Array
29: BigInteger bigIntObjectByByteArr = new BigInteger(byteArray);
30: Console.WriteLine(" >using Byte Array {0} \n", bigIntObjectByByteArr);
31:
32:
33: //You can declare a BigInteger variable and assign it a value
34: Console.WriteLine("(2) You can declare a BigInteger variable and assign it a value");
35: long longValue = 5645634573;
36: // using long value assignment
37: BigInteger bigIntObjectByLongVal = longValue;
38: Console.WriteLine(" >using long value assignment {0}\n", bigIntObjectByLongVal);
39:
40: //You can assign a decimal or floating-point value to a BigInteger object if you cast the value or convert it first.
41: Console.WriteLine("(3) You can assign a decimal or floating-point ");
42: Console.WriteLine(" to a BigInteger object if you cast the value first.");
43:
44: // using double by casting first
45: BigInteger bigIntObjectByDoubleVal = (BigInteger)(-987654.3210123);
46: Console.WriteLine(" >using double by casting first {0}\n", bigIntObjectByDoubleVal);
47:
48: Console.WriteLine("Instantiating BigInteger Ends \n");
49: #endregion #region Instantiating BigInteger
50:
51:
52: #region Exploring BigInteger Properties/Methods
53:
54: Console.WriteLine(" Exploring BigInteger Properties/Methods Starts \n");
55:
56: BigInteger divRemainder = new BigInteger();
57: Console.WriteLine(" Default Initialization Value {0} ", divRemainder);
58:
59: Console.WriteLine(" IsEven Property {0} ", bigIntObjectDouble.IsEven);
60:
61: Console.WriteLine(" IsPowerOfTwo Property {0} ", bigIntObjectDouble.IsPowerOfTwo);
62:
63: Console.WriteLine(" Sign Property {0} ", bigIntObjectDouble.Sign);
64:
65: Console.WriteLine(" CompareTo() Method {0} ", bigIntObjectDouble.CompareTo(bigIntObjectByDoubleVal));
66:
67: Console.WriteLine(" ToString() Method {0} ", bigIntObjectByByteArr.ToString("N0"));
68:
69:
70: Console.WriteLine(" Static Abs() Method {0} ", BigInteger.Abs(bigIntObjectByDoubleVal));
71:
72: Console.WriteLine(" Static Add() Method {0} ", BigInteger.Add(bigIntObjectByByteArr, bigIntObjectByDoubleVal));
73:
74: Console.WriteLine(" Static Compare() Method {0} ", BigInteger.Compare(bigIntObjectByByteArr, bigIntObjectByDoubleVal));
75:
76: Console.WriteLine(" Static Divide() Method {0} ", BigInteger.Divide(bigIntObjectByByteArr, bigIntObjectByDoubleVal));
77:
78: BigInteger.DivRem(bigIntObjectByByteArr, bigIntObjectByDoubleVal, out divRemainder);
79:
80: Console.WriteLine(" Static DivRem() Method {0} ", divRemainder);
81:
82: Console.WriteLine(" Static Equals() Method {0} ", BigInteger.Equals(bigIntObjectByDoubleVal, bigIntObjectDouble));
83:
84: Console.WriteLine(" Static GreatestCommonDivisor() Method {0} ", BigInteger.GreatestCommonDivisor(bigIntObjectByByteArr, bigIntObjectDouble));
85:
86: Console.WriteLine(" Static Log() Method {0} ", BigInteger.Log(bigIntObjectByByteArr, 2));
87:
88: Console.WriteLine(" Static Log10() Method {0} ", BigInteger.Log10(bigIntObjectByByteArr));
89:
90: Console.WriteLine(" Static Max() Method {0} ", BigInteger.Max(bigIntObjectByByteArr, bigIntObjectByDoubleVal));
91:
92: Console.WriteLine(" Static Min() Method {0} ", BigInteger.Min(bigIntObjectByByteArr, bigIntObjectByDoubleVal));
93:
94: Console.WriteLine(" Static MinusOne Property {0} ", BigInteger.MinusOne);
95:
96: Console.WriteLine(" Static ModPow() Method {0} ", BigInteger.ModPow(bigIntObjectByByteArr, 200000, bigIntObjectDouble));
97:
98: BigInteger veryBigInt = (BigInteger)999999999999999999;
99: Console.WriteLine(" Static Multiply() Method {0} ", BigInteger.Multiply(bigIntObjectByByteArr, veryBigInt));
100:
101: Console.WriteLine(" Static Negate() Method {0} ", BigInteger.Negate(bigIntObjectByByteArr));
102:
103: Console.WriteLine(" Static One Property {0} ", BigInteger.One);
104:
105: Console.WriteLine(" Static Parse() Method {0} ", BigInteger.Parse("12345678900"));
106:
107: Console.WriteLine(" Static Pow() Method {0} ", BigInteger.Pow(bigIntObjectLong, 4));
108:
109: Console.WriteLine(" Static Remainder() Method {0} ", BigInteger.Remainder(veryBigInt, bigIntObjectLong));
110:
111: Console.WriteLine(" Static Subtract() Method {0} ", BigInteger.Subtract(veryBigInt, bigIntObjectLong));
112:
113:
114: Console.WriteLine("\n Exploring BigInteger Properties/Methods Ends \n");
115:
116: #endregion Exploring BigInteger Properties/Methods
117:
118: Console.WriteLine("Press enter key to exit...");
119: Console.ReadLine();
120: }
121: }
122: }
123:
Sample Output:
1: BigInteger Usage
2:
3: Instantiating BigInteger Starts
4:
5: (1) You can use the new keyword
6: >using double 987654
7: >using long 9898989898
8: >using Byte Array 4759477275222530853130
9:
10: (2) You can declare a BigInteger variable and assign it a value
11: >using long value assignment 5645634573
12:
13: (3) You can assign a decimal or floating-point
14: to a BigInteger object if you cast the value first.
15: >using double by casting first -987654
16:
17: Instantiating BigInteger Ends
18:
19: Exploring BigInteger Properties/Methods Starts
20:
21: Default Initialization Value 0
22: IsEven Property True
23: IsPowerOfTwo Property False
24: Sign Property 1
25: CompareTo() Method 1
26: ToString() Method 4,759,477,275,222,530,853,130
27: Static Abs() Method 987654
28: Static Add() Method 4759477275222529865476
29: Static Compare() Method 1
30: Static Divide() Method -4818972307328812
31: Static DivRem() Method 366082
32: Static Equals() Method False
33: Static GreatestCommonDivisor() Method 2
34: Static Log() Method 72.0112931262823
35: Static Log10() Method 21.6775592575624
36: Static Max() Method 4759477275222530853130
37: Static Min() Method -987654
38: Static MinusOne Property -1
39: Static ModPow() Method 494470
40: Static Multiply() Method 4759477275222530848370522724777469146870
41: Static Negate() Method -4759477275222530853130
42: Static One Property 1
43: Static Parse() Method 12345678900
44: Static Pow() Method 9602040296118372759588519315172988563216
45: Static Remainder() Method 1716161615
46: Static Subtract() Method 999999990101010101
47:
48: Exploring BigInteger Properties/Methods Ends
49:
50: Press enter key to exit...
51:
Source Code Download.