Export (0) Print
Expand All

BigInteger Explicit Conversion (BigInteger to Single)

Defines an explicit conversion of a BigInteger object to a single-precision floating-point value.

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

public static explicit operator float (
	BigInteger value
)

Parameters

value
Type: System.Numerics.BigInteger

The value to convert to a single-precision floating-point value.

Return Value

Type: System.Single
An object that contains the closest possible representation of the value parameter.

The overloads of the Explicit method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss or a loss of precision. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CSng in Visual Basic) is used. Otherwise, they display a compiler error.

Because the BigInteger value can be outside the range of the Single data type, this operation is a narrowing conversion. If the conversion is unsuccessful, it does not throw an OverflowException. Instead, if the BigInteger value is less than Single.MinValue, the resulting Single value is Single.NegativeInfinity. If the BigInteger value is greater than Single.MaxValue, the resulting Single value is Single.PositiveInfinity.

The conversion of a BigInteger to a Single may involve a loss of precision. In some cases, the loss of precision may cause the casting or conversion operation to succeed even if the BigInteger value is outside the range of the Single data type. The following example provides an illustration. It assigns the maximum value of a Single to two BigInteger variables, increments one BigInteger variable by 9.999e291, and tests the two variables for equality. As expected, the call to the Equals(BigInteger) method shows that they are unequal. However, the conversion of the larger BigInteger value back to a Single succeeds, although the BigInteger value now exceeds Single.MaxValue.

// Increase a BigInteger so it exceeds Single.MaxValue.
BigInteger number1 = (BigInteger) Single.MaxValue;
BigInteger number2 = number1;
number2 = number2 + (BigInteger) 9.999e30;
// Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1));

// Convert the BigInteger to a Single. 
float sng = (float) number2;

// Display the two values.
Console.WriteLine("BigInteger: {0}", number2);
Console.WriteLine("Single:     {0}", sng); 
// The example displays the following output: 
//       BigIntegers equal: False 
//       BigInteger: 3.4028235663752885981170396038E+38 
//       Single:     3.402823E+38

The following example illustrates the conversion of BigInteger values to Single values.

// BigInteger to Single conversion.
BigInteger goodSingle = (BigInteger) 102.43e22F;
BigInteger badSingle = (BigInteger) float.MaxValue;  
badSingle = badSingle * 2;

float singleFromBigInteger;

// Successful conversion using cast operator. 
singleFromBigInteger = (float) goodSingle;
Console.WriteLine(singleFromBigInteger);

// Convert an out-of-bounds BigInteger value to a Single.
singleFromBigInteger = (float) badSingle;
Console.WriteLine(singleFromBigInteger);

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft