public struct Single : IComparable, IFormattable, IComparable<Single>, IEquatable<Single>
Object
ValueType
SingleThis type implements IComparable, IFormattable, System.IComparable<System.Single>, and System.IEquatable<System.Single>.
mscorlib
ExtendedNumerics
Represents a 32-bit single-precision floating-point number.
Single is a 32-bit single precision floating-point type that represents values ranging from approximately 1.5E-45 to 3.4E+38 and from approximately -1.5E-45 to -3.4E+38 with a precision of 7 decimal digits. The Single type conforms to standard IEC 60559:1989, Binary Floating-point Arithmetic for Microprocessor Systems.A Single can represent the following values:
When performing binary operations, if one of the operands is a floating-point type (Double or Single ), then the other operand is required to be an integral type or a floating-point type and the operation is evaluated as follows:
- The finite set of non-zero values of the form s * m * 2e, where s is 1 or -1, and 0 < m < 224 and -149 <= e <= 104.
- Positive infinity and negative infinity. Infinities are produced by operations that produce results with a magnitude greater than that which can be represented by a Single, such as dividing a non-zero number by zero. For example, using Single operands,
1.0 / 0.0
yields positive infinity, and-1.0 / 0.0
yields negative infinity. Operations include passing parameters and returning values.- The Not-a-Number value (NaN). NaN values are produced by invalid floating-point operations, such as dividing zero by zero.
The floating-point operators, including the assignment operators, do not throw exceptions. Instead, in exceptional situations, the result of a floating-point operation is zero, infinity, or NaN, as described below:
- If one of the operands is of an integral type, then that operand is converted to the floating-point type of the other operand.
- Then, if either of the operands is of type Double, the other operand is converted to Double, and the operation is performed using at least the range and precision of the Double type. For numeric operations, the type of the result is Double.
- Otherwise, the operation is performed using at least the range and precision of the Single type and, for numeric operations, the type of the result is Single.
Conforming implementations of the CLI are permitted to perform floating-point operations using a precision that is higher than that required by the Single type. For example, hardware architectures that support an "extended" or "long double" floating-point type with greater range and precision than the Single type could implicitly perform all floating-point operations using this higher precision type. Expressions evaluated using a higher precision might cause a finite result to be produced instead of an infinity.
- If the result of a floating-point operation is too small for the destination format, the result of the operation is zero.
- If the magnitude of the result of a floating-point operation is too large for the destination format, the result of the operation is positive infinity or negative infinity.
- If a floating-point operation is invalid, the result of the operation is NaN.
- If one or both operands of a floating-point operation are NaN, the result of the operation is NaN.
System Namespace
Single Methods
Single.CompareTo(float) Method
Single.CompareTo(System.Object) Method
Single.Equals(System.Object) Method
Single.Equals(float) Method
Single.GetHashCode Method
Single.IsInfinity Method
Single.IsNaN Method
Single.IsNegativeInfinity Method
Single.IsPositiveInfinity Method
Single.Parse(System.String) Method
Single.Parse(System.String, System.Globalization.NumberStyles) Method
Single.Parse(System.String, System.IFormatProvider) Method
Single.Parse(System.String, System.Globalization.NumberStyles, System.IFormatProvider) Method
Single.ToString(System.String, System.IFormatProvider) Method
Single.ToString(System.IFormatProvider) Method
Single.ToString() Method
Single.ToString(System.String) Method
Single Fields
Single.Epsilon Field
Single.MaxValue Field
Single.MinValue Field
Single.NaN Field
Single.NegativeInfinity Field
Single.PositiveInfinity Field
public int CompareTo(float value);
Returns the sort order of the current instance compared to the specified Single .
- value
- The Single to compare to the current instance.
The return value is a negative number, zero, or a positive number reflecting the sort order of the current instance as compared to value. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:
Return Value Description Any negative number Current instance < value. -or-
Current instance is a NaN and value is not a NaN.
Zero Current instance == value . -or-
Current instance and value are both NaN, positive infinity, or negative infinity.
A positive number Current instance > value. -or-
Current instance is not a NaN and value is a NaN.
[Note: This method is implemented to support the System.IComparable<Single> interface.]
System.Single Structure, System Namespace
public int CompareTo(object value);
Returns the sort order of the current instance compared to the specified Object .
- value
- The Object to compare to the current instance.
The return value is a negative number, zero, or a positive number reflecting the sort order of the current instance as compared to value. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:
Return Value Description Any negative number Current instance < value. -or-
Current instance is a NaN and value is not a NaN and is not a null reference.
Zero Current instance == value . -or-
Current instance and value are both NaN, positive infinity, or negative infinity.
A positive number Current instance > value. -or-
value is a null reference.
-or-
Current instance is not a NaN and value is a NaN.
Exception Type Condition ArgumentException value is not a null reference and is not of type Single.
[Note: This method is implemented to support the IComparable interface. Note that, although a NaN is not considered to be equal to another NaN (even itself), the IComparable interface requires that A.CompareTo(A) return zero. ]
System.Single Structure, System Namespace
public override bool Equals(object obj);
Determines whether the current instance and the specified Object represent the same type and value.
- obj
- The Object to compare to the current instance.
true
if obj represents the same type and value as the current instance, otherwisefalse
. If obj is a null reference or is not an instance of Single, returnsfalse
. If either obj or the current instance is a NaN and the other is not, returnsfalse
. If obj and the current instance are both NaN, positive infinity, or negative infinity, returnstrue
.
[Note: This method overrides System.Object.Equals(System.Object). ]
System.Single Structure, System Namespace
public override bool Equals(float obj);
Determines whether the current instance and the specified Single represent the same value.
- obj
- The Single to compare to the current instance.
true
if obj represents the same value as the current instance, otherwisefalse
. If either obj or the current instance is a NaN and the other is not, returnsfalse
. If obj and the current instance are both NaN, positive infinity, or negative infinity, returnstrue
.
[Note: This method is implemented to support the System.IEquatable<Single> interface.]
System.Single Structure, System Namespace
public override int GetHashCode();
Generates a hash code for the current instance.
A Int32 containing the hash code for this instance.
The algorithm used to generate the hash code is unspecified.[Note: This method overrides System.Object.GetHashCode. ]
System.Single Structure, System Namespace
public static bool IsInfinity(float f);
Determines whether the specified Single represents an infinity, which can be either positive or negative.
- f
- The Single to be checked.
true
if f represents a positive or negative infinity value; otherwisefalse
.
[Note: Floating-point operations return positive or negative infinity values to signal an overflow condition. ]
System.Single Structure, System Namespace
public static bool IsNaN(float f);
Determines whether the value of the specified Single is undefined (Not-a-Number).
- f
- The Single to be checked.
true
if f represents a NaN value; otherwisefalse
.
[Note: Floating-point operations return NaN values to signal that the result of the operation is undefined. For example, dividing (Single) 0.0 by 0.0 results in a NaN value.]
System.Single Structure, System Namespace
public static bool IsNegativeInfinity(float f);
Determines whether the specified Single represents a negative infinity value.
- f
- The Single to be checked.
true
if f represents a negative infinity value; otherwisefalse
.
[Note: Floating-point operations return negative infinity values to signal an overflow condition. ]
System.Single Structure, System Namespace
public static bool IsPositiveInfinity(float f);
Determines whether the specified Single represents a positive infinity value.
- f
- The Single to be checked.
true
if f represents a positive infinity value; otherwisefalse
.
[Note: Floating-point operations return positive infinity values to signal an overflow condition. ]
System.Single Structure, System Namespace
public static float Parse(string s);
Returns the specified String converted to a Single value.
- s
- A String containing the value to convert. The string is interpreted using the System.Globalization.NumberStyles.Float and/or System.Globalization.NumberStyles.AllowThousands style.
The Single value obtained from s. If s equals System.Globalization.NumberFormatInfo.NaNSymbol, this method returns System.Single.NaN .
Exception Type Condition ArgumentNullException s is a null reference. FormatException s is not in the correct style. OverflowException s represents a value that is less than System.Single.MinValue or greater than System.Single.MaxValue.
This version of System.Single.Parse(System.String) is equivalent to System.Single.Parse(System.String)(s, System.Globalization.NumberStyles.Float| System.Globalization.NumberStyles.AllowThousands,null
).The string s is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture. [Note: For more information, see System.Globalization.NumberFormatInfo.CurrentInfo. ]
System.Single Structure, System Namespace
public static float Parse(string s, NumberStyles style);
Returns the specified String converted to a Single value.
- s
- A String containing the value to convert. The string is interpreted using the style specified by style .
- style
- Zero or more NumberStyles values that specify the style of s. Specify multiple values for style using the bitwise OR operator. If style is a null reference, the string is interpreted using the System.Globalization.NumberStyles.Float and System.Globalization.NumberStyles.AllowThousands styles.
The Single value obtained from s. If s equals System.Globalization.NumberFormatInfo.NaNSymbol, this method returns System.Single.NaN .
Exception Type Condition ArgumentNullException s is a null reference. FormatException s is not in the correct style. OverflowException s represents a value that is less than System.Single.MinValue or greater than System.Single.MaxValue.
This version of System.Single.Parse(System.String) is equivalent to System.Single.Parse(System.String) (s, style, null).The string s is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture. [Note: For more information, see System.Globalization.NumberFormatInfo.CurrentInfo. ]
System.Single Structure, System Namespace
public static float Parse(string s, IFormatProvider provider);
Returns the specified String converted to a Single value.
- s
- A String containing the value to convert. The string is interpreted using the System.Globalization.NumberStyles.Float and/or System.Globalization.NumberStyles.AllowThousands style.
- provider
- A IFormatProvider that supplies a NumberFormatInfo containing culture-specific formatting information about s.
The Single value obtained from s. If s equals System.Globalization.NumberFormatInfo.NaNSymbol, this method returns System.Single.NaN .
Exception Type Condition ArgumentNullException s is a null reference. FormatException s is not in the correct style. OverflowException s represents a value that is less than System.Single.MinValue or greater than System.Single.MaxValue.
This version of System.Single.Parse(System.String) is equivalent to System.Single.Parse(System.String) (s, System.Globalization.NumberStyles.Float | System.Globalization.NumberStyles.AllowThousands , provider).The string s is parsed using the culture-specific formatting information from the NumberFormatInfo instance supplied by provider. If provider is
null
or a NumberFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.
System.Single Structure, System Namespace
public static float Parse(string s, NumberStyles style, IFormatProvider provider);
Returns the specified String converted to a Single value.
- s
- A String containing the value to convert. The string is interpreted using the style specified by style .
- style
- Zero or more NumberStyles values that specify the style of s. Specify multiple values for style using the bitwise OR operator. If style is a null reference, the string is interpreted using the System.Globalization.NumberStyles.Float and System.Globalization.NumberStyles.AllowThousands styles.
- provider
- A IFormatProvider that supplies a NumberFormatInfo containing culture-specific formatting information about s.
The Single value obtained from s. If s equals System.Globalization.NumberFormatInfo.NaNSymbol, this method returns NaN.
Exception Type Condition ArgumentNullException s is a null reference. FormatException s is not in the correct style. OverflowException s represents a value that is less than System.Single.MinValue or greater than System.Single.MaxValue.
The string s is parsed using the culture-specific formatting information from the NumberFormatInfo instance supplied by provider. If provider isnull
or a NumberFormatInfo cannot be obtained from provider , the formatting information for the current system culture is used.
System.Single Structure, System Namespace
public string ToString(string format, IFormatProvider provider);
Returns a String representation of the value of the current instance.
- format
- A String containing a character that specifies the format of the returned string, optionally followed by a non-negative integer that specifies the precision of the number in the returned String.
- provider
- A IFormatProvider that supplies a NumberFormatInfo instance containing culture-specific formatting information.
A String representation of the current instance formatted as specified by format. The string takes into account the information in the NumberFormatInfo instance supplied by provider.
Exception Type Condition FormatException format is invalid.
If provider isnull
or a NumberFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.If format is a null reference, the general format specifier "G" is used.
The following table lists the format characters that are valid for the Single type.
[Note: For a detailed description of the format strings, see the IFormattable interface.
Format Characters Description "C", "c" Currency format. "E", "e" Exponential notation format. "F", "f" Fixed-point format. "G", "g" General format. "N", "n" Number format. "P", "p" Percent format. "R", "r" Round-trip format. This method is implemented to support the IFormattable interface.
]
System.Single Structure, System Namespace
public string ToString(IFormatProvider provider);
Returns a String representation of the value of the current instance.
- provider
- A IFormatProvider that supplies a NumberFormatInfo containing culture-specific formatting information.
A String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the formatting information in the NumberFormatInfo instance supplied by provider.
This version of System.Single.ToString is equivalent to System.Single.ToString (null
, provider ).If provider is
null
or a NumberFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.[Note: The general format specifier formats the number in either fixed-point or exponential notation form. For a detailed description of the general format, see the IFormattable interface. ]
System.Single Structure, System Namespace
public override string ToString();
Returns a String representation of the value of the current instance.
A String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the current system culture.
This version of System.Single.ToString is equivalent to System.Single.ToString (null
,null
).[Note: The general format specifier formats the number in either fixed-point or exponential notation form. For a detailed description of the general format, see the IFormattable interface.
This method overrides System.Object.ToString.
]
System.Single Structure, System Namespace
public string ToString(string format);
Returns a String representation of the value of the current instance.
- format
- A String that specifies the format of the returned string. [Note: For a list of valid values, see System.Single.ToString (String, IFormatProvider ).]
A String representation of the current instance formatted as specified by format . The string takes into account the current system culture.
Exception Type Condition FormatException format is invalid.
This version of System.Single.ToString is equivalent to System.Single.ToString (format,null
).If format is a null reference, the general format specifier "G" is used.
The following example shows the effects of various formats on the string returned by System.Single.ToString .
using System; class test { public static void Main() { float f = 1234.567f; Console.WriteLine(f); string[] fmts = {"C","E","e5","F","G","N","P","R"}; for (int i=0;i<fmts.Length;i++) Console.WriteLine("{0}: {1}", fmts[i],f.ToString(fmts[i])); } }The output is
1234.567
C: $1,234.57
E: 1.234567E+003
e5: 1.23457e+003
F: 1234.57
G: 1234.567
N: 1,234.57
P: 123,456.70 %
R: 1234.567
System.Single Structure, System Namespace
public const float Epsilon = (float)1.401298E-45;
Represents the smallest positive Single value greater than zero.
The value of this constant is 1.401298E-45.
System.Single Structure, System Namespace
public const float MaxValue = (float)3.402823E+38;
Contains the maximum positive value for the Single type.
The value of this constant is 3.40282346638528859E+38 converted to Single .
System.Single Structure, System Namespace
public const float MinValue = (float)-3.402823E+38;
Contains the minimum (most negative) value for the Single type.
The value of this constant is -3.40282346638528859E+38 converted to Single .
System.Single Structure, System Namespace
public const float NaN = (float)0.0 / (float)0.0;
Represents an undefined result of operations involving Single .
Not-a-Number (NaN) values are returned when the result of a Single operation is undefined.A NaN value is not equal to any other value, including another NaN value.
The value of this field is obtained by dividing Single zero by zero.
[Note: System.Single.NaN represents one of many possible NaN values. To test whether a Single value is a NaN, use the System.Single.IsNaN(System.Single) method. ]
System.Single Structure, System Namespace
public const float NegativeInfinity = (float)-1.0 / (float)0.0;
Represents a negative infinity of type Single .
The value of this constant can be obtained by dividing a negative Single by zero.[Note: To test whether a Single value is a negative infinity value, use the System.Single.IsNegativeInfinity(System.Single) method. ]
System.Single Structure, System Namespace
public const float PositiveInfinity = (float)1.0 / (float)0.0;
Represents a positive infinity of type Single.
The value of this constant can be obtained by dividing a positive Single by zero.[Note: To test whether a Single value is a positive infinity value, use the System.Single.IsPositiveInfinity(System.Single) method. ]
System.Single Structure, System Namespace