BigInteger Constructor (Double)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the BigInteger structure using a double-precision floating-point value.

Namespace:  System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

Syntax

'Declaration
Public Sub New ( _
    value As Double _
)
public BigInteger(
    double value
)

Parameters

  • value
    Type: System.Double
    A double-precision floating-point value.

Exceptions

Exception Condition
OverflowException

The value of value is Double.NaN.

-or-

The value of value is Double.NegativeInfinity.

-or-

The value of value is Double.PositiveInfinity.

Remarks

Any fractional part of the value parameter is truncated when instantiating a BigInteger object.

Because of the lack of precision of the Double data type, calling this constructor can cause data loss.

The BigInteger value that results from calling this constructor is identical to the value that results from explicitly assigning a Double value to a BigInteger.

Examples

The following example illustrates the use of the BigInteger(Double) constructor to instantiate a BigInteger object. It also illustrates the loss of precision that may occur when you use the Double data type. A Double is assigned a large value, which is then assigned to a BigInteger object. As the output shows, this assignment involves a loss of precision. Both values are then incremented by one. The output shows that the BigInteger object reflects the changed value, whereas the Double object does not.

' Create a BigInteger from a large double value.
Dim doubleValue As Double = -6.0E+20
Dim bigIntValue As New BigInteger(doubleValue)
outputBlock.Text += String.Format("Original Double value: {0:N0}", doubleValue) & vbCrLf
outputBlock.Text += String.Format("Original BigInteger value: {0}", bigIntValue) & vbCrLf
' Increment and then display both values.
doubleValue += 1
bigIntValue += BigInteger.One
outputBlock.Text += String.Format("Incremented Double value: {0:N0}", doubleValue) & vbCrLf
outputBlock.Text += String.Format("Incremented BigInteger value: {0}", bigIntValue) & vbCrLf
' The example displays the following output:
'    Original Double value: -600,000,000,000,000,000,000
'    Original BigInteger value: -600000000000000000000
'    Incremented Double value: -600,000,000,000,000,000,000
'    Incremented BigInteger value: -599999999999999999999
// Create a BigInteger from a large double value.
double doubleValue = -6e20;
BigInteger bigIntValue = new BigInteger(doubleValue);
outputBlock.Text += String.Format("Original Double value: {0:N0}", doubleValue) + "\n";
outputBlock.Text += String.Format("Original BigInteger value: {0}", bigIntValue) + "\n";
// Increment and then display both values.
doubleValue++;
bigIntValue += BigInteger.One;
outputBlock.Text += String.Format("Incremented Double value: {0:N0}", doubleValue) + "\n";
outputBlock.Text += String.Format("Incremented BigInteger value: {0}", bigIntValue) + "\n";
// The example displays the following output:
//    Original Double value: -600,000,000,000,000,000,000
//    Original BigInteger value: -600000000000000000000
//    Incremented Double value: -600,000,000,000,000,000,000
//    Incremented BigInteger value: -599999999999999999999

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.